diff --git a/src/execution/IncrementalGraph.ts b/src/execution/IncrementalGraph.ts index 6dd07747c0..83ce78089b 100644 --- a/src/execution/IncrementalGraph.ts +++ b/src/execution/IncrementalGraph.ts @@ -73,15 +73,13 @@ class DeferredFragmentFactory { if (depth === 0) { return; } + const stack: Array = []; let currentPath = path; - invariant(currentPath !== undefined); - let currentDepth = currentPath.depth; - while (currentDepth > depth) { + while (currentPath !== undefined) { + stack.unshift(currentPath); currentPath = currentPath.prev; - invariant(currentPath !== undefined); - currentDepth = currentPath.depth; } - return currentPath; + return stack[depth - 1]; } } diff --git a/src/execution/__tests__/executor-test.ts b/src/execution/__tests__/executor-test.ts index f6f98143a5..de33f8c91b 100644 --- a/src/execution/__tests__/executor-test.ts +++ b/src/execution/__tests__/executor-test.ts @@ -239,7 +239,7 @@ describe('Execute: Handles basic execution tasks', () => { const field = operation.selectionSet.selections[0]; expect(resolvedInfo).to.deep.include({ fieldNodes: [field], - path: { prev: undefined, key: 'result', typename: 'Test', depth: 1 }, + path: { prev: undefined, key: 'result', typename: 'Test' }, variableValues: { var: 'abc' }, }); }); @@ -291,15 +291,12 @@ describe('Execute: Handles basic execution tasks', () => { expect(path).to.deep.equal({ key: 'l2', typename: 'SomeObject', - depth: 3, prev: { key: 0, typename: undefined, - depth: 2, prev: { key: 'l1', typename: 'SomeQuery', - depth: 1, prev: undefined, }, }, diff --git a/src/execution/collectFields.ts b/src/execution/collectFields.ts index cfece964fb..c6688ae8f8 100644 --- a/src/execution/collectFields.ts +++ b/src/execution/collectFields.ts @@ -1,6 +1,8 @@ import { AccumulatorMap } from '../jsutils/AccumulatorMap.js'; import { invariant } from '../jsutils/invariant.js'; import type { ObjMap } from '../jsutils/ObjMap.js'; +import type { Path } from '../jsutils/Path.js'; +import { pathToArray } from '../jsutils/Path.js'; import type { FieldNode, @@ -100,7 +102,7 @@ export function collectSubfields( operation: OperationDefinitionNode, returnType: GraphQLObjectType, fieldGroup: FieldGroup, - depth: number, + path: Path, ): GroupedFieldSet { const context: CollectFieldsContext = { schema, @@ -119,8 +121,8 @@ export function collectSubfields( context, node.selectionSet, subGroupedFieldSet, + path, deferUsage, - depth, ); } } @@ -134,8 +136,8 @@ function collectFieldsImpl( groupedFieldSet: AccumulatorMap & { encounteredDefer?: boolean; }, + path?: Path, deferUsage?: DeferUsage, - depth = 0, ): void { const { schema, @@ -170,8 +172,8 @@ function collectFieldsImpl( operation, variableValues, selection, + path, deferUsage, - depth, ); if (!newDeferUsage) { @@ -179,8 +181,8 @@ function collectFieldsImpl( context, selection.selectionSet, groupedFieldSet, + path, deferUsage, - depth, ); } else { groupedFieldSet.encounteredDefer = true; @@ -188,8 +190,8 @@ function collectFieldsImpl( context, selection.selectionSet, groupedFieldSet, + path, newDeferUsage, - depth, ); } @@ -202,8 +204,8 @@ function collectFieldsImpl( operation, variableValues, selection, + path, deferUsage, - depth, ); if ( @@ -227,8 +229,8 @@ function collectFieldsImpl( context, fragment.selectionSet, groupedFieldSet, + path, deferUsage, - depth, ); } else { groupedFieldSet.encounteredDefer = true; @@ -236,8 +238,8 @@ function collectFieldsImpl( context, fragment.selectionSet, groupedFieldSet, + path, newDeferUsage, - depth, ); } break; @@ -255,8 +257,8 @@ function getDeferUsage( operation: OperationDefinitionNode, variableValues: { [variable: string]: unknown }, node: FragmentSpreadNode | InlineFragmentNode, + path: Path | undefined, parentDeferUsage: DeferUsage | undefined, - depth: number, ): DeferUsage | undefined { const defer = getDirectiveValues(GraphQLDeferDirective, node, variableValues); @@ -276,7 +278,7 @@ function getDeferUsage( return { label: typeof defer.label === 'string' ? defer.label : undefined, parentDeferUsage, - depth, + depth: pathToArray(path).length, }; } diff --git a/src/execution/execute.ts b/src/execution/execute.ts index 1a591f35b2..1e208b41a8 100644 --- a/src/execution/execute.ts +++ b/src/execution/execute.ts @@ -88,7 +88,7 @@ const collectSubfields = memoize3( exeContext: ExecutionContext, returnType: GraphQLObjectType, fieldGroup: FieldGroup, - depth: number, + path: Path, ) => _collectSubfields( exeContext.schema, @@ -97,7 +97,7 @@ const collectSubfields = memoize3( exeContext.operation, returnType, fieldGroup, - depth, + path, ), ); @@ -1630,7 +1630,7 @@ function collectAndExecuteSubfields( exeContext, returnType, fieldGroup, - path.depth, + path, ); if ( !exeContext.encounteredDefer && diff --git a/src/jsutils/Path.ts b/src/jsutils/Path.ts index 0afde2ea4a..d223b6e752 100644 --- a/src/jsutils/Path.ts +++ b/src/jsutils/Path.ts @@ -4,7 +4,6 @@ export interface Path { readonly prev: Path | undefined; readonly key: string | number; readonly typename: string | undefined; - readonly depth: number; } /** @@ -15,12 +14,7 @@ export function addPath( key: string | number, typename: string | undefined, ): Path { - return { - prev, - key, - typename, - depth: prev === undefined ? 1 : prev.depth + 1, - }; + return { prev, key, typename }; } /** diff --git a/src/jsutils/__tests__/Path-test.ts b/src/jsutils/__tests__/Path-test.ts index c5c8762745..0484377db9 100644 --- a/src/jsutils/__tests__/Path-test.ts +++ b/src/jsutils/__tests__/Path-test.ts @@ -11,7 +11,6 @@ describe('Path', () => { prev: undefined, key: 1, typename: 'First', - depth: 1, }); }); @@ -23,7 +22,6 @@ describe('Path', () => { prev: first, key: 'two', typename: 'Second', - depth: 2, }); });