diff --git a/src/execution/buildFieldPlan.ts b/src/execution/buildFieldPlan.ts index d29ae94cde..51e2fdf8bb 100644 --- a/src/execution/buildFieldPlan.ts +++ b/src/execution/buildFieldPlan.ts @@ -19,53 +19,22 @@ export function buildFieldPlan( parentDeferUsages: DeferUsageSet = new Set(), ): FieldPlan { const groupedFieldSet = new Map(); - const newGroupedFieldSets = new Map>(); - - const map = new Map< - string, - { - deferUsageSet: DeferUsageSet; - fieldGroup: FieldGroup; - } - >(); - for (const [responseKey, fieldGroup] of originalGroupedFieldSet) { - const deferUsageSet = new Set(); - let inOriginalResult = false; - for (const fieldDetails of fieldGroup) { - const deferUsage = fieldDetails.deferUsage; - if (deferUsage === undefined) { - inOriginalResult = true; - continue; - } - deferUsageSet.add(deferUsage); - } - if (inOriginalResult) { - deferUsageSet.clear(); - } else { - deferUsageSet.forEach((deferUsage) => { - const ancestors = getAncestors(deferUsage); - for (const ancestor of ancestors) { - if (deferUsageSet.has(ancestor)) { - deferUsageSet.delete(deferUsage); - } - } - }); - } - map.set(responseKey, { deferUsageSet, fieldGroup }); - } + const filteredDeferUsageSet = getFilteredDeferUsageSet(fieldGroup); - for (const [responseKey, { deferUsageSet, fieldGroup }] of map) { - if (isSameSet(deferUsageSet, parentDeferUsages)) { + if (isSameSet(filteredDeferUsageSet, parentDeferUsages)) { groupedFieldSet.set(responseKey, fieldGroup); continue; } - let newGroupedFieldSet = getBySet(newGroupedFieldSets, deferUsageSet); + let newGroupedFieldSet = getBySet( + newGroupedFieldSets, + filteredDeferUsageSet, + ); if (newGroupedFieldSet === undefined) { newGroupedFieldSet = new Map(); - newGroupedFieldSets.set(deferUsageSet, newGroupedFieldSet); + newGroupedFieldSets.set(filteredDeferUsageSet, newGroupedFieldSet); } newGroupedFieldSet.set(responseKey, fieldGroup); } @@ -76,12 +45,28 @@ export function buildFieldPlan( }; } -function getAncestors(deferUsage: DeferUsage): ReadonlyArray { - const ancestors: Array = []; - let parentDeferUsage: DeferUsage | undefined = deferUsage.parentDeferUsage; - while (parentDeferUsage !== undefined) { - ancestors.unshift(parentDeferUsage); - parentDeferUsage = parentDeferUsage.parentDeferUsage; +function getFilteredDeferUsageSet( + fieldGroup: FieldGroup, +): ReadonlySet { + const filteredDeferUsageSet = new Set(); + for (const fieldDetails of fieldGroup) { + const deferUsage = fieldDetails.deferUsage; + if (deferUsage === undefined) { + filteredDeferUsageSet.clear(); + return filteredDeferUsageSet; + } + filteredDeferUsageSet.add(deferUsage); + } + + for (const deferUsage of filteredDeferUsageSet) { + let parentDeferUsage: DeferUsage | undefined = deferUsage.parentDeferUsage; + while (parentDeferUsage !== undefined) { + if (filteredDeferUsageSet.has(parentDeferUsage)) { + filteredDeferUsageSet.delete(deferUsage); + break; + } + parentDeferUsage = parentDeferUsage.parentDeferUsage; + } } - return ancestors; + return filteredDeferUsageSet; }