Skip to content

Commit b7ccc01

Browse files
author
Sylvain Lebresne
committed
Fix case where __typename is alongside inline fragments
The code from apollographql#2137 is optimize some use of __typename when other fields are queried alongside it. But when __typename had only inline fragments alongside it, the logic was flawed and was discarding any potential instance of the optimization done somewhere more nested. This fixes that problem.
1 parent c99de57 commit b7ccc01

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

query-planner-js/src/buildPlan.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -1974,19 +1974,24 @@ function optimizeSiblingTypenames(selectionSet: SelectionSet): SelectionSet {
19741974
}
19751975
}
19761976

1977-
if (!updatedSelections || (typenameSelection && !firstFieldSelection)) {
1978-
// This means that either no selection was modified at all, or there is no other field selection than __typename one.
1979-
// In both case, we want to return the current selectionSet unmodified.
1977+
if (!updatedSelections || updatedSelections.length === 0) {
1978+
// No selection was modified at all, or there is no other field selection than __typename one.
1979+
// In both case, we just return the current selectionSet unmodified.
19801980
return selectionSet;
19811981
}
19821982

1983-
// If we have some __typename selection that was removed but need to be "remembered" for later, "tag" whichever first
1984-
// field selection is still part of the operation (what we tag doesn't matter since again, __typename can be queried from
1985-
// anywhere and that has no performance impact).
1983+
// If we have some __typename selection that was removed but need to be "remembered" for later,
1984+
// "tag" whichever first field selection is still part of the operation.
19861985
if (typenameSelection) {
1987-
assert(firstFieldSelection, 'Should not have got here');
1988-
// Note that as we tag the element, we also record the alias used if any since that needs to be preserved.
1989-
firstFieldSelection.element().addAttachement(SIBLING_TYPENAME_KEY, typenameSelection.field.alias ? typenameSelection.field.alias : '');
1986+
if (firstFieldSelection) {
1987+
// Note that as we tag the element, we also record the alias used if any since that needs to be preserved.
1988+
firstFieldSelection.element().addAttachement(SIBLING_TYPENAME_KEY, typenameSelection.field.alias ? typenameSelection.field.alias : '');
1989+
} else {
1990+
// If we have no other field selection, then we can't optimize __typename and we need to add
1991+
// it back to the updated subselections (we add it first because that's usually where we
1992+
// put __typename by convention).
1993+
updatedSelections = [typenameSelection as Selection].concat(updatedSelections);
1994+
}
19901995
}
19911996
return new SelectionSet(selectionSet.parentType, selectionSet.fragments).addAll(updatedSelections)
19921997
}

0 commit comments

Comments
 (0)