Skip to content

Commit fcb1a1d

Browse files
committed
perf: optimize buildResolveInfo
1 parent e3542b2 commit fcb1a1d

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/execution/execute.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { isAsyncIterable } from '../jsutils/isAsyncIterable.js';
55
import { isIterableObject } from '../jsutils/isIterableObject.js';
66
import { isObjectLike } from '../jsutils/isObjectLike.js';
77
import { isPromise } from '../jsutils/isPromise.js';
8-
import { mapValue } from '../jsutils/mapValue.js';
98
import type { Maybe } from '../jsutils/Maybe.js';
109
import { memoize3 } from '../jsutils/memoize3.js';
1110
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';
@@ -139,6 +139,10 @@ const collectSubfields = memoize3(
139139
*/
140140
export interface ValidatedExecutionArgs {
141141
schema: GraphQLSchema;
142+
// TODO: consider deprecating/removing fragmentDefinitions if/when fragment
143+
// arguments are officially supported and/or the full fragment details are
144+
// exposed within GraphQLResolveInfo.
145+
fragmentDefinitions: ObjMap<FragmentDefinitionNode>;
142146
fragments: ObjMap<FragmentDetails>;
143147
rootValue: unknown;
144148
contextValue: unknown;
@@ -492,6 +496,8 @@ export function validateExecutionArgs(
492496
assertValidSchema(schema);
493497

494498
let operation: OperationDefinitionNode | undefined;
499+
const fragmentDefinitions: ObjMap<FragmentDefinitionNode> =
500+
Object.create(null);
495501
const fragments: ObjMap<FragmentDetails> = Object.create(null);
496502
for (const definition of document.definitions) {
497503
switch (definition.kind) {
@@ -510,6 +516,7 @@ export function validateExecutionArgs(
510516
}
511517
break;
512518
case Kind.FRAGMENT_DEFINITION: {
519+
fragmentDefinitions[definition.name.value] = definition;
513520
let variableSignatures;
514521
if (definition.variableDefinitions) {
515522
variableSignatures = Object.create(null);
@@ -550,6 +557,7 @@ export function validateExecutionArgs(
550557

551558
return {
552559
schema,
560+
fragmentDefinitions,
553561
fragments,
554562
rootValue,
555563
contextValue,
@@ -842,7 +850,7 @@ export function buildResolveInfo(
842850
parentType: GraphQLObjectType,
843851
path: Path,
844852
): GraphQLResolveInfo {
845-
const { schema, fragments, rootValue, operation, variableValues } =
853+
const { schema, fragmentDefinitions, rootValue, operation, variableValues } =
846854
validatedExecutionArgs;
847855
// The resolve function's optional fourth argument is a collection of
848856
// information about the current execution state.
@@ -853,10 +861,7 @@ export function buildResolveInfo(
853861
parentType,
854862
path,
855863
schema,
856-
fragments: mapValue(
857-
fragments,
858-
(fragmentDetails) => fragmentDetails.definition,
859-
),
864+
fragments: fragmentDefinitions,
860865
rootValue,
861866
operation,
862867
variableValues,

0 commit comments

Comments
 (0)