Skip to content

Commit 4e610b7

Browse files
committed
[Validation] Unwrap recursion for collecting fragment spreads
1 parent ef7c755 commit 4e610b7

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/validation/validate.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,18 @@ export class ValidationContext {
230230
let spreads = this._fragmentSpreads.get(node);
231231
if (!spreads) {
232232
spreads = [];
233-
gatherSpreads(spreads, node.selectionSet);
233+
const setsToVisit: Array<SelectionSet> = [ node.selectionSet ];
234+
while (setsToVisit.length !== 0) {
235+
const set = setsToVisit.pop();
236+
for (let i = 0; i < set.selections.length; i++) {
237+
const selection = set.selections[i];
238+
if (selection.kind === Kind.FRAGMENT_SPREAD) {
239+
spreads.push(selection);
240+
} else if (selection.selectionSet) {
241+
setsToVisit.push(selection.selectionSet);
242+
}
243+
}
244+
}
234245
this._fragmentSpreads.set(node, spreads);
235246
}
236247
return spreads;
@@ -329,17 +340,3 @@ export class ValidationContext {
329340
return this._typeInfo.getArgument();
330341
}
331342
}
332-
333-
/**
334-
* Given a selection set, gather all the named spreads defined within.
335-
*/
336-
function gatherSpreads(spreads: Array<FragmentSpread>, node: SelectionSet) {
337-
for (let i = 0; i < node.selections.length; i++) {
338-
const selection = node.selections[i];
339-
if (selection.kind === Kind.FRAGMENT_SPREAD) {
340-
spreads.push(selection);
341-
} else if (selection.selectionSet) {
342-
gatherSpreads(spreads, selection.selectionSet);
343-
}
344-
}
345-
}

0 commit comments

Comments
 (0)