@@ -96,7 +96,7 @@ node with
96
96
InternalOperationDefinition = operationDefinition ,
97
97
ShortHash = shortHash ,
98
98
SchemaName = "None" ,
99
- SelectionSetIndex = index . ToImmutable ( ) ,
99
+ SelectionSetIndex = index ,
100
100
Backlog = ImmutableStack < WorkItem > . Empty . Push ( workItem ) ,
101
101
PathCost = 1 ,
102
102
BacklogCost = 1
@@ -149,15 +149,13 @@ node with
149
149
backlog = backlog . Push ( OperationWorkItem . CreateRoot ( selectionSet ) ) ;
150
150
}
151
151
152
- index = indexBuilder . ToImmutable ( ) ;
153
-
154
152
var node = new PlanNode
155
153
{
156
154
OperationDefinition = operationDefinition ,
157
155
InternalOperationDefinition = operationDefinition ,
158
156
ShortHash = shortHash ,
159
157
SchemaName = ISchemaDefinition . DefaultName ,
160
- SelectionSetIndex = index ,
158
+ SelectionSetIndex = indexBuilder ,
161
159
Backlog = backlog ,
162
160
PathCost = 1 ,
163
161
BacklogCost = 1
@@ -321,7 +319,7 @@ lookup is null
321
319
InternalOperationDefinition = current . InternalOperationDefinition ,
322
320
ShortHash = current . ShortHash ,
323
321
SchemaName = current . SchemaName ,
324
- SelectionSetIndex = index . ToImmutable ( ) ,
322
+ SelectionSetIndex = index ,
325
323
Backlog = backlog ,
326
324
Steps = current . Steps . Add ( step ) ,
327
325
PathCost = current . PathCost ,
@@ -375,14 +373,18 @@ private PlanNode InlineLookupRequirements(
375
373
InlineSelections (
376
374
step . Definition ,
377
375
index ,
378
- step . Type ,
376
+ workItemSelectionSet . Type ,
379
377
index . GetId ( resolvable ) ,
380
378
resolvable ) ;
381
379
382
380
var updatedStep = step with
383
381
{
384
382
Definition = operation ,
385
383
384
+ // we need to update the selection sets that this plan step
385
+ // has as the requirement could have introduced new ones.
386
+ SelectionSets = SelectionSetIndexer . CreateIdSet ( operation . SelectionSet , index ) ,
387
+
386
388
// we add the new lookup node to the dependents of the current step.
387
389
// the new lookup node will be the next index added which is the last index aka Count.
388
390
Dependents = step . Dependents . Add ( lookupStepId )
@@ -524,7 +526,7 @@ private void PlanInlineFieldWithRequirements(
524
526
InternalOperationDefinition = current . InternalOperationDefinition ,
525
527
ShortHash = current . ShortHash ,
526
528
SchemaName = current . SchemaName ,
527
- SelectionSetIndex = index . ToImmutable ( ) ,
529
+ SelectionSetIndex = index ,
528
530
Backlog = backlog ,
529
531
Steps = steps ,
530
532
PathCost = current . PathCost ,
@@ -686,7 +688,7 @@ private void PlanFieldWithRequirement(
686
688
InternalOperationDefinition = current . InternalOperationDefinition ,
687
689
ShortHash = current . ShortHash ,
688
690
SchemaName = current . SchemaName ,
689
- SelectionSetIndex = index . ToImmutable ( ) ,
691
+ SelectionSetIndex = index ,
690
692
Backlog = backlog ,
691
693
Steps = steps . Add ( step ) ,
692
694
PathCost = current . PathCost ,
@@ -794,6 +796,15 @@ private void PlanFieldWithRequirement(
794
796
continue ;
795
797
}
796
798
799
+ // the resolvable part of the requirement could be different from the requirement
800
+ // if we are unable to inline the complete requirement into a single plan step.
801
+ // in this case we will register the resolvable part as part of the requirements selection set
802
+ // so that they logically belong together.
803
+ if ( resolvable != requirements )
804
+ {
805
+ index . Register ( workItem . Selection . SelectionSetId , resolvable ) ;
806
+ }
807
+
797
808
var operation =
798
809
InlineSelections (
799
810
step . Definition ,
@@ -858,55 +869,61 @@ private OperationDefinitionNode InlineSelections(
858
869
var rewriter = SyntaxRewriter . Create < Stack < ISyntaxNode > > (
859
870
( node , path ) =>
860
871
{
861
- if ( node is SelectionSetNode selectionSet )
872
+ if ( node is not SelectionSetNode selectionSet )
862
873
{
863
- var originalSelectionSet = ( SelectionSetNode ) path . Peek ( ) ;
864
- var id = index . GetId ( originalSelectionSet ) ;
874
+ return node ;
875
+ }
865
876
866
- if ( ! ReferenceEquals ( originalSelectionSet , selectionSet ) )
867
- {
868
- index . Register ( originalSelectionSet , selectionSet ) ;
869
- }
877
+ var originalSelectionSet = ( SelectionSetNode ) path . Peek ( ) ;
878
+ var id = index . GetId ( originalSelectionSet ) ;
870
879
871
- if ( targetSelectionSetId == id )
880
+ if ( ! ReferenceEquals ( originalSelectionSet , selectionSet ) )
881
+ {
882
+ index . Register ( originalSelectionSet , selectionSet ) ;
883
+ }
884
+
885
+ if ( targetSelectionSetId != id )
886
+ {
887
+ return node ;
888
+ }
889
+
890
+ SelectionSetNode newSelectionSet ;
891
+
892
+ if ( inlineInternal )
893
+ {
894
+ var size = selectionSet . Selections . Count + selectionsToInline . Selections . Count ;
895
+ var selections = new List < ISelectionNode > ( size ) ;
896
+ selections . AddRange ( originalSelectionSet . Selections ) ;
897
+
898
+ foreach ( var selection in selectionsToInline . Selections )
872
899
{
873
- SelectionSetNode newSelectionSet ;
900
+ var directives = AddInternalDirective ( selection ) ;
874
901
875
- if ( inlineInternal )
902
+ switch ( selection )
876
903
{
877
- var size = selectionSet . Selections . Count + selectionsToInline . Selections . Count ;
878
- var selections = new List < ISelectionNode > ( size ) ;
879
- selections . AddRange ( originalSelectionSet . Selections ) ;
880
-
881
- foreach ( var selection in selectionsToInline . Selections )
882
- {
883
- var directives = AddInternalDirective ( selection ) ;
884
-
885
- switch ( selection )
886
- {
887
- case FieldNode field :
888
- selections . Add ( field . WithDirectives ( directives ) ) ;
889
- break ;
890
-
891
- case InlineFragmentNode inlineFragment :
892
- selections . Add ( inlineFragment . WithDirectives ( directives ) ) ;
893
- break ;
894
- }
895
- }
896
-
897
- newSelectionSet = new SelectionSetNode ( selections ) ;
898
- }
899
- else
900
- {
901
- newSelectionSet = _mergeRewriter . Merge ( selectionSet , selectionsToInline , selectionSetType ) ;
902
- }
904
+ case FieldNode field :
905
+ selections . Add ( field . WithDirectives ( directives ) ) ;
906
+ break ;
903
907
904
- index . Register ( originalSelectionSet , newSelectionSet ) ;
905
- return newSelectionSet ;
908
+ case InlineFragmentNode inlineFragment :
909
+ selections . Add ( inlineFragment . WithDirectives ( directives ) ) ;
910
+ break ;
911
+ }
906
912
}
913
+
914
+ newSelectionSet = new SelectionSetNode ( selections ) ;
915
+ }
916
+ else
917
+ {
918
+ newSelectionSet = _mergeRewriter . Merge (
919
+ selectionSet ,
920
+ selectionsToInline ,
921
+ selectionSetType ,
922
+ index ) ;
907
923
}
908
924
909
- return node ;
925
+ index . Register ( originalSelectionSet , newSelectionSet ) ;
926
+ return newSelectionSet ;
910
927
} ,
911
928
( node , path ) =>
912
929
{
@@ -1004,16 +1021,6 @@ public static ImmutableStack<WorkItem> Push(
1004
1021
return backlog ;
1005
1022
}
1006
1023
1007
- public static ISelectionSetIndex ToImmutable ( this ISelectionSetIndex index )
1008
- {
1009
- if ( index is SelectionSetIndexBuilder builder )
1010
- {
1011
- return builder . Build ( ) ;
1012
- }
1013
-
1014
- return index ;
1015
- }
1016
-
1017
1024
public static void Enqueue (
1018
1025
this PriorityQueue < PlanNode , double > possiblePlans ,
1019
1026
PlanNode planNodeTemplate ,
0 commit comments