Skip to content

Commit

Permalink
fix in execute as well
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jun 19, 2024
1 parent d9c4bd3 commit 4407b55
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
3 changes: 0 additions & 3 deletions src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { GraphQLBoolean, GraphQLInt, GraphQLString } from '../../type/scalars';
import { GraphQLSchema } from '../../type/schema';

import { execute, executeSync } from '../execute';
import { print } from '../../language';

describe('Execute: Handles basic execution tasks', () => {
it('throws if no document is provided', () => {
Expand Down Expand Up @@ -81,7 +80,6 @@ describe('Execute: Handles basic execution tasks', () => {
}),
});


const n = 10000;
const fragments = Array.from(Array(n).keys()).reduce(
(acc, next) =>
Expand All @@ -96,7 +94,6 @@ describe('Execute: Handles basic execution tasks', () => {
const document = parse(`
query {
...X${n}
__typename
}
${fragments}
fragment X0 on Query {
Expand Down
55 changes: 45 additions & 10 deletions src/execution/collectFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import { typeFromAST } from '../utilities/typeFromAST';

import { getDirectiveValues } from './values';

interface FragmentEntry {
fragment: FragmentDefinitionNode;
runtimeType: GraphQLObjectType;
};

/**
* Given a selectionSet, collects all of the fields and returns them.
*
Expand All @@ -37,16 +42,34 @@ export function collectFields(
runtimeType: GraphQLObjectType,
selectionSet: SelectionSetNode,
): Map<string, ReadonlyArray<FieldNode>> {
const foundFragments: Array<FragmentEntry> = [];
const fields = new Map();
const visited = new Set<string>();
collectFieldsImpl(
schema,
fragments,
variableValues,
runtimeType,
selectionSet,
fields,
new Set(),
visited,
foundFragments,
);

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

return fields;
}

Expand All @@ -68,6 +91,7 @@ export function collectSubfields(
fieldNodes: ReadonlyArray<FieldNode>,
): Map<string, ReadonlyArray<FieldNode>> {
const subFieldNodes = new Map();
const foundFragments: Array<FragmentEntry> = [];
const visitedFragmentNames = new Set<string>();
for (const node of fieldNodes) {
if (node.selectionSet) {
Expand All @@ -79,9 +103,25 @@ export function collectSubfields(
node.selectionSet,
subFieldNodes,
visitedFragmentNames,
foundFragments,
);
}
}

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

return subFieldNodes;
}

Expand All @@ -93,6 +133,7 @@ function collectFieldsImpl(
selectionSet: SelectionSetNode,
fields: Map<string, Array<FieldNode>>,
visitedFragmentNames: Set<string>,
foundFragments: Array<FragmentEntry>,
): void {
for (const selection of selectionSet.selections) {
switch (selection.kind) {
Expand Down Expand Up @@ -124,6 +165,7 @@ function collectFieldsImpl(
selection.selectionSet,
fields,
visitedFragmentNames,
foundFragments,
);
break;
}
Expand All @@ -143,15 +185,8 @@ function collectFieldsImpl(
) {
continue;
}
collectFieldsImpl(
schema,
fragments,
variableValues,
runtimeType,
fragment.selectionSet,
fields,
visitedFragmentNames,
);

foundFragments.push({ runtimeType, fragment });
break;
}
}
Expand Down

0 comments on commit 4407b55

Please sign in to comment.