@@ -17,40 +17,52 @@ public Selection RewriteSelection(
17
17
SelectionOptimizerContext context ,
18
18
Selection selection )
19
19
{
20
- if ( context . Type is ObjectType type &&
20
+ if ( ! ( context . Type is ObjectType type &&
21
21
type . ContextData . TryGetValue ( AlwaysProjectedFieldsKey , out object ? fieldsObj ) &&
22
- fieldsObj is string [ ] fields )
22
+ fieldsObj is string [ ] fields ) )
23
23
{
24
- int aliasCount = 0 ;
25
- for ( var i = 0 ; i < fields . Length ; i ++ )
24
+ return selection ;
25
+ }
26
+
27
+ for ( var i = 0 ; i < fields . Length ; i ++ )
28
+ {
29
+ var alias = "__projection_alias_" + i ;
30
+
31
+ // if the field is already in the selection set we do not need to project it
32
+ if ( context . Fields . TryGetValue ( fields [ i ] , out var field ) &&
33
+ field . Field . Name == fields [ i ] )
26
34
{
27
- if ( ! context . Fields . TryGetValue ( fields [ i ] , out var field ) ||
28
- field . Field . Name != fields [ i ] )
29
- {
30
- IObjectField nodesField = type . Fields [ fields [ i ] ] ;
31
- var alias = "__projection_alias_" + aliasCount ++ ;
32
- var nodesFieldNode = new FieldNode (
33
- null ,
34
- new NameNode ( fields [ i ] ) ,
35
- new NameNode ( alias ) ,
36
- Array . Empty < DirectiveNode > ( ) ,
37
- Array . Empty < ArgumentNode > ( ) ,
38
- null ) ;
39
-
40
- FieldDelegate nodesPipeline =
41
- context . CompileResolverPipeline ( nodesField , nodesFieldNode ) ;
42
-
43
- var compiledSelection = new Selection (
44
- context . Type ,
45
- nodesField ,
46
- nodesFieldNode ,
47
- nodesPipeline ,
48
- arguments : selection . Arguments ,
49
- internalSelection : true ) ;
50
-
51
- context . Fields [ alias ] = compiledSelection ;
52
- }
35
+ continue ;
53
36
}
37
+
38
+ // if the field is already added as an alias we do not need to add it
39
+ if ( context . Fields . TryGetValue ( alias , out field ) &&
40
+ field . Field . Name == fields [ i ] )
41
+ {
42
+ continue ;
43
+ }
44
+
45
+ IObjectField nodesField = type . Fields [ fields [ i ] ] ;
46
+ var nodesFieldNode = new FieldNode (
47
+ null ,
48
+ new NameNode ( fields [ i ] ) ,
49
+ new NameNode ( alias ) ,
50
+ Array . Empty < DirectiveNode > ( ) ,
51
+ Array . Empty < ArgumentNode > ( ) ,
52
+ null ) ;
53
+
54
+ FieldDelegate nodesPipeline =
55
+ context . CompileResolverPipeline ( nodesField , nodesFieldNode ) ;
56
+
57
+ var compiledSelection = new Selection (
58
+ context . Type ,
59
+ nodesField ,
60
+ nodesFieldNode ,
61
+ nodesPipeline ,
62
+ arguments : selection . Arguments ,
63
+ internalSelection : true ) ;
64
+
65
+ context . Fields [ alias ] = compiledSelection ;
54
66
}
55
67
56
68
return selection ;
0 commit comments