@@ -42,7 +42,7 @@ class QueryPlan
42
42
private $ fragments ;
43
43
44
44
/** @var bool */
45
- private $ withMetaFields ;
45
+ private $ withTypeConditions ;
46
46
47
47
/**
48
48
* @param FieldNode[] $fieldNodes
@@ -52,10 +52,10 @@ class QueryPlan
52
52
*/
53
53
public function __construct (ObjectType $ parentType , Schema $ schema , iterable $ fieldNodes , array $ variableValues , array $ fragments , array $ options )
54
54
{
55
- $ this ->schema = $ schema ;
56
- $ this ->variableValues = $ variableValues ;
57
- $ this ->fragments = $ fragments ;
58
- $ this ->withMetaFields = in_array ('with-meta-fields ' , $ options , true );
55
+ $ this ->schema = $ schema ;
56
+ $ this ->variableValues = $ variableValues ;
57
+ $ this ->fragments = $ fragments ;
58
+ $ this ->withTypeConditions = in_array ('with-type-conditions ' , $ options , true );
59
59
$ this ->analyzeQueryPlan ($ parentType , $ fieldNodes );
60
60
}
61
61
@@ -128,11 +128,11 @@ private function analyzeQueryPlan(ObjectType $parentType, iterable $fieldNodes)
128
128
}
129
129
130
130
$ selectionSet = $ fieldNode ->selectionSet ;
131
- $ data = $ this ->withMetaFields ? $ this ->analyzeSelectionSetWithMetaFields ($ selectionSet , $ type ) : $ this ->analyzeSelectionSet ($ selectionSet , $ type );
131
+ $ data = $ this ->withTypeConditions ? $ this ->analyzeSelectionSetWithTypeConditions ($ selectionSet , $ type ) : $ this ->analyzeSelectionSet ($ selectionSet , $ type );
132
132
133
133
$ this ->types [$ type ->name ] = array_unique (array_merge (
134
134
array_key_exists ($ type ->name , $ this ->types ) ? $ this ->types [$ type ->name ] : [],
135
- array_keys ($ this ->withMetaFields ? $ data ['fields ' ] : $ data )
135
+ array_keys ($ this ->withTypeConditions ? $ data ['fields ' ] : $ data )
136
136
));
137
137
138
138
$ queryPlan = array_merge_recursive (
@@ -151,11 +151,10 @@ private function analyzeQueryPlan(ObjectType $parentType, iterable $fieldNodes)
151
151
*
152
152
* @throws Error
153
153
*/
154
- private function analyzeSelectionSetWithMetaFields (SelectionSetNode $ selectionSet , Type $ parentType ) : array
154
+ private function analyzeSelectionSetWithTypeConditions (SelectionSetNode $ selectionSet , Type $ parentType ) : array
155
155
{
156
- $ fields = [];
157
- $ fragments = [];
158
- $ inlineFragments = [];
156
+ $ fields = [];
157
+ $ typeConditions = [];
159
158
160
159
foreach ($ selectionSet ->selections as $ selectionNode ) {
161
160
if ($ selectionNode instanceof FieldNode && ($ parentType instanceof InterfaceType || $ parentType instanceof ObjectType)) {
@@ -173,19 +172,23 @@ private function analyzeSelectionSetWithMetaFields(SelectionSetNode $selectionSe
173
172
if ($ fragment ) {
174
173
$ type = $ this ->schema ->getType ($ fragment ->typeCondition ->name ->value );
175
174
176
- $ fragmentData = &$ fragments [$ spreadName ];
177
- $ fragmentData ['type ' ] = $ type ;
178
- $ fragmentData += $ this ->analyzeSubFields ($ type , $ fragment ->selectionSet );
175
+ $ typeConditions [$ type ->name ] = $ this ->arrayMergeDeep (
176
+ $ this ->analyzeSubFields ($ type , $ fragment ->selectionSet ),
177
+ $ typeConditions [$ type ->name ] ?? []
178
+ );
179
179
}
180
180
} elseif ($ selectionNode instanceof InlineFragmentNode) {
181
181
$ type = $ this ->schema ->getType ($ selectionNode ->typeCondition ->name ->value );
182
182
183
- $ inlineFragments [$ type ->name ] = $ this ->analyzeSubFields ($ type , $ selectionNode ->selectionSet );
183
+ $ typeConditions [$ type ->name ] = $ this ->arrayMergeDeep (
184
+ $ this ->analyzeSubFields ($ type , $ selectionNode ->selectionSet ),
185
+ $ typeConditions [$ type ->name ] ?? []
186
+ );
184
187
}
185
188
}
186
- unset($ fieldData, $ fragmentData );
189
+ unset($ fieldData );
187
190
188
- return ['fields ' => $ fields ] + array_filter (['fragments ' => $ fragments , ' inlineFragments ' => $ inlineFragments ]);
191
+ return ['fields ' => $ fields ] + array_filter (['typeConditions ' => $ typeConditions ]);
189
192
}
190
193
191
194
/**
@@ -243,11 +246,15 @@ private function analyzeSubFields(Type $type, SelectionSetNode $selectionSet) :
243
246
$ type = $ type ->getWrappedType ();
244
247
}
245
248
246
- $ data = $ this ->withMetaFields ? $ this ->analyzeSelectionSetWithMetaFields ($ selectionSet , $ type ) : $ this ->analyzeSelectionSet ($ selectionSet , $ type );
249
+ if (! $ this ->withTypeConditions && ! $ type instanceof ObjectType) {
250
+ return [];
251
+ }
252
+
253
+ $ data = $ this ->withTypeConditions ? $ this ->analyzeSelectionSetWithTypeConditions ($ selectionSet , $ type ) : $ this ->analyzeSelectionSet ($ selectionSet , $ type );
247
254
248
255
$ this ->types [$ type ->name ] = array_unique (array_merge (
249
256
array_key_exists ($ type ->name , $ this ->types ) ? $ this ->types [$ type ->name ] : [],
250
- array_keys ($ this ->withMetaFields ? $ data ['fields ' ] : $ data )
257
+ array_keys ($ this ->withTypeConditions ? $ data ['fields ' ] : $ data )
251
258
));
252
259
253
260
return $ data ;
0 commit comments