Skip to content

Commit 0d164ab

Browse files
committed
do not modify Path interface
1 parent e9e0031 commit 0d164ab

File tree

6 files changed

+22
-33
lines changed

6 files changed

+22
-33
lines changed

src/execution/IncrementalGraph.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,13 @@ class DeferredFragmentFactory {
7373
if (depth === 0) {
7474
return;
7575
}
76+
const stack: Array<Path> = [];
7677
let currentPath = path;
77-
invariant(currentPath !== undefined);
78-
let currentDepth = currentPath.depth;
79-
while (currentDepth > depth) {
78+
while (currentPath !== undefined) {
79+
stack.unshift(currentPath);
8080
currentPath = currentPath.prev;
81-
invariant(currentPath !== undefined);
82-
currentDepth = currentPath.depth;
8381
}
84-
return currentPath;
82+
return stack[depth - 1];
8583
}
8684
}
8785

src/execution/__tests__/executor-test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ describe('Execute: Handles basic execution tasks', () => {
239239
const field = operation.selectionSet.selections[0];
240240
expect(resolvedInfo).to.deep.include({
241241
fieldNodes: [field],
242-
path: { prev: undefined, key: 'result', typename: 'Test', depth: 1 },
242+
path: { prev: undefined, key: 'result', typename: 'Test' },
243243
variableValues: { var: 'abc' },
244244
});
245245
});
@@ -291,15 +291,12 @@ describe('Execute: Handles basic execution tasks', () => {
291291
expect(path).to.deep.equal({
292292
key: 'l2',
293293
typename: 'SomeObject',
294-
depth: 3,
295294
prev: {
296295
key: 0,
297296
typename: undefined,
298-
depth: 2,
299297
prev: {
300298
key: 'l1',
301299
typename: 'SomeQuery',
302-
depth: 1,
303300
prev: undefined,
304301
},
305302
},

src/execution/collectFields.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { AccumulatorMap } from '../jsutils/AccumulatorMap.js';
22
import { invariant } from '../jsutils/invariant.js';
33
import type { ObjMap } from '../jsutils/ObjMap.js';
4+
import type { Path } from '../jsutils/Path.js';
5+
import { pathToArray } from '../jsutils/Path.js';
46

57
import type {
68
FieldNode,
@@ -100,7 +102,7 @@ export function collectSubfields(
100102
operation: OperationDefinitionNode,
101103
returnType: GraphQLObjectType,
102104
fieldGroup: FieldGroup,
103-
depth: number,
105+
path: Path,
104106
): GroupedFieldSet {
105107
const context: CollectFieldsContext = {
106108
schema,
@@ -119,8 +121,8 @@ export function collectSubfields(
119121
context,
120122
node.selectionSet,
121123
subGroupedFieldSet,
124+
path,
122125
deferUsage,
123-
depth,
124126
);
125127
}
126128
}
@@ -134,8 +136,8 @@ function collectFieldsImpl(
134136
groupedFieldSet: AccumulatorMap<string, FieldDetails> & {
135137
encounteredDefer?: boolean;
136138
},
139+
path?: Path,
137140
deferUsage?: DeferUsage,
138-
depth = 0,
139141
): void {
140142
const {
141143
schema,
@@ -170,26 +172,26 @@ function collectFieldsImpl(
170172
operation,
171173
variableValues,
172174
selection,
175+
path,
173176
deferUsage,
174-
depth,
175177
);
176178

177179
if (!newDeferUsage) {
178180
collectFieldsImpl(
179181
context,
180182
selection.selectionSet,
181183
groupedFieldSet,
184+
path,
182185
deferUsage,
183-
depth,
184186
);
185187
} else {
186188
groupedFieldSet.encounteredDefer = true;
187189
collectFieldsImpl(
188190
context,
189191
selection.selectionSet,
190192
groupedFieldSet,
193+
path,
191194
newDeferUsage,
192-
depth,
193195
);
194196
}
195197

@@ -202,8 +204,8 @@ function collectFieldsImpl(
202204
operation,
203205
variableValues,
204206
selection,
207+
path,
205208
deferUsage,
206-
depth,
207209
);
208210

209211
if (
@@ -227,17 +229,17 @@ function collectFieldsImpl(
227229
context,
228230
fragment.selectionSet,
229231
groupedFieldSet,
232+
path,
230233
deferUsage,
231-
depth,
232234
);
233235
} else {
234236
groupedFieldSet.encounteredDefer = true;
235237
collectFieldsImpl(
236238
context,
237239
fragment.selectionSet,
238240
groupedFieldSet,
241+
path,
239242
newDeferUsage,
240-
depth,
241243
);
242244
}
243245
break;
@@ -255,8 +257,8 @@ function getDeferUsage(
255257
operation: OperationDefinitionNode,
256258
variableValues: { [variable: string]: unknown },
257259
node: FragmentSpreadNode | InlineFragmentNode,
260+
path: Path | undefined,
258261
parentDeferUsage: DeferUsage | undefined,
259-
depth: number,
260262
): DeferUsage | undefined {
261263
const defer = getDirectiveValues(GraphQLDeferDirective, node, variableValues);
262264

@@ -276,7 +278,7 @@ function getDeferUsage(
276278
return {
277279
label: typeof defer.label === 'string' ? defer.label : undefined,
278280
parentDeferUsage,
279-
depth,
281+
depth: pathToArray(path).length,
280282
};
281283
}
282284

src/execution/execute.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const collectSubfields = memoize3(
8888
exeContext: ExecutionContext,
8989
returnType: GraphQLObjectType,
9090
fieldGroup: FieldGroup,
91-
depth: number,
91+
path: Path,
9292
) =>
9393
_collectSubfields(
9494
exeContext.schema,
@@ -97,7 +97,7 @@ const collectSubfields = memoize3(
9797
exeContext.operation,
9898
returnType,
9999
fieldGroup,
100-
depth,
100+
path,
101101
),
102102
);
103103

@@ -1630,7 +1630,7 @@ function collectAndExecuteSubfields(
16301630
exeContext,
16311631
returnType,
16321632
fieldGroup,
1633-
path.depth,
1633+
path,
16341634
);
16351635
if (
16361636
!exeContext.encounteredDefer &&

src/jsutils/Path.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export interface Path {
44
readonly prev: Path | undefined;
55
readonly key: string | number;
66
readonly typename: string | undefined;
7-
readonly depth: number;
87
}
98

109
/**
@@ -15,12 +14,7 @@ export function addPath(
1514
key: string | number,
1615
typename: string | undefined,
1716
): Path {
18-
return {
19-
prev,
20-
key,
21-
typename,
22-
depth: prev === undefined ? 1 : prev.depth + 1,
23-
};
17+
return { prev, key, typename };
2418
}
2519

2620
/**

src/jsutils/__tests__/Path-test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ describe('Path', () => {
1111
prev: undefined,
1212
key: 1,
1313
typename: 'First',
14-
depth: 1,
1514
});
1615
});
1716

@@ -23,7 +22,6 @@ describe('Path', () => {
2322
prev: first,
2423
key: 'two',
2524
typename: 'Second',
26-
depth: 2,
2725
});
2826
});
2927

0 commit comments

Comments
 (0)