Skip to content

Commit

Permalink
do not modify Path interface
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Jul 25, 2024
1 parent b5dfb51 commit 8cdcbac
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 33 deletions.
10 changes: 4 additions & 6 deletions src/execution/IncrementalGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@ class DeferredFragmentFactory {
if (depth === 0) {
return;
}
const stack: Array<Path> = [];
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];
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
});
});
Expand Down Expand Up @@ -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,
},
},
Expand Down
24 changes: 13 additions & 11 deletions src/execution/collectFields.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -100,7 +102,7 @@ export function collectSubfields(
operation: OperationDefinitionNode,
returnType: GraphQLObjectType,
fieldGroup: FieldGroup,
depth: number,
path: Path,
): GroupedFieldSet {
const context: CollectFieldsContext = {
schema,
Expand All @@ -119,8 +121,8 @@ export function collectSubfields(
context,
node.selectionSet,
subGroupedFieldSet,
path,
deferUsage,
depth,
);
}
}
Expand All @@ -134,8 +136,8 @@ function collectFieldsImpl(
groupedFieldSet: AccumulatorMap<string, FieldDetails> & {
encounteredDefer?: boolean;
},
path?: Path,
deferUsage?: DeferUsage,
depth = 0,
): void {
const {
schema,
Expand Down Expand Up @@ -170,26 +172,26 @@ function collectFieldsImpl(
operation,
variableValues,
selection,
path,
deferUsage,
depth,
);

if (!newDeferUsage) {
collectFieldsImpl(
context,
selection.selectionSet,
groupedFieldSet,
path,
deferUsage,
depth,
);
} else {
groupedFieldSet.encounteredDefer = true;
collectFieldsImpl(
context,
selection.selectionSet,
groupedFieldSet,
path,
newDeferUsage,
depth,
);
}

Expand All @@ -202,8 +204,8 @@ function collectFieldsImpl(
operation,
variableValues,
selection,
path,
deferUsage,
depth,
);

if (
Expand All @@ -227,17 +229,17 @@ function collectFieldsImpl(
context,
fragment.selectionSet,
groupedFieldSet,
path,
deferUsage,
depth,
);
} else {
groupedFieldSet.encounteredDefer = true;
collectFieldsImpl(
context,
fragment.selectionSet,
groupedFieldSet,
path,
newDeferUsage,
depth,
);
}
break;
Expand All @@ -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);

Expand All @@ -276,7 +278,7 @@ function getDeferUsage(
return {
label: typeof defer.label === 'string' ? defer.label : undefined,
parentDeferUsage,
depth,
depth: pathToArray(path).length,
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const collectSubfields = memoize3(
exeContext: ExecutionContext,
returnType: GraphQLObjectType,
fieldGroup: FieldGroup,
depth: number,
path: Path,
) =>
_collectSubfields(
exeContext.schema,
Expand All @@ -97,7 +97,7 @@ const collectSubfields = memoize3(
exeContext.operation,
returnType,
fieldGroup,
depth,
path,
),
);

Expand Down Expand Up @@ -1630,7 +1630,7 @@ function collectAndExecuteSubfields(
exeContext,
returnType,
fieldGroup,
path.depth,
path,
);
if (
!exeContext.encounteredDefer &&
Expand Down
8 changes: 1 addition & 7 deletions src/jsutils/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export interface Path {
readonly prev: Path | undefined;
readonly key: string | number;
readonly typename: string | undefined;
readonly depth: number;
}

/**
Expand All @@ -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 };
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/jsutils/__tests__/Path-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('Path', () => {
prev: undefined,
key: 1,
typename: 'First',
depth: 1,
});
});

Expand All @@ -23,7 +22,6 @@ describe('Path', () => {
prev: first,
key: 'two',
typename: 'Second',
depth: 2,
});
});

Expand Down

0 comments on commit 8cdcbac

Please sign in to comment.