@@ -461,47 +461,55 @@ function resolveField(
461
461
fieldASTs : Array < Field >
462
462
) : any {
463
463
var fieldAST = fieldASTs [ 0 ] ;
464
+ var fieldName = fieldAST . name . value ;
464
465
465
- var fieldDef = getFieldDef ( exeContext . schema , parentType , fieldAST ) ;
466
+ var fieldDef = getFieldDef ( exeContext . schema , parentType , fieldName ) ;
466
467
if ( ! fieldDef ) {
467
468
return;
468
469
}
469
470
470
- var fieldType = fieldDef . type ;
471
+ var returnType = fieldDef . type ;
471
472
var resolveFn = fieldDef . resolve || defaultResolveFn ;
472
473
473
474
// Build a JS object of arguments from the field.arguments AST, using the
474
475
// variables scope to fulfill any variable references.
475
476
// TODO: find a way to memoize, in case this field is within a List type.
476
- var args = fieldDef . args ?
477
- getArgumentValues ( fieldDef . args , fieldAST . arguments , exeContext . variables ) :
478
- null ;
477
+ var args = getArgumentValues (
478
+ fieldDef . args ,
479
+ fieldAST . arguments ,
480
+ exeContext . variables
481
+ ) ;
482
+
483
+ // The resolve function's optional third argument is a collection of
484
+ // information about the current execution state.
485
+ var info = {
486
+ fieldName,
487
+ fieldASTs,
488
+ returnType,
489
+ parentType,
490
+ schema : exeContext . schema ,
491
+ fragments : exeContext . fragments ,
492
+ rootValue : exeContext . rootValue ,
493
+ operation : exeContext . operation ,
494
+ variables : exeContext . variables ,
495
+ } ;
479
496
480
497
// If an error occurs while calling the field `resolve` function, ensure that
481
498
// it is wrapped as a GraphQLError with locations. Log this error and return
482
499
// null if allowed, otherwise throw the error so the parent field can handle
483
500
// it.
484
501
try {
485
- var result = resolveFn (
486
- source ,
487
- args ,
488
- exeContext . rootValue ,
489
- // TODO: provide all fieldASTs, not just the first field
490
- fieldAST ,
491
- fieldType ,
492
- parentType ,
493
- exeContext . schema
494
- ) ;
502
+ var result = resolveFn ( source , args , info ) ;
495
503
} catch ( error ) {
496
504
var reportedError = locatedError ( error , fieldASTs ) ;
497
- if ( fieldType instanceof GraphQLNonNull ) {
505
+ if ( returnType instanceof GraphQLNonNull ) {
498
506
throw reportedError ;
499
507
}
500
508
exeContext . errors . push ( reportedError ) ;
501
509
return null ;
502
510
}
503
511
504
- return completeValueCatchingError ( exeContext , fieldType , fieldASTs , result ) ;
512
+ return completeValueCatchingError ( exeContext , returnType , fieldASTs , result ) ;
505
513
}
506
514
507
515
function completeValueCatchingError (
@@ -660,8 +668,8 @@ function completeValue(
660
668
* and returns it as the result, or if it's a function, returns the result
661
669
* of calling that function.
662
670
*/
663
- function defaultResolveFn ( source , args , root , fieldAST ) {
664
- var property = source [ fieldAST . name . value ] ;
671
+ function defaultResolveFn ( source , args , { fieldName } ) {
672
+ var property = source [ fieldName ] ;
665
673
return typeof property === 'function' ? property . call ( source ) : property ;
666
674
}
667
675
@@ -685,17 +693,16 @@ function isThenable(value: any): boolean {
685
693
function getFieldDef (
686
694
schema : GraphQLSchema ,
687
695
parentType : GraphQLObjectType ,
688
- fieldAST : Field
696
+ fieldName : string
689
697
) : ?GraphQLFieldDefinition {
690
- var name = fieldAST . name . value ;
691
- if ( name === SchemaMetaFieldDef . name &&
698
+ if ( fieldName === SchemaMetaFieldDef . name &&
692
699
schema . getQueryType ( ) === parentType ) {
693
700
return SchemaMetaFieldDef ;
694
- } else if ( name === TypeMetaFieldDef . name &&
701
+ } else if ( fieldName === TypeMetaFieldDef . name &&
695
702
schema . getQueryType ( ) === parentType ) {
696
703
return TypeMetaFieldDef ;
697
- } else if ( name === TypeNameMetaFieldDef . name ) {
704
+ } else if ( fieldName === TypeNameMetaFieldDef . name ) {
698
705
return TypeNameMetaFieldDef ;
699
706
}
700
- return parentType . getFields ( ) [ name ] ;
707
+ return parentType . getFields ( ) [ fieldName ] ;
701
708
}
0 commit comments