@@ -21,8 +21,8 @@ import { typeFromAST } from '../utilities/typeFromAST';
21
21
22
22
import { getDirectiveValues } from './values' ;
23
23
24
- interface FragmentEntry {
25
- fragment : FragmentDefinitionNode ;
24
+ interface StackEntry {
25
+ selectionSet : SelectionSetNode ;
26
26
runtimeType : GraphQLObjectType ;
27
27
}
28
28
@@ -42,31 +42,21 @@ export function collectFields(
42
42
runtimeType : GraphQLObjectType ,
43
43
selectionSet : SelectionSetNode ,
44
44
) : Map < string , ReadonlyArray < FieldNode > > {
45
- const foundFragments : Array < FragmentEntry > = [ ] ;
45
+ const stack : Array < StackEntry > = [ { selectionSet , runtimeType } ] ;
46
46
const fields = new Map ( ) ;
47
47
const visited = new Set < string > ( ) ;
48
- collectFieldsImpl (
49
- schema ,
50
- fragments ,
51
- variableValues ,
52
- runtimeType ,
53
- selectionSet ,
54
- fields ,
55
- visited ,
56
- foundFragments ,
57
- ) ;
58
48
59
49
let entry ;
60
- while ( ( entry = foundFragments . pop ( ) ) !== undefined ) {
50
+ while ( ( entry = stack . shift ( ) ) !== undefined ) {
61
51
collectFieldsImpl (
62
52
schema ,
63
53
fragments ,
64
54
variableValues ,
65
55
entry . runtimeType ,
66
- entry . fragment . selectionSet ,
56
+ entry . selectionSet ,
67
57
fields ,
68
58
visited ,
69
- foundFragments ,
59
+ stack ,
70
60
) ;
71
61
}
72
62
@@ -91,34 +81,25 @@ export function collectSubfields(
91
81
fieldNodes : ReadonlyArray < FieldNode > ,
92
82
) : Map < string , ReadonlyArray < FieldNode > > {
93
83
const subFieldNodes = new Map ( ) ;
94
- const foundFragments : Array < FragmentEntry > = [ ] ;
84
+ const stack : Array < StackEntry > = [ ] ;
95
85
const visitedFragmentNames = new Set < string > ( ) ;
96
86
for ( const node of fieldNodes ) {
97
87
if ( node . selectionSet ) {
98
- collectFieldsImpl (
99
- schema ,
100
- fragments ,
101
- variableValues ,
102
- returnType ,
103
- node . selectionSet ,
104
- subFieldNodes ,
105
- visitedFragmentNames ,
106
- foundFragments ,
107
- ) ;
88
+ stack . push ( { selectionSet : node . selectionSet , runtimeType : returnType } ) ;
108
89
}
109
90
}
110
91
111
92
let entry ;
112
- while ( ( entry = foundFragments . pop ( ) ) !== undefined ) {
93
+ while ( ( entry = stack . shift ( ) ) !== undefined ) {
113
94
collectFieldsImpl (
114
95
schema ,
115
96
fragments ,
116
97
variableValues ,
117
98
entry . runtimeType ,
118
- entry . fragment . selectionSet ,
99
+ entry . selectionSet ,
119
100
subFieldNodes ,
120
101
visitedFragmentNames ,
121
- foundFragments ,
102
+ stack ,
122
103
) ;
123
104
}
124
105
@@ -133,8 +114,9 @@ function collectFieldsImpl(
133
114
selectionSet : SelectionSetNode ,
134
115
fields : Map < string , Array < FieldNode > > ,
135
116
visitedFragmentNames : Set < string > ,
136
- foundFragments : Array < FragmentEntry > ,
117
+ stack : Array < StackEntry > ,
137
118
) : void {
119
+ const discovered = [ ] ;
138
120
for ( const selection of selectionSet . selections ) {
139
121
switch ( selection . kind ) {
140
122
case Kind . FIELD : {
@@ -157,16 +139,7 @@ function collectFieldsImpl(
157
139
) {
158
140
continue ;
159
141
}
160
- collectFieldsImpl (
161
- schema ,
162
- fragments ,
163
- variableValues ,
164
- runtimeType ,
165
- selection . selectionSet ,
166
- fields ,
167
- visitedFragmentNames ,
168
- foundFragments ,
169
- ) ;
142
+ discovered . push ( { selectionSet : selection . selectionSet , runtimeType } ) ;
170
143
break ;
171
144
}
172
145
case Kind . FRAGMENT_SPREAD : {
@@ -186,11 +159,15 @@ function collectFieldsImpl(
186
159
continue ;
187
160
}
188
161
189
- foundFragments . push ( { runtimeType , fragment } ) ;
162
+ discovered . push ( { selectionSet : fragment . selectionSet , runtimeType } ) ;
190
163
break ;
191
164
}
192
165
}
193
166
}
167
+
168
+ if ( discovered . length !== 0 ) {
169
+ stack . unshift ( ...discovered ) ;
170
+ }
194
171
}
195
172
196
173
/**
0 commit comments