@@ -325,25 +325,51 @@ export class Executor {
325
325
this . _collectSubfields ( returnType , fieldNodes ) ,
326
326
) ;
327
327
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 ;
332
358
}
333
359
334
360
/**
335
361
* Implements the "Executing operations" section of the spec.
336
362
*/
337
363
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 ) ;
341
367
const fields = collectFields (
342
- schema ,
343
- fragments ,
344
- variableValues ,
368
+ _schema ,
369
+ _fragments ,
370
+ _variableValues ,
345
371
type ,
346
- operation . selectionSet ,
372
+ _operation . selectionSet ,
347
373
new Map ( ) ,
348
374
new Set ( ) ,
349
375
) ;
@@ -355,18 +381,18 @@ export class Executor {
355
381
// in this case is the entire response.
356
382
try {
357
383
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 ) ;
361
387
if ( isPromise ( result ) ) {
362
388
return result . then ( undefined , ( error ) => {
363
- errors . push ( error ) ;
389
+ this . _errors . push ( error ) ;
364
390
return Promise . resolve ( null ) ;
365
391
} ) ;
366
392
}
367
393
return result ;
368
394
} catch ( error ) {
369
- errors . push ( error ) ;
395
+ this . _errors . push ( error ) ;
370
396
return null ;
371
397
}
372
398
}
@@ -381,8 +407,9 @@ export class Executor {
381
407
if ( isPromise ( data ) ) {
382
408
return data . then ( ( resolved ) => this . buildResponse ( resolved ) ) ;
383
409
}
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 } ;
386
413
}
387
414
388
415
/**
@@ -474,16 +501,15 @@ export class Executor {
474
501
fieldNodes : ReadonlyArray < FieldNode > ,
475
502
path : Path ,
476
503
) : PromiseOrValue < unknown > {
477
- const { schema, contextValue, variableValues, fieldResolver } =
478
- this . _exeContext ;
504
+ const { _schema, _contextValue, _variableValues, _fieldResolver } = this ;
479
505
480
- const fieldDef = getFieldDef ( schema , parentType , fieldNodes [ 0 ] ) ;
506
+ const fieldDef = getFieldDef ( _schema , parentType , fieldNodes [ 0 ] ) ;
481
507
if ( ! fieldDef ) {
482
508
return ;
483
509
}
484
510
485
511
const returnType = fieldDef . type ;
486
- const resolveFn = fieldDef . resolve ?? fieldResolver ;
512
+ const resolveFn = fieldDef . resolve ?? _fieldResolver ;
487
513
488
514
const info = this . buildResolveInfo ( fieldDef , fieldNodes , parentType , path ) ;
489
515
@@ -492,12 +518,12 @@ export class Executor {
492
518
// Build a JS object of arguments from the field.arguments AST, using the
493
519
// variables scope to fulfill any variable references.
494
520
// 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 ) ;
496
522
497
523
// The resolve function's optional third argument is a context value that
498
524
// is provided to every resolve function within an execution. It is commonly
499
525
// 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 ) ;
501
527
502
528
let completed ;
503
529
if ( isPromise ( result ) ) {
@@ -538,8 +564,8 @@ export class Executor {
538
564
parentType : GraphQLObjectType ,
539
565
path : Path ,
540
566
) : GraphQLResolveInfo {
541
- const { schema , fragments , rootValue , operation , variableValues } =
542
- this . _exeContext ;
567
+ const { _schema , _fragments , _rootValue , _operation , _variableValues } =
568
+ this ;
543
569
544
570
// The resolve function's optional fourth argument is a collection of
545
571
// information about the current execution state.
@@ -549,11 +575,11 @@ export class Executor {
549
575
returnType : fieldDef . type ,
550
576
parentType,
551
577
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 ,
557
583
} ;
558
584
}
559
585
@@ -566,7 +592,7 @@ export class Executor {
566
592
567
593
// Otherwise, error protection is applied, logging the error and resolving
568
594
// a null value for this field if one is encountered.
569
- this . _exeContext . errors . push ( error ) ;
595
+ this . _errors . push ( error ) ;
570
596
return null ;
571
597
}
572
598
@@ -758,10 +784,10 @@ export class Executor {
758
784
path : Path ,
759
785
result : unknown ,
760
786
) : PromiseOrValue < ObjMap < unknown > > {
761
- const { contextValue , typeResolver } = this . _exeContext ;
787
+ const { _contextValue , _typeResolver } = this ;
762
788
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 ) ;
765
791
766
792
if ( isPromise ( runtimeType ) ) {
767
793
return runtimeType . then ( ( resolvedRuntimeType ) =>
@@ -825,8 +851,7 @@ export class Executor {
825
851
) ;
826
852
}
827
853
828
- const { schema } = this . _exeContext ;
829
- const runtimeType = schema . getType ( runtimeTypeName ) ;
854
+ const runtimeType = this . _schema . getType ( runtimeTypeName ) ;
830
855
if ( runtimeType == null ) {
831
856
throw new GraphQLError (
832
857
`Abstract type "${ returnType . name } " was resolved to a type "${ runtimeTypeName } " that does not exist inside the schema.` ,
@@ -841,7 +866,7 @@ export class Executor {
841
866
) ;
842
867
}
843
868
844
- if ( ! schema . isSubType ( returnType , runtimeType ) ) {
869
+ if ( ! this . _schema . isSubType ( returnType , runtimeType ) ) {
845
870
throw new GraphQLError (
846
871
`Runtime Object type "${ runtimeType . name } " is not a possible type for "${ returnType . name } ".` ,
847
872
fieldNodes ,
@@ -868,11 +893,7 @@ export class Executor {
868
893
// current result. If isTypeOf returns false, then raise an error rather
869
894
// than continuing execution.
870
895
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 ) ;
876
897
877
898
if ( isPromise ( isTypeOf ) ) {
878
899
return isTypeOf . then ( ( resolvedIsTypeOf ) => {
@@ -899,16 +920,16 @@ export class Executor {
899
920
returnType : GraphQLObjectType ,
900
921
fieldNodes : ReadonlyArray < FieldNode > ,
901
922
) : Map < string , ReadonlyArray < FieldNode > > {
902
- const { schema , fragments , variableValues } = this . _exeContext ;
923
+ const { _schema , _fragments , _variableValues } = this ;
903
924
904
925
let subFieldNodes = new Map ( ) ;
905
926
const visitedFragmentNames = new Set < string > ( ) ;
906
927
for ( const node of fieldNodes ) {
907
928
if ( node . selectionSet ) {
908
929
subFieldNodes = collectFields (
909
- schema ,
910
- fragments ,
911
- variableValues ,
930
+ _schema ,
931
+ _fragments ,
932
+ _variableValues ,
912
933
returnType ,
913
934
node . selectionSet ,
914
935
subFieldNodes ,
0 commit comments