@@ -38,7 +38,8 @@ public sealed partial class OperationCompiler
38
38
private readonly List < Selection > _selections = [ ] ;
39
39
private readonly HashSet < string > _directiveNames = new ( Ordinal ) ;
40
40
private readonly List < FieldMiddleware > _pipelineComponents = [ ] ;
41
- private IncludeCondition [ ] _includeConditions = Array . Empty < IncludeCondition > ( ) ;
41
+ private readonly HashSet < int > _enqueuedSelectionSets = new ( ) ;
42
+ private IncludeCondition [ ] _includeConditions = [ ] ;
42
43
private CompilerContext ? _deferContext ;
43
44
private int _nextSelectionId ;
44
45
private int _nextSelectionSetRefId ;
@@ -160,8 +161,9 @@ public IOperation Compile(
160
161
_selections . Clear ( ) ;
161
162
_directiveNames . Clear ( ) ;
162
163
_pipelineComponents . Clear ( ) ;
164
+ _enqueuedSelectionSets . Clear ( ) ;
163
165
164
- _includeConditions = Array . Empty < IncludeCondition > ( ) ;
166
+ _includeConditions = [ ] ;
165
167
_deferContext = null ;
166
168
}
167
169
}
@@ -355,18 +357,15 @@ private void CompleteSelectionSet(CompilerContext context)
355
357
356
358
var selectionPath = context . Path . Append ( selection . ResponseName ) ;
357
359
selectionSetId = GetOrCreateSelectionSetRefId ( selection . SelectionSet , selectionPath ) ;
358
- var selectionVariants = GetOrCreateSelectionVariants ( selectionSetId ) ;
359
360
var possibleTypes = context . Schema . GetPossibleTypes ( fieldType ) ;
360
361
361
- for ( var i = possibleTypes . Count - 1 ; i >= 0 ; i -- )
362
+ if ( _enqueuedSelectionSets . Add ( selectionSetId ) )
362
363
{
363
- var objectType = possibleTypes [ i ] ;
364
-
365
- if ( ! selectionVariants . ContainsSelectionSet ( objectType ) )
364
+ for ( var i = possibleTypes . Count - 1 ; i >= 0 ; i -- )
366
365
{
367
366
_backlog . Push (
368
367
new BacklogItem (
369
- objectType ,
368
+ possibleTypes [ i ] ,
370
369
selectionSetId ,
371
370
selection ,
372
371
selectionPath ,
@@ -451,21 +450,21 @@ private void ResolveFields(
451
450
case SyntaxKind . Field :
452
451
ResolveField (
453
452
context ,
454
- ( FieldNode ) selection ,
453
+ ( FieldNode ) selection ,
455
454
includeCondition ) ;
456
455
break ;
457
456
458
457
case SyntaxKind . InlineFragment :
459
458
ResolveInlineFragment (
460
459
context ,
461
- ( InlineFragmentNode ) selection ,
460
+ ( InlineFragmentNode ) selection ,
462
461
includeCondition ) ;
463
462
break ;
464
463
465
464
case SyntaxKind . FragmentSpread :
466
465
ResolveFragmentSpread (
467
466
context ,
468
- ( FragmentSpreadNode ) selection ,
467
+ ( FragmentSpreadNode ) selection ,
469
468
includeCondition ) ;
470
469
break ;
471
470
}
@@ -579,9 +578,9 @@ private void ResolveFragment(
579
578
IReadOnlyList < DirectiveNode > directives ,
580
579
long includeCondition )
581
580
{
582
- if ( typeCondition is null ||
583
- ( context . Schema . TryGetTypeFromAst ( typeCondition , out IType typeCon ) &&
584
- DoesTypeApply ( typeCon , context . Type ) ) )
581
+ if ( typeCondition is null
582
+ || ( context . Schema . TryGetTypeFromAst ( typeCondition , out IType typeCon )
583
+ && DoesTypeApply ( typeCon , context . Type ) ) )
585
584
{
586
585
includeCondition = GetSelectionIncludeCondition ( selection , includeCondition ) ;
587
586
@@ -644,8 +643,8 @@ private static bool DoesTypeApply(IType typeCondition, IObjectType current)
644
643
=> typeCondition . Kind switch
645
644
{
646
645
TypeKind . Object => ReferenceEquals ( typeCondition , current ) ,
647
- TypeKind . Interface => current . IsImplementing ( ( InterfaceType ) typeCondition ) ,
648
- TypeKind . Union => ( ( UnionType ) typeCondition ) . Types . ContainsKey ( current . Name ) ,
646
+ TypeKind . Interface => current . IsImplementing ( ( InterfaceType ) typeCondition ) ,
647
+ TypeKind . Union => ( ( UnionType ) typeCondition ) . Types . ContainsKey ( current . Name ) ,
649
648
_ => false
650
649
} ;
651
650
@@ -661,8 +660,8 @@ private FragmentDefinitionNode GetFragmentDefinition(
661
660
662
661
for ( var i = 0 ; i < document . Definitions . Count ; i ++ )
663
662
{
664
- if ( document . Definitions [ i ] is FragmentDefinitionNode fragmentDefinition &&
665
- fragmentDefinition . Name . Value . EqualsOrdinal ( fragmentName ) )
663
+ if ( document . Definitions [ i ] is FragmentDefinitionNode fragmentDefinition
664
+ && fragmentDefinition . Name . Value . EqualsOrdinal ( fragmentName ) )
666
665
{
667
666
value = fragmentDefinition ;
668
667
_fragmentDefinitions . Add ( fragmentName , value ) ;
@@ -704,6 +703,7 @@ private SelectionVariants GetOrCreateSelectionVariants(int selectionSetId)
704
703
variants = new SelectionVariants ( selectionSetId ) ;
705
704
_selectionVariants . Add ( selectionSetId , variants ) ;
706
705
}
706
+
707
707
return variants ;
708
708
}
709
709
@@ -847,26 +847,25 @@ internal void RegisterNewSelection(Selection newSelection)
847
847
}
848
848
}
849
849
850
- private readonly struct SelectionSetRef : IEquatable < SelectionSetRef >
850
+ private readonly struct SelectionSetRef (
851
+ SelectionSetNode selectionSet ,
852
+ SelectionPath path )
853
+ : IEquatable < SelectionSetRef >
851
854
{
852
- public SelectionSetRef ( SelectionSetNode selectionSet , SelectionPath path )
853
- {
854
- SelectionSet = selectionSet ;
855
- Path = path ;
856
- }
857
-
858
- public SelectionSetNode SelectionSet { get ; }
855
+ public SelectionSetNode SelectionSet { get ; } = selectionSet ;
859
856
860
- public SelectionPath Path { get ; }
857
+ public SelectionPath Path { get ; } = path ;
861
858
862
859
public bool Equals ( SelectionSetRef other )
863
- => SelectionSet . Equals ( other . SelectionSet , SyntaxComparison . Syntax ) &&
864
- Path . Equals ( other . Path ) ;
860
+ => SyntaxComparer . BySyntax . Equals ( SelectionSet , other . SelectionSet )
861
+ && Path . Equals ( other . Path ) ;
865
862
866
863
public override bool Equals ( object ? obj )
867
864
=> obj is SelectionSetRef other && Equals ( other ) ;
868
865
869
866
public override int GetHashCode ( )
870
- => HashCode . Combine ( SelectionSet , Path ) ;
867
+ => HashCode . Combine (
868
+ SyntaxComparer . BySyntax . GetHashCode ( SelectionSet ) ,
869
+ Path . GetHashCode ( ) ) ;
871
870
}
872
- }
871
+ }
0 commit comments