Skip to content

Commit b102391

Browse files
committed
stack iterator depth first
1 parent 006e077 commit b102391

File tree

1 file changed

+19
-42
lines changed

1 file changed

+19
-42
lines changed

src/execution/collectFields.ts

+19-42
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import { typeFromAST } from '../utilities/typeFromAST';
2121

2222
import { getDirectiveValues } from './values';
2323

24-
interface FragmentEntry {
25-
fragment: FragmentDefinitionNode;
24+
interface StackEntry {
25+
selectionSet: SelectionSetNode;
2626
runtimeType: GraphQLObjectType;
2727
}
2828

@@ -42,31 +42,21 @@ export function collectFields(
4242
runtimeType: GraphQLObjectType,
4343
selectionSet: SelectionSetNode,
4444
): Map<string, ReadonlyArray<FieldNode>> {
45-
const foundFragments: Array<FragmentEntry> = [];
45+
const stack: Array<StackEntry> = [{ selectionSet, runtimeType }];
4646
const fields = new Map();
4747
const visited = new Set<string>();
48-
collectFieldsImpl(
49-
schema,
50-
fragments,
51-
variableValues,
52-
runtimeType,
53-
selectionSet,
54-
fields,
55-
visited,
56-
foundFragments,
57-
);
5848

5949
let entry;
60-
while ((entry = foundFragments.pop()) !== undefined) {
50+
while ((entry = stack.shift()) !== undefined) {
6151
collectFieldsImpl(
6252
schema,
6353
fragments,
6454
variableValues,
6555
entry.runtimeType,
66-
entry.fragment.selectionSet,
56+
entry.selectionSet,
6757
fields,
6858
visited,
69-
foundFragments,
59+
stack,
7060
);
7161
}
7262

@@ -91,34 +81,25 @@ export function collectSubfields(
9181
fieldNodes: ReadonlyArray<FieldNode>,
9282
): Map<string, ReadonlyArray<FieldNode>> {
9383
const subFieldNodes = new Map();
94-
const foundFragments: Array<FragmentEntry> = [];
84+
const stack: Array<StackEntry> = [];
9585
const visitedFragmentNames = new Set<string>();
9686
for (const node of fieldNodes) {
9787
if (node.selectionSet) {
98-
collectFieldsImpl(
99-
schema,
100-
fragments,
101-
variableValues,
102-
returnType,
103-
node.selectionSet,
104-
subFieldNodes,
105-
visitedFragmentNames,
106-
foundFragments,
107-
);
88+
stack.push({ selectionSet: node.selectionSet, runtimeType: returnType });
10889
}
10990
}
11091

11192
let entry;
112-
while ((entry = foundFragments.pop()) !== undefined) {
93+
while ((entry = stack.shift()) !== undefined) {
11394
collectFieldsImpl(
11495
schema,
11596
fragments,
11697
variableValues,
11798
entry.runtimeType,
118-
entry.fragment.selectionSet,
99+
entry.selectionSet,
119100
subFieldNodes,
120101
visitedFragmentNames,
121-
foundFragments,
102+
stack,
122103
);
123104
}
124105

@@ -133,8 +114,9 @@ function collectFieldsImpl(
133114
selectionSet: SelectionSetNode,
134115
fields: Map<string, Array<FieldNode>>,
135116
visitedFragmentNames: Set<string>,
136-
foundFragments: Array<FragmentEntry>,
117+
stack: Array<StackEntry>,
137118
): void {
119+
const discovered = [];
138120
for (const selection of selectionSet.selections) {
139121
switch (selection.kind) {
140122
case Kind.FIELD: {
@@ -157,16 +139,7 @@ function collectFieldsImpl(
157139
) {
158140
continue;
159141
}
160-
collectFieldsImpl(
161-
schema,
162-
fragments,
163-
variableValues,
164-
runtimeType,
165-
selection.selectionSet,
166-
fields,
167-
visitedFragmentNames,
168-
foundFragments,
169-
);
142+
discovered.push({ selectionSet: selection.selectionSet, runtimeType });
170143
break;
171144
}
172145
case Kind.FRAGMENT_SPREAD: {
@@ -186,11 +159,15 @@ function collectFieldsImpl(
186159
continue;
187160
}
188161

189-
foundFragments.push({ runtimeType, fragment });
162+
discovered.push({ selectionSet: fragment.selectionSet, runtimeType });
190163
break;
191164
}
192165
}
193166
}
167+
168+
if (discovered.length !== 0) {
169+
stack.unshift(...discovered);
170+
}
194171
}
195172

196173
/**

0 commit comments

Comments
 (0)