Skip to content

Commit

Permalink
stack iterator depth first
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jun 24, 2024
1 parent 006e077 commit b102391
Showing 1 changed file with 19 additions and 42 deletions.
61 changes: 19 additions & 42 deletions src/execution/collectFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { typeFromAST } from '../utilities/typeFromAST';

import { getDirectiveValues } from './values';

interface FragmentEntry {
fragment: FragmentDefinitionNode;
interface StackEntry {
selectionSet: SelectionSetNode;
runtimeType: GraphQLObjectType;
}

Expand All @@ -42,31 +42,21 @@ export function collectFields(
runtimeType: GraphQLObjectType,
selectionSet: SelectionSetNode,
): Map<string, ReadonlyArray<FieldNode>> {
const foundFragments: Array<FragmentEntry> = [];
const stack: Array<StackEntry> = [{ selectionSet, runtimeType }];
const fields = new Map();
const visited = new Set<string>();
collectFieldsImpl(
schema,
fragments,
variableValues,
runtimeType,
selectionSet,
fields,
visited,
foundFragments,
);

let entry;
while ((entry = foundFragments.pop()) !== undefined) {
while ((entry = stack.shift()) !== undefined) {
collectFieldsImpl(
schema,
fragments,
variableValues,
entry.runtimeType,
entry.fragment.selectionSet,
entry.selectionSet,
fields,
visited,
foundFragments,
stack,
);
}

Expand All @@ -91,34 +81,25 @@ export function collectSubfields(
fieldNodes: ReadonlyArray<FieldNode>,
): Map<string, ReadonlyArray<FieldNode>> {
const subFieldNodes = new Map();
const foundFragments: Array<FragmentEntry> = [];
const stack: Array<StackEntry> = [];
const visitedFragmentNames = new Set<string>();
for (const node of fieldNodes) {
if (node.selectionSet) {
collectFieldsImpl(
schema,
fragments,
variableValues,
returnType,
node.selectionSet,
subFieldNodes,
visitedFragmentNames,
foundFragments,
);
stack.push({ selectionSet: node.selectionSet, runtimeType: returnType });
}
}

let entry;
while ((entry = foundFragments.pop()) !== undefined) {
while ((entry = stack.shift()) !== undefined) {
collectFieldsImpl(
schema,
fragments,
variableValues,
entry.runtimeType,
entry.fragment.selectionSet,
entry.selectionSet,
subFieldNodes,
visitedFragmentNames,
foundFragments,
stack,
);
}

Expand All @@ -133,8 +114,9 @@ function collectFieldsImpl(
selectionSet: SelectionSetNode,
fields: Map<string, Array<FieldNode>>,
visitedFragmentNames: Set<string>,
foundFragments: Array<FragmentEntry>,
stack: Array<StackEntry>,
): void {
const discovered = [];
for (const selection of selectionSet.selections) {
switch (selection.kind) {
case Kind.FIELD: {
Expand All @@ -157,16 +139,7 @@ function collectFieldsImpl(
) {
continue;
}
collectFieldsImpl(
schema,
fragments,
variableValues,
runtimeType,
selection.selectionSet,
fields,
visitedFragmentNames,
foundFragments,
);
discovered.push({ selectionSet: selection.selectionSet, runtimeType });
break;
}
case Kind.FRAGMENT_SPREAD: {
Expand All @@ -186,11 +159,15 @@ function collectFieldsImpl(
continue;
}

foundFragments.push({ runtimeType, fragment });
discovered.push({ selectionSet: fragment.selectionSet, runtimeType });
break;
}
}
}

if (discovered.length !== 0) {
stack.unshift(...discovered);
}
}

/**
Expand Down

0 comments on commit b102391

Please sign in to comment.