Skip to content

Commit e64852b

Browse files
committed
spread exeContext properties into the class
1 parent 602942f commit e64852b

File tree

1 file changed

+68
-47
lines changed

1 file changed

+68
-47
lines changed

src/execution/execute.ts

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -325,25 +325,51 @@ export class Executor {
325325
this._collectSubfields(returnType, fieldNodes),
326326
);
327327

328-
protected _exeContext: ExecutionContext;
329-
330-
constructor(exeContext: ExecutionContext) {
331-
this._exeContext = exeContext;
328+
protected _schema: GraphQLSchema;
329+
protected _fragments: ObjMap<FragmentDefinitionNode>;
330+
protected _rootValue: unknown;
331+
protected _contextValue: unknown;
332+
protected _operation: OperationDefinitionNode;
333+
protected _variableValues: { [variable: string]: unknown };
334+
protected _fieldResolver: GraphQLFieldResolver<any, any>;
335+
protected _typeResolver: GraphQLTypeResolver<any, any>;
336+
protected _errors: Array<GraphQLError>;
337+
338+
constructor({
339+
schema,
340+
fragments,
341+
rootValue,
342+
contextValue,
343+
operation,
344+
variableValues,
345+
fieldResolver,
346+
typeResolver,
347+
errors,
348+
}: ExecutionContext) {
349+
this._schema = schema;
350+
this._fragments = fragments;
351+
this._rootValue = rootValue;
352+
this._contextValue = contextValue;
353+
this._operation = operation;
354+
this._variableValues = variableValues;
355+
this._fieldResolver = fieldResolver;
356+
this._typeResolver = typeResolver;
357+
this._errors = errors;
332358
}
333359

334360
/**
335361
* Implements the "Executing operations" section of the spec.
336362
*/
337363
executeOperation(): PromiseOrValue<ObjMap<unknown> | null> {
338-
const { schema, fragments, rootValue, operation, variableValues, errors } =
339-
this._exeContext;
340-
const type = getOperationRootType(schema, operation);
364+
const { _schema, _fragments, _rootValue, _operation, _variableValues } =
365+
this;
366+
const type = getOperationRootType(_schema, _operation);
341367
const fields = collectFields(
342-
schema,
343-
fragments,
344-
variableValues,
368+
_schema,
369+
_fragments,
370+
_variableValues,
345371
type,
346-
operation.selectionSet,
372+
_operation.selectionSet,
347373
new Map(),
348374
new Set(),
349375
);
@@ -355,18 +381,18 @@ export class Executor {
355381
// in this case is the entire response.
356382
try {
357383
const result =
358-
operation.operation === 'mutation'
359-
? this.executeFieldsSerially(type, rootValue, path, fields)
360-
: this.executeFields(type, rootValue, path, fields);
384+
_operation.operation === 'mutation'
385+
? this.executeFieldsSerially(type, _rootValue, path, fields)
386+
: this.executeFields(type, _rootValue, path, fields);
361387
if (isPromise(result)) {
362388
return result.then(undefined, (error) => {
363-
errors.push(error);
389+
this._errors.push(error);
364390
return Promise.resolve(null);
365391
});
366392
}
367393
return result;
368394
} catch (error) {
369-
errors.push(error);
395+
this._errors.push(error);
370396
return null;
371397
}
372398
}
@@ -381,8 +407,9 @@ export class Executor {
381407
if (isPromise(data)) {
382408
return data.then((resolved) => this.buildResponse(resolved));
383409
}
384-
const errors = this._exeContext.errors;
385-
return errors.length === 0 ? { data } : { errors, data };
410+
return this._errors.length === 0
411+
? { data }
412+
: { errors: this._errors, data };
386413
}
387414

388415
/**
@@ -474,16 +501,15 @@ export class Executor {
474501
fieldNodes: ReadonlyArray<FieldNode>,
475502
path: Path,
476503
): PromiseOrValue<unknown> {
477-
const { schema, contextValue, variableValues, fieldResolver } =
478-
this._exeContext;
504+
const { _schema, _contextValue, _variableValues, _fieldResolver } = this;
479505

480-
const fieldDef = getFieldDef(schema, parentType, fieldNodes[0]);
506+
const fieldDef = getFieldDef(_schema, parentType, fieldNodes[0]);
481507
if (!fieldDef) {
482508
return;
483509
}
484510

485511
const returnType = fieldDef.type;
486-
const resolveFn = fieldDef.resolve ?? fieldResolver;
512+
const resolveFn = fieldDef.resolve ?? _fieldResolver;
487513

488514
const info = this.buildResolveInfo(fieldDef, fieldNodes, parentType, path);
489515

@@ -492,12 +518,12 @@ export class Executor {
492518
// Build a JS object of arguments from the field.arguments AST, using the
493519
// variables scope to fulfill any variable references.
494520
// TODO: find a way to memoize, in case this field is within a List type.
495-
const args = getArgumentValues(fieldDef, fieldNodes[0], variableValues);
521+
const args = getArgumentValues(fieldDef, fieldNodes[0], _variableValues);
496522

497523
// The resolve function's optional third argument is a context value that
498524
// is provided to every resolve function within an execution. It is commonly
499525
// used to represent an authenticated user, or request-specific caches.
500-
const result = resolveFn(source, args, contextValue, info);
526+
const result = resolveFn(source, args, _contextValue, info);
501527

502528
let completed;
503529
if (isPromise(result)) {
@@ -538,8 +564,8 @@ export class Executor {
538564
parentType: GraphQLObjectType,
539565
path: Path,
540566
): GraphQLResolveInfo {
541-
const { schema, fragments, rootValue, operation, variableValues } =
542-
this._exeContext;
567+
const { _schema, _fragments, _rootValue, _operation, _variableValues } =
568+
this;
543569

544570
// The resolve function's optional fourth argument is a collection of
545571
// information about the current execution state.
@@ -549,11 +575,11 @@ export class Executor {
549575
returnType: fieldDef.type,
550576
parentType,
551577
path,
552-
schema,
553-
fragments,
554-
rootValue,
555-
operation,
556-
variableValues,
578+
schema: _schema,
579+
fragments: _fragments,
580+
rootValue: _rootValue,
581+
operation: _operation,
582+
variableValues: _variableValues,
557583
};
558584
}
559585

@@ -566,7 +592,7 @@ export class Executor {
566592

567593
// Otherwise, error protection is applied, logging the error and resolving
568594
// a null value for this field if one is encountered.
569-
this._exeContext.errors.push(error);
595+
this._errors.push(error);
570596
return null;
571597
}
572598

@@ -758,10 +784,10 @@ export class Executor {
758784
path: Path,
759785
result: unknown,
760786
): PromiseOrValue<ObjMap<unknown>> {
761-
const { contextValue, typeResolver } = this._exeContext;
787+
const { _contextValue, _typeResolver } = this;
762788

763-
const resolveTypeFn = returnType.resolveType ?? typeResolver;
764-
const runtimeType = resolveTypeFn(result, contextValue, info, returnType);
789+
const resolveTypeFn = returnType.resolveType ?? _typeResolver;
790+
const runtimeType = resolveTypeFn(result, _contextValue, info, returnType);
765791

766792
if (isPromise(runtimeType)) {
767793
return runtimeType.then((resolvedRuntimeType) =>
@@ -825,8 +851,7 @@ export class Executor {
825851
);
826852
}
827853

828-
const { schema } = this._exeContext;
829-
const runtimeType = schema.getType(runtimeTypeName);
854+
const runtimeType = this._schema.getType(runtimeTypeName);
830855
if (runtimeType == null) {
831856
throw new GraphQLError(
832857
`Abstract type "${returnType.name}" was resolved to a type "${runtimeTypeName}" that does not exist inside the schema.`,
@@ -841,7 +866,7 @@ export class Executor {
841866
);
842867
}
843868

844-
if (!schema.isSubType(returnType, runtimeType)) {
869+
if (!this._schema.isSubType(returnType, runtimeType)) {
845870
throw new GraphQLError(
846871
`Runtime Object type "${runtimeType.name}" is not a possible type for "${returnType.name}".`,
847872
fieldNodes,
@@ -868,11 +893,7 @@ export class Executor {
868893
// current result. If isTypeOf returns false, then raise an error rather
869894
// than continuing execution.
870895
if (returnType.isTypeOf) {
871-
const isTypeOf = returnType.isTypeOf(
872-
result,
873-
this._exeContext.contextValue,
874-
info,
875-
);
896+
const isTypeOf = returnType.isTypeOf(result, this._contextValue, info);
876897

877898
if (isPromise(isTypeOf)) {
878899
return isTypeOf.then((resolvedIsTypeOf) => {
@@ -899,16 +920,16 @@ export class Executor {
899920
returnType: GraphQLObjectType,
900921
fieldNodes: ReadonlyArray<FieldNode>,
901922
): Map<string, ReadonlyArray<FieldNode>> {
902-
const { schema, fragments, variableValues } = this._exeContext;
923+
const { _schema, _fragments, _variableValues } = this;
903924

904925
let subFieldNodes = new Map();
905926
const visitedFragmentNames = new Set<string>();
906927
for (const node of fieldNodes) {
907928
if (node.selectionSet) {
908929
subFieldNodes = collectFields(
909-
schema,
910-
fragments,
911-
variableValues,
930+
_schema,
931+
_fragments,
932+
_variableValues,
912933
returnType,
913934
node.selectionSet,
914935
subFieldNodes,

0 commit comments

Comments
 (0)