@@ -293,10 +293,11 @@ lookup is null
293
293
requirements = requirements . Add ( argumentRequirementKey , operationRequirement ) ;
294
294
}
295
295
296
- // If the lookup returns an abstract type, we might need to insert a type refinement
297
- // around our selection set.
296
+ var isAbstractLookup = false ;
298
297
if ( _schema . Types . TryGetType ( lookup . FieldType , out var lookupFieldType )
299
- && lookupFieldType != workItem . SelectionSet . Type )
298
+ && lookupFieldType != workItem . SelectionSet . Type
299
+ && ! resolvable . Selections . All ( s => s is InlineFragmentNode inlineFragment
300
+ && inlineFragment . TypeCondition ? . Name . Value == workItem . SelectionSet . Type . Name ) )
300
301
{
301
302
var typeRefinement =
302
303
new InlineFragmentNode (
@@ -313,9 +314,11 @@ lookup is null
313
314
index = indexBuilder ;
314
315
315
316
operationBuilder . SetSelectionSet ( selectionSetWithTypeRefinement ) ;
317
+
318
+ isAbstractLookup = true ;
316
319
}
317
320
318
- operationBuilder . SetLookup ( lookup , requirementKey ) ;
321
+ operationBuilder . SetLookup ( lookup , isAbstractLookup , requirementKey ) ;
319
322
}
320
323
321
324
( var definition , index , var source ) = operationBuilder . Build ( index ) ;
@@ -686,7 +689,32 @@ private void PlanFieldWithRequirement(
686
689
requirements = requirements . Add ( argumentRequirementKey , operationRequirement ) ;
687
690
}
688
691
689
- operationBuilder . SetLookup ( workItem . Lookup , requirementKey ) ;
692
+ var isAbstractLookup = false ;
693
+ if ( _schema . Types . TryGetType ( lookup . FieldType , out var lookupFieldType )
694
+ && lookupFieldType != selectionSetStub . Type
695
+ && ! selectionSetNode . Selections . All ( s => s is InlineFragmentNode inlineFragment
696
+ && inlineFragment . TypeCondition ? . Name . Value == selectionSetStub . Type . Name ) )
697
+ {
698
+ var typeRefinement =
699
+ new InlineFragmentNode (
700
+ null ,
701
+ new NamedTypeNode ( selectionSetStub . Type . Name ) ,
702
+ [ ] ,
703
+ selectionSetNode ) ;
704
+ var selectionSetWithTypeRefinement = new SelectionSetNode ( null , [ typeRefinement ] ) ;
705
+
706
+ var indexBuilder = index . ToBuilder ( ) ;
707
+
708
+ indexBuilder . Register ( selectionSetNode , selectionSetWithTypeRefinement ) ;
709
+
710
+ index = indexBuilder ;
711
+
712
+ operationBuilder . SetSelectionSet ( selectionSetWithTypeRefinement ) ;
713
+
714
+ isAbstractLookup = true ;
715
+ }
716
+
717
+ operationBuilder . SetLookup ( workItem . Lookup , isAbstractLookup , requirementKey ) ;
690
718
691
719
var ( definition , _, source ) = operationBuilder . Build ( index ) ;
692
720
0 commit comments