Skip to content

Commit 1d5253e

Browse files
committed
perf: optimize buildResolveInfo
1 parent 6022932 commit 1d5253e

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/execution/execute.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { isAsyncIterable } from '../jsutils/isAsyncIterable.js';
44
import { isIterableObject } from '../jsutils/isIterableObject.js';
55
import { isObjectLike } from '../jsutils/isObjectLike.js';
66
import { isPromise } from '../jsutils/isPromise.js';
7-
import { mapValue } from '../jsutils/mapValue.js';
87
import type { Maybe } from '../jsutils/Maybe.js';
98
import { memoize3 } from '../jsutils/memoize3.js';
109
import type { ObjMap } from '../jsutils/ObjMap.js';
@@ -21,6 +20,7 @@ import { locatedError } from '../error/locatedError.js';
2120
import type {
2221
DocumentNode,
2322
FieldNode,
23+
FragmentDefinitionNode,
2424
OperationDefinitionNode,
2525
} from '../language/ast.js';
2626
import { OperationTypeNode } from '../language/ast.js';
@@ -119,6 +119,10 @@ const collectSubfields = memoize3(
119119
*/
120120
export interface ValidatedExecutionArgs {
121121
schema: GraphQLSchema;
122+
// TODO: consider deprecating/removing fragmentDefinitions if/when fragment
123+
// arguments are officially supported and/or the full fragment details are
124+
// exposed within GraphQLResolveInfo.
125+
fragmentDefinitions: ObjMap<FragmentDefinitionNode>;
122126
fragments: ObjMap<FragmentDetails>;
123127
rootValue: unknown;
124128
contextValue: unknown;
@@ -329,6 +333,8 @@ export function validateExecutionArgs(
329333
assertValidSchema(schema);
330334

331335
let operation: OperationDefinitionNode | undefined;
336+
const fragmentDefinitions: ObjMap<FragmentDefinitionNode> =
337+
Object.create(null);
332338
const fragments: ObjMap<FragmentDetails> = Object.create(null);
333339
for (const definition of document.definitions) {
334340
switch (definition.kind) {
@@ -347,6 +353,7 @@ export function validateExecutionArgs(
347353
}
348354
break;
349355
case Kind.FRAGMENT_DEFINITION: {
356+
fragmentDefinitions[definition.name.value] = definition;
350357
let variableSignatures;
351358
if (definition.variableDefinitions) {
352359
variableSignatures = Object.create(null);
@@ -387,6 +394,7 @@ export function validateExecutionArgs(
387394

388395
return {
389396
schema,
397+
fragmentDefinitions,
390398
fragments,
391399
rootValue,
392400
contextValue,
@@ -629,7 +637,7 @@ export function buildResolveInfo(
629637
parentType: GraphQLObjectType,
630638
path: Path,
631639
): GraphQLResolveInfo {
632-
const { schema, fragments, rootValue, operation, variableValues } =
640+
const { schema, fragmentDefinitions, rootValue, operation, variableValues } =
633641
validatedExecutionArgs;
634642
// The resolve function's optional fourth argument is a collection of
635643
// information about the current execution state.
@@ -640,10 +648,7 @@ export function buildResolveInfo(
640648
parentType,
641649
path,
642650
schema,
643-
fragments: mapValue(
644-
fragments,
645-
(fragmentDetails) => fragmentDetails.definition,
646-
),
651+
fragments: fragmentDefinitions,
647652
rootValue,
648653
operation,
649654
variableValues,

0 commit comments

Comments
 (0)