@@ -38,6 +38,7 @@ import type {
38
38
GraphQLTypeResolver ,
39
39
} from '../type/definition.js' ;
40
40
import {
41
+ getNamedType ,
41
42
isAbstractType ,
42
43
isLeafType ,
43
44
isListType ,
@@ -630,19 +631,26 @@ function executeFieldsSerially(
630
631
( results , [ responseName , fieldGroup ] ) => {
631
632
const fieldPath = exeContext . addPath ( path , responseName , parentType . name ) ;
632
633
633
- if ( ! shouldExecute ( fieldGroup ) ) {
634
+ const fieldName = fieldGroup [ 0 ] . fieldNode . name . value ;
635
+ const fieldDef = exeContext . schema . getField ( parentType , fieldName ) ;
636
+ if ( ! fieldDef ) {
637
+ return results ;
638
+ }
639
+
640
+ const returnType = fieldDef . type ;
641
+
642
+ if ( ! shouldExecute ( fieldGroup , returnType ) ) {
634
643
return results ;
635
644
}
636
645
const result = executeField (
637
646
exeContext ,
638
647
parentType ,
648
+ fieldDef ,
649
+ returnType ,
639
650
sourceValue ,
640
651
fieldGroup ,
641
652
fieldPath ,
642
653
) ;
643
- if ( result === undefined ) {
644
- return results ;
645
- }
646
654
if ( isPromise ( result ) ) {
647
655
return result . then ( ( resolvedResult ) => {
648
656
results [ responseName ] = resolvedResult ;
@@ -658,11 +666,25 @@ function executeFieldsSerially(
658
666
659
667
function shouldExecute (
660
668
fieldGroup : FieldGroup ,
669
+ returnType : GraphQLOutputType ,
661
670
deferDepth ?: number | undefined ,
662
671
) : boolean {
663
- return fieldGroup . some (
664
- ( { deferDepth : fieldDeferDepth } ) => fieldDeferDepth === deferDepth ,
665
- ) ;
672
+ if ( deferDepth === undefined || ! isLeafType ( getNamedType ( returnType ) ) ) {
673
+ return fieldGroup . some (
674
+ ( { deferDepth : fieldDeferDepth } ) => fieldDeferDepth === deferDepth ,
675
+ ) ;
676
+ }
677
+
678
+ let hasDepth = false ;
679
+ for ( const { deferDepth : fieldDeferDepth } of fieldGroup ) {
680
+ if ( fieldDeferDepth === undefined ) {
681
+ return false ;
682
+ }
683
+ if ( fieldDeferDepth === deferDepth ) {
684
+ hasDepth = true ;
685
+ }
686
+ }
687
+ return hasDepth ;
666
688
}
667
689
668
690
/**
@@ -684,10 +706,22 @@ function executeFields(
684
706
for ( const [ responseName , fieldGroup ] of groupedFieldSet ) {
685
707
const fieldPath = exeContext . addPath ( path , responseName , parentType . name ) ;
686
708
687
- if ( shouldExecute ( fieldGroup , asyncPayloadRecord ?. deferDepth ) ) {
709
+ const fieldName = fieldGroup [ 0 ] . fieldNode . name . value ;
710
+ const fieldDef = exeContext . schema . getField ( parentType , fieldName ) ;
711
+ if ( ! fieldDef ) {
712
+ continue ;
713
+ }
714
+
715
+ const returnType = fieldDef . type ;
716
+
717
+ if (
718
+ shouldExecute ( fieldGroup , returnType , asyncPayloadRecord ?. deferDepth )
719
+ ) {
688
720
const result = executeField (
689
721
exeContext ,
690
722
parentType ,
723
+ fieldDef ,
724
+ returnType ,
691
725
sourceValue ,
692
726
fieldGroup ,
693
727
fieldPath ,
@@ -735,19 +769,15 @@ function toNodes(fieldGroup: FieldGroup): ReadonlyArray<FieldNode> {
735
769
function executeField (
736
770
exeContext : ExecutionContext ,
737
771
parentType : GraphQLObjectType ,
772
+ fieldDef : GraphQLField < unknown , unknown > ,
773
+ returnType : GraphQLOutputType ,
738
774
source : unknown ,
739
775
fieldGroup : FieldGroup ,
740
776
path : Path ,
741
777
asyncPayloadRecord ?: AsyncPayloadRecord ,
742
778
) : PromiseOrValue < unknown > {
743
779
const errors = asyncPayloadRecord ?. errors ?? exeContext . errors ;
744
- const fieldName = fieldGroup [ 0 ] . fieldNode . name . value ;
745
- const fieldDef = exeContext . schema . getField ( parentType , fieldName ) ;
746
- if ( ! fieldDef ) {
747
- return ;
748
- }
749
780
750
- const returnType = fieldDef . type ;
751
781
const resolveFn = fieldDef . resolve ?? exeContext . fieldResolver ;
752
782
753
783
const info = buildResolveInfo (
0 commit comments