@@ -24,9 +24,10 @@ import {
24
24
isAbstractType
25
25
} from '../type/definition' ;
26
26
import type {
27
- GraphQLFieldDefinition ,
28
27
GraphQLType ,
29
- GraphQLAbstractType
28
+ GraphQLAbstractType ,
29
+ GraphQLFieldDefinition ,
30
+ GraphQLResolveInfo ,
30
31
} from '../type/definition' ;
31
32
import type { GraphQLSchema } from '../type/schema' ;
32
33
import {
@@ -482,7 +483,7 @@ function resolveField(
482
483
483
484
// The resolve function's optional third argument is a collection of
484
485
// information about the current execution state.
485
- var info = {
486
+ var info : GraphQLResolveInfo = {
486
487
fieldName,
487
488
fieldASTs,
488
489
returnType,
@@ -509,25 +510,38 @@ function resolveField(
509
510
return null ;
510
511
}
511
512
512
- return completeValueCatchingError ( exeContext , returnType , fieldASTs , result ) ;
513
+ return completeValueCatchingError (
514
+ exeContext ,
515
+ returnType ,
516
+ fieldASTs ,
517
+ info ,
518
+ result
519
+ ) ;
513
520
}
514
521
515
522
function completeValueCatchingError (
516
523
exeContext : ExecutionContext ,
517
- fieldType : GraphQLType ,
524
+ returnType : GraphQLType ,
518
525
fieldASTs : Array < Field > ,
526
+ info : GraphQLResolveInfo ,
519
527
result : any
520
528
) : any {
521
529
// If the field type is non-nullable, then it is resolved without any
522
530
// protection from errors.
523
- if ( fieldType instanceof GraphQLNonNull ) {
524
- return completeValue ( exeContext , fieldType , fieldASTs , result ) ;
531
+ if ( returnType instanceof GraphQLNonNull ) {
532
+ return completeValue ( exeContext , returnType , fieldASTs , info , result ) ;
525
533
}
526
534
527
535
// Otherwise, error protection is applied, logging the error and resolving
528
536
// a null value for this field if one is encountered.
529
537
try {
530
- var completed = completeValue ( exeContext , fieldType , fieldASTs , result ) ;
538
+ var completed = completeValue (
539
+ exeContext ,
540
+ returnType ,
541
+ fieldASTs ,
542
+ info ,
543
+ result
544
+ ) ;
531
545
if ( isThenable ( completed ) ) {
532
546
// Note: we don't rely on a `catch` method, but we do expect "thenable"
533
547
// to take a second callback for the error case.
@@ -563,26 +577,34 @@ function completeValueCatchingError(
563
577
*/
564
578
function completeValue (
565
579
exeContext : ExecutionContext ,
566
- fieldType : GraphQLType ,
580
+ returnType : GraphQLType ,
567
581
fieldASTs : Array < Field > ,
582
+ info : GraphQLResolveInfo ,
568
583
result : any
569
584
) : any {
570
585
// If result is a Promise, resolve it, if the Promise is rejected, construct
571
586
// a GraphQLError with proper locations.
572
587
if ( isThenable ( result ) ) {
573
588
return result . then (
574
- resolved => completeValue ( exeContext , fieldType , fieldASTs , resolved ) ,
589
+ resolved => completeValue (
590
+ exeContext ,
591
+ returnType ,
592
+ fieldASTs ,
593
+ info ,
594
+ resolved
595
+ ) ,
575
596
error => Promise . reject ( locatedError ( error , fieldASTs ) )
576
597
) ;
577
598
}
578
599
579
600
// If field type is NonNull, complete for inner type, and throw field error
580
601
// if result is null.
581
- if ( fieldType instanceof GraphQLNonNull ) {
602
+ if ( returnType instanceof GraphQLNonNull ) {
582
603
var completed = completeValue (
583
604
exeContext ,
584
- fieldType . ofType ,
605
+ returnType . ofType ,
585
606
fieldASTs ,
607
+ info ,
586
608
result
587
609
) ;
588
610
if ( completed === null ) {
@@ -600,19 +622,19 @@ function completeValue(
600
622
}
601
623
602
624
// If field type is List, complete each item in the list with the inner type
603
- if ( fieldType instanceof GraphQLList ) {
625
+ if ( returnType instanceof GraphQLList ) {
604
626
invariant (
605
627
Array . isArray ( result ) ,
606
628
'User Error: expected iterable, but did not find one.'
607
629
) ;
608
630
609
631
// This is specified as a simple map, however we're optimizing the path
610
632
// where the list contains no Promises by avoiding creating another Promise.
611
- var itemType = fieldType . ofType ;
633
+ var itemType = returnType . ofType ;
612
634
var containsPromise = false ;
613
635
var completedResults = result . map ( item => {
614
636
var completedItem =
615
- completeValueCatchingError ( exeContext , itemType , fieldASTs , item ) ;
637
+ completeValueCatchingError ( exeContext , itemType , fieldASTs , info , item ) ;
616
638
if ( ! containsPromise && isThenable ( completedItem ) ) {
617
639
containsPromise = true ;
618
640
}
@@ -624,20 +646,19 @@ function completeValue(
624
646
625
647
// If field type is Scalar or Enum, serialize to a valid value, returning
626
648
// null if serialization is not possible.
627
- if ( fieldType instanceof GraphQLScalarType ||
628
- fieldType instanceof GraphQLEnumType ) {
629
- invariant ( fieldType . serialize , 'Missing serialize method on type' ) ;
630
- var serializedResult = fieldType . serialize ( result ) ;
649
+ if ( returnType instanceof GraphQLScalarType ||
650
+ returnType instanceof GraphQLEnumType ) {
651
+ invariant ( returnType . serialize , 'Missing serialize method on type' ) ;
652
+ var serializedResult = returnType . serialize ( result ) ;
631
653
return isNullish ( serializedResult ) ? null : serializedResult ;
632
654
}
633
655
634
656
// Field type must be Object, Interface or Union and expect sub-selections.
635
-
636
657
var objectType : ?GraphQLObjectType =
637
- fieldType instanceof GraphQLObjectType ? fieldType :
638
- isAbstractType ( fieldType ) ?
639
- ( ( fieldType : any ) : GraphQLAbstractType ) . resolveType ( result ) :
640
- null ;
658
+ returnType instanceof GraphQLObjectType ? returnType :
659
+ isAbstractType ( returnType ) ?
660
+ ( ( returnType : any ) : GraphQLAbstractType ) . resolveType ( result , info ) :
661
+ null ;
641
662
642
663
if ( ! objectType ) {
643
664
return null ;
0 commit comments