Skip to content

Commit

Permalink
add memoization around fieldNodes (#130)
Browse files Browse the repository at this point in the history
now that executor uses a cache when collecting fieldNodes, we can benefit from even further memoization
  • Loading branch information
yaacovCR authored Jan 14, 2022
1 parent 672a743 commit 6bb154a
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/execution/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
import type { Maybe } from '../jsutils/Maybe';
import type { Push, Stop } from '../jsutils/repeater';
import { inspect } from '../jsutils/inspect';
import { memoize2 } from '../jsutils/memoize2';
import { memoize3 } from '../jsutils/memoize3';
import { invariant } from '../jsutils/invariant';
import { devAssert } from '../jsutils/devAssert';
Expand Down Expand Up @@ -248,6 +249,15 @@ export class Executor {
) => _getArgumentValues(this._executorSchema, def, node, variableValues),
);

/**
* A memoized method that looks up the field given a parent type
* and an array of field nodes.
*/
getFieldDef = memoize2(
(parentType: GraphQLObjectType, fieldNodes: ReadonlyArray<FieldNode>) =>
this._getFieldDef(parentType, fieldNodes),
);

private _schema: GraphQLSchema;
private _executorSchema: ExecutorSchema;

Expand Down Expand Up @@ -832,7 +842,7 @@ export class Executor {
path: Path,
payloadContext: PayloadContext,
): PromiseOrValue<unknown> {
const fieldDef = this.getFieldDef(parentType, fieldNodes[0]);
const fieldDef = this.getFieldDef(parentType, fieldNodes);
if (!fieldDef) {
return;
}
Expand Down Expand Up @@ -1619,11 +1629,11 @@ export class Executor {
* require mutating type definitions, which would cause issues.
*
*/
getFieldDef(
_getFieldDef(
parentType: GraphQLObjectType,
fieldNode: FieldNode,
fieldNodes: ReadonlyArray<FieldNode>,
): Maybe<GraphQLField<unknown, unknown>> {
const fieldName = fieldNode.name.value;
const fieldName = fieldNodes[0].name.value;

if (
fieldName === SchemaMetaFieldDef.name &&
Expand Down Expand Up @@ -1742,7 +1752,7 @@ export class Executor {
);

const [responseName, fieldNodes] = [...fields.entries()][0];
const fieldDef = this.getFieldDef(rootType, fieldNodes[0]);
const fieldDef = this.getFieldDef(rootType, fieldNodes);

if (!fieldDef) {
const fieldName = fieldNodes[0].name.value;
Expand Down

0 comments on commit 6bb154a

Please sign in to comment.