1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is a part of graphql-youshido project. |
4
|
|
|
* |
5
|
|
|
* @author Portey Vasil <[email protected]> |
6
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
7
|
|
|
* created: 11/28/15 1:05 AM |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Youshido\GraphQL\Execution; |
11
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Execution\Context\ExecutionContext; |
13
|
|
|
use Youshido\GraphQL\Execution\Visitor\AbstractQueryVisitor; |
14
|
|
|
use Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor; |
15
|
|
|
use Youshido\GraphQL\Field\AbstractField; |
16
|
|
|
use Youshido\GraphQL\Field\Field; |
17
|
|
|
use Youshido\GraphQL\Introspection\Field\SchemaField; |
18
|
|
|
use Youshido\GraphQL\Introspection\Field\TypeDefinitionField; |
19
|
|
|
use Youshido\GraphQL\Parser\Ast\Field as FieldAst; |
20
|
|
|
use Youshido\GraphQL\Parser\Ast\Fragment; |
21
|
|
|
use Youshido\GraphQL\Parser\Ast\FragmentInterface; |
22
|
|
|
use Youshido\GraphQL\Parser\Ast\FragmentReference; |
23
|
|
|
use Youshido\GraphQL\Parser\Ast\Mutation; |
24
|
|
|
use Youshido\GraphQL\Parser\Ast\Query; |
25
|
|
|
use Youshido\GraphQL\Parser\Ast\TypedFragmentReference; |
26
|
|
|
use Youshido\GraphQL\Parser\Parser; |
27
|
|
|
use Youshido\GraphQL\Schema\AbstractSchema; |
28
|
|
|
use Youshido\GraphQL\Type\AbstractType; |
29
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
30
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
31
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
32
|
|
|
use Youshido\GraphQL\Type\TypeService; |
33
|
|
|
use Youshido\GraphQL\Type\Union\AbstractUnionType; |
34
|
|
|
use Youshido\GraphQL\Validator\Exception\ResolveException; |
35
|
|
|
use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidator; |
36
|
|
|
use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidatorInterface; |
37
|
|
|
use Youshido\GraphQL\Validator\SchemaValidator\SchemaValidator; |
38
|
|
|
|
39
|
|
|
class Processor |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
const TYPE_NAME_QUERY = '__typename'; |
43
|
|
|
|
44
|
|
|
/** @var array */ |
45
|
|
|
protected $data; |
46
|
|
|
|
47
|
|
|
/** @var ResolveValidatorInterface */ |
48
|
|
|
protected $resolveValidator; |
49
|
|
|
|
50
|
|
|
/** @var ExecutionContext */ |
51
|
|
|
protected $executionContext; |
52
|
|
|
|
53
|
|
|
/** @var int */ |
54
|
|
|
protected $maxComplexity; |
55
|
|
|
|
56
|
29 |
|
public function __construct(AbstractSchema $schema) |
57
|
|
|
{ |
58
|
29 |
|
(new SchemaValidator())->validate($schema); |
59
|
|
|
|
60
|
28 |
|
$this->introduceIntrospectionFields($schema); |
61
|
28 |
|
$this->executionContext = new ExecutionContext(); |
62
|
28 |
|
$this->executionContext->setSchema($schema); |
63
|
|
|
|
64
|
28 |
|
$this->resolveValidator = new ResolveValidator($this->executionContext); |
65
|
28 |
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
27 |
|
public function processPayload($payload, $variables = [], $reducers = []) |
69
|
|
|
{ |
70
|
27 |
|
if ($this->executionContext->hasErrors()) { |
71
|
7 |
|
$this->executionContext->clearErrors(); |
72
|
|
|
} |
73
|
|
|
|
74
|
27 |
|
$this->data = []; |
75
|
|
|
|
76
|
|
|
try { |
77
|
27 |
|
$this->parseAndCreateRequest($payload, $variables); |
78
|
|
|
|
79
|
27 |
|
$queryType = $this->executionContext->getSchema()->getQueryType(); |
80
|
27 |
|
$mutationType = $this->executionContext->getSchema()->getMutationType(); |
81
|
|
|
|
82
|
27 |
|
if ($this->maxComplexity) { |
83
|
1 |
|
$reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
84
|
|
|
} |
85
|
|
|
|
86
|
27 |
|
$this->reduceQuery($queryType, $mutationType, $reducers); |
87
|
|
|
|
88
|
27 |
|
foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
89
|
27 |
|
if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) { |
90
|
27 |
|
$this->data = array_merge($this->data, $operationResult); |
91
|
|
|
}; |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
} catch (\Exception $e) { |
95
|
4 |
|
$this->executionContext->addError($e); |
96
|
|
|
} |
97
|
|
|
|
98
|
27 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
27 |
|
protected function parseAndCreateRequest($payload, $variables = []) |
102
|
|
|
{ |
103
|
27 |
|
if (empty($payload)) { |
104
|
1 |
|
throw new \Exception('Must provide an operation.'); |
105
|
|
|
} |
106
|
27 |
|
$parser = new Parser(); |
107
|
|
|
|
108
|
27 |
|
$data = $parser->parse($payload); |
109
|
27 |
|
$this->executionContext->setRequest(new Request($data, $variables)); |
110
|
27 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Query|Field $query |
114
|
|
|
* @param AbstractObjectType $currentLevelSchema |
115
|
|
|
* @return array|bool|mixed |
116
|
|
|
*/ |
117
|
27 |
|
protected function executeOperation(Query $query, $currentLevelSchema) |
118
|
|
|
{ |
119
|
27 |
|
if (!$this->resolveValidator->objectHasField($currentLevelSchema, $query)) { |
120
|
1 |
|
return null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** @var AbstractField $field */ |
124
|
27 |
|
$operationField = $currentLevelSchema->getField($query->getName()); |
125
|
27 |
|
$alias = $query->getAlias() ?: $query->getName(); |
126
|
|
|
|
127
|
27 |
|
if (!$this->resolveValidator->validateArguments($operationField, $query, $this->executionContext->getRequest())) { |
128
|
6 |
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
24 |
|
return [$alias => $this->processQueryAST($query, $operationField)]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param Query $query |
136
|
|
|
* @param AbstractField $field |
137
|
|
|
* @param $contextValue |
138
|
|
|
* @return array|mixed|null |
139
|
|
|
*/ |
140
|
24 |
|
protected function processQueryAST(Query $query, AbstractField $field, $contextValue = null) |
141
|
|
|
{ |
142
|
24 |
|
if (!$this->resolveValidator->validateArguments($field, $query, $this->executionContext->getRequest())) { |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
24 |
|
$resolvedValue = $this->resolveFieldValue($field, $contextValue, $query->getFields(), $this->parseArgumentsValues($field, $query)); |
147
|
|
|
|
148
|
24 |
|
if (!$this->resolveValidator->isValidValueForField($field, $resolvedValue)) { |
149
|
2 |
|
return null; |
150
|
|
|
} |
151
|
|
|
|
152
|
24 |
|
return $this->collectValueForQueryWithType($query, $field->getType(), $resolvedValue); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param Query|Mutation $query |
157
|
|
|
* @param AbstractType $fieldType |
158
|
|
|
* @param mixed $resolvedValue |
159
|
|
|
* @return array|mixed |
160
|
|
|
*/ |
161
|
24 |
|
protected function collectValueForQueryWithType(Query $query, AbstractType $fieldType, $resolvedValue) |
162
|
|
|
{ |
163
|
24 |
|
if (is_null($resolvedValue)) { |
164
|
7 |
|
return null; |
165
|
|
|
} |
166
|
|
|
|
167
|
22 |
|
$fieldType = $this->resolveValidator->resolveTypeIfAbstract($fieldType, $resolvedValue); |
168
|
22 |
|
$value = []; |
169
|
22 |
|
$kind = $fieldType->getKind(); |
170
|
|
|
|
171
|
|
|
if ( |
172
|
22 |
|
$kind === TypeMap::KIND_LIST || |
173
|
22 |
|
($kind === TypeMap::KIND_NON_NULL && $fieldType->getNamedType()->getKind() === TypeMap::KIND_LIST) |
174
|
|
|
) { |
175
|
9 |
|
if ($kind === TypeMap::KIND_NON_NULL) $fieldType = $fieldType->getNamedType(); |
176
|
9 |
|
if (!$this->resolveValidator->hasArrayAccess($resolvedValue)) return null; |
177
|
|
|
|
178
|
9 |
|
$namedType = $fieldType->getNamedType(); |
179
|
9 |
|
$isAbstract = TypeService::isAbstractType($namedType); |
180
|
9 |
|
$validItemStructure = false; |
181
|
|
|
|
182
|
9 |
|
foreach ($resolvedValue as $resolvedValueItem) { |
|
|
|
|
183
|
8 |
|
$value[] = []; |
184
|
8 |
|
$index = count($value) - 1; |
185
|
|
|
|
186
|
8 |
|
if ($isAbstract) { |
187
|
2 |
|
$namedType = $this->resolveValidator->resolveAbstractType($fieldType->getNamedType(), $resolvedValueItem); |
188
|
|
|
} |
189
|
|
|
|
190
|
8 |
|
if (!$validItemStructure) { |
191
|
8 |
|
if (!$namedType->isValidValue($resolvedValueItem)) { |
192
|
1 |
|
$this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $query->getName()))); |
193
|
1 |
|
$value[$index] = null; |
194
|
1 |
|
continue; |
195
|
|
|
} |
196
|
7 |
|
$validItemStructure = true; |
197
|
|
|
} |
198
|
|
|
|
199
|
9 |
|
$value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
200
|
|
|
} |
201
|
|
|
} else { |
202
|
22 |
|
if (!$query->hasFields()) { |
203
|
5 |
|
if (TypeService::isObjectType($fieldType)) { |
204
|
1 |
|
throw new ResolveException(sprintf('You have to specify fields for "%s"', $query->getName())); |
205
|
|
|
} |
206
|
|
|
|
207
|
4 |
|
return $this->getOutputValue($fieldType, $resolvedValue); |
208
|
|
|
} |
209
|
|
|
|
210
|
20 |
|
$value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value); |
211
|
|
|
} |
212
|
|
|
|
213
|
20 |
|
return $value; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param FieldAst $fieldAst |
218
|
|
|
* @param AbstractField $field |
219
|
|
|
* |
220
|
|
|
* @param mixed $contextValue |
221
|
|
|
* @return array|mixed|null |
222
|
|
|
* @throws ResolveException |
223
|
|
|
* @throws \Exception |
224
|
|
|
*/ |
225
|
19 |
|
protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue) |
226
|
|
|
{ |
227
|
19 |
|
$value = null; |
|
|
|
|
228
|
19 |
|
$fieldType = $field->getType(); |
229
|
19 |
|
$preResolvedValue = $this->getPreResolvedValue($contextValue, $fieldAst, $field); |
230
|
|
|
|
231
|
19 |
|
if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
232
|
1 |
|
$listValue = []; |
233
|
1 |
|
$type = $fieldType->getNamedType(); |
234
|
|
|
|
235
|
1 |
|
foreach ($preResolvedValue as $resolvedValueItem) { |
236
|
1 |
|
$listValue[] = $this->getOutputValue($type, $resolvedValueItem); |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
$value = $listValue; |
240
|
|
|
} else { |
241
|
19 |
|
$value = $this->getOutputValue($fieldType, $preResolvedValue); |
242
|
|
|
} |
243
|
|
|
|
244
|
19 |
|
return $value; |
245
|
|
|
} |
246
|
|
|
|
247
|
24 |
|
protected function createResolveInfo($field, $fields) |
248
|
|
|
{ |
249
|
24 |
|
return new ResolveInfo($field, $fields, $this->executionContext); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param $contextValue |
254
|
|
|
* @param FieldAst $fieldAst |
255
|
|
|
* @param AbstractField $field |
256
|
|
|
* |
257
|
|
|
* @throws \Exception |
258
|
|
|
* |
259
|
|
|
* @return mixed |
260
|
|
|
*/ |
261
|
19 |
|
protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
262
|
|
|
{ |
263
|
19 |
|
if ($field->hasArguments() && !$this->resolveValidator->validateArguments($field, $fieldAst, $this->executionContext->getRequest())) { |
264
|
|
|
throw new \Exception(sprintf('Not valid arguments for the field "%s"', $fieldAst->getName())); |
265
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
19 |
|
return $this->resolveFieldValue($field, $contextValue, [$fieldAst], $fieldAst->getKeyValueArguments()); |
269
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
272
|
24 |
|
protected function resolveFieldValue(AbstractField $field, $contextValue, array $fields, array $args) |
273
|
|
|
{ |
274
|
24 |
|
return $field->resolve($contextValue, $args, $this->createResolveInfo($field, $fields)); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param $field AbstractField |
279
|
|
|
* @param $query Query |
280
|
|
|
* |
281
|
|
|
* @return array |
282
|
|
|
*/ |
283
|
24 |
|
protected function parseArgumentsValues(AbstractField $field, Query $query) |
284
|
|
|
{ |
285
|
24 |
|
$args = []; |
286
|
24 |
|
foreach ($query->getArguments() as $argument) { |
287
|
15 |
|
if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
288
|
15 |
|
$args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
24 |
|
return $args; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param $query Query|FragmentInterface |
297
|
|
|
* @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
298
|
|
|
* @param $resolvedValue mixed |
299
|
|
|
* @param $value array |
300
|
|
|
* |
301
|
|
|
* @throws \Exception |
302
|
|
|
* |
303
|
|
|
* @return array |
304
|
|
|
*/ |
305
|
20 |
|
protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
306
|
|
|
{ |
307
|
20 |
|
$currentType = $queryType->getNullableType(); |
308
|
|
|
|
309
|
20 |
View Code Duplication |
if ($currentType->getKind() == TypeMap::KIND_SCALAR) { |
|
|
|
|
310
|
1 |
|
if (!$query->hasFields()) { |
311
|
1 |
|
return $this->getOutputValue($currentType, $resolvedValue); |
312
|
|
|
} else { |
313
|
1 |
|
$this->executionContext->addError(new ResolveException(sprintf('Fields are not found in query "%s"', $query->getName()))); |
314
|
|
|
|
315
|
1 |
|
return null; |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
20 |
|
foreach ($query->getFields() as $fieldAst) { |
320
|
|
|
|
321
|
20 |
|
if ($fieldAst instanceof FragmentInterface) { |
322
|
|
|
/** @var TypedFragmentReference $fragment */ |
323
|
3 |
|
$fragment = $fieldAst; |
324
|
3 |
|
if ($fieldAst instanceof FragmentReference) { |
325
|
|
|
/** @var Fragment $fragment */ |
326
|
2 |
|
$fieldAstName = $fieldAst->getName(); |
327
|
2 |
|
$fragment = $this->executionContext->getRequest()->getFragment($fieldAstName); |
328
|
2 |
|
$this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $queryType); |
329
|
1 |
|
} elseif ($fragment->getTypeName() !== $queryType->getName()) { |
330
|
1 |
|
continue; |
331
|
|
|
} |
332
|
|
|
|
333
|
3 |
|
$fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value); |
334
|
3 |
|
$value = is_array($fragmentValue) ? $fragmentValue : []; |
335
|
|
|
} else { |
336
|
20 |
|
$fieldAstName = $fieldAst->getName(); |
337
|
20 |
|
$alias = $fieldAst->getAlias() ?: $fieldAstName; |
338
|
|
|
|
339
|
20 |
|
if ($fieldAstName == self::TYPE_NAME_QUERY) { |
340
|
1 |
|
$value[$alias] = $queryType->getName(); |
341
|
|
|
} else { |
342
|
20 |
|
$field = $currentType->getField($fieldAstName); |
343
|
20 |
|
if (!$field) { |
344
|
3 |
|
$this->executionContext->addError(new ResolveException(sprintf('Field "%s" is not found in type "%s"', $fieldAstName, $currentType->getName()))); |
345
|
|
|
|
346
|
3 |
|
return null; |
347
|
|
|
} |
348
|
20 |
|
if ($fieldAst instanceof Query) { |
349
|
10 |
|
$value[$alias] = $this->processQueryAST($fieldAst, $field, $resolvedValue); |
350
|
|
|
} elseif ($fieldAst instanceof FieldAst) { |
351
|
19 |
|
$value[$alias] = $this->processFieldAST($fieldAst, $field, $resolvedValue); |
352
|
|
|
} else { |
353
|
20 |
|
return $value; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
360
|
20 |
|
return $value; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
protected function getFieldValidatedValue(AbstractField $field, $value) |
364
|
|
|
{ |
365
|
|
|
return ($this->resolveValidator->isValidValueForField($field, $value)) ? $this->getOutputValue($field->getType(), $value) : null; |
366
|
|
|
} |
367
|
|
|
|
368
|
21 |
|
protected function getOutputValue(AbstractType $type, $value) |
369
|
|
|
{ |
370
|
21 |
|
return in_array($type->getKind(), [TypeMap::KIND_OBJECT, TypeMap::KIND_NON_NULL]) ? $value : $type->serialize($value); |
371
|
|
|
} |
372
|
|
|
|
373
|
28 |
|
protected function introduceIntrospectionFields(AbstractSchema $schema) |
374
|
|
|
{ |
375
|
28 |
|
$schemaField = new SchemaField(); |
376
|
28 |
|
$schema->addQueryField($schemaField); |
377
|
28 |
|
$schema->addQueryField(new TypeDefinitionField()); |
378
|
28 |
|
} |
379
|
|
|
|
380
|
27 |
|
public function getResponseData() |
381
|
|
|
{ |
382
|
27 |
|
$result = []; |
383
|
|
|
|
384
|
27 |
|
if (!empty($this->data)) { |
385
|
24 |
|
$result['data'] = $this->data; |
386
|
|
|
} |
387
|
|
|
|
388
|
27 |
|
if ($this->executionContext->hasErrors()) { |
389
|
10 |
|
$result['errors'] = $this->executionContext->getErrorsArray(); |
390
|
|
|
} |
391
|
|
|
|
392
|
27 |
|
return $result; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Convenience function for attaching a MaxComplexityQueryVisitor($max) to the next processor run |
397
|
|
|
* |
398
|
|
|
* @param int $max |
399
|
|
|
*/ |
400
|
1 |
|
public function setMaxComplexity($max) |
401
|
|
|
{ |
402
|
1 |
|
$this->maxComplexity = $max; |
403
|
1 |
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Apply all of $reducers to this query. Example reducer operations: checking for maximum query complexity, |
407
|
|
|
* performing look-ahead query planning, etc. |
408
|
|
|
* |
409
|
|
|
* @param AbstractType $queryType |
410
|
|
|
* @param AbstractType $mutationType |
411
|
|
|
* @param AbstractQueryVisitor[] $reducers |
412
|
|
|
*/ |
413
|
27 |
|
protected function reduceQuery($queryType, $mutationType, array $reducers) |
414
|
|
|
{ |
415
|
27 |
|
foreach ($reducers as $reducer) { |
416
|
2 |
|
foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
417
|
2 |
|
$this->doVisit($operation, $operation instanceof Mutation ? $mutationType : $queryType, $reducer); |
418
|
|
|
} |
419
|
|
|
} |
420
|
27 |
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Entry point for the `walkQuery` routine. Execution bounces between here, where the reducer's ->visit() method |
424
|
|
|
* is invoked, and `walkQuery` where we send in the scores from the `visit` call. |
425
|
|
|
* |
426
|
|
|
* @param Query $query |
427
|
|
|
* @param AbstractType $currentLevelSchema |
428
|
|
|
* @param AbstractQueryVisitor $reducer |
429
|
|
|
*/ |
430
|
2 |
|
protected function doVisit(Query $query, $currentLevelSchema, $reducer) |
431
|
|
|
{ |
432
|
2 |
|
if (!($currentLevelSchema instanceof AbstractObjectType) || !$currentLevelSchema->hasField($query->getName())) { |
433
|
|
|
return; |
434
|
|
|
} |
435
|
|
|
|
436
|
2 |
|
if ($operationField = $currentLevelSchema->getField($query->getName())) { |
437
|
|
|
|
438
|
2 |
|
$coroutine = $this->walkQuery($query, $operationField); |
439
|
|
|
|
440
|
2 |
|
if ($results = $coroutine->current()) { |
441
|
2 |
|
$queryCost = 0; |
442
|
2 |
|
while ($results) { |
443
|
|
|
// initial values come from advancing the generator via ->current, subsequent values come from ->send() |
444
|
2 |
|
list($queryField, $astField, $childCost) = $results; |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* @var Query|FieldAst $queryField |
448
|
|
|
* @var Field $astField |
449
|
|
|
*/ |
450
|
2 |
|
$cost = $reducer->visit($queryField->getKeyValueArguments(), $astField->getConfig(), $childCost); |
451
|
2 |
|
$queryCost += $cost; |
452
|
2 |
|
$results = $coroutine->send($cost); |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
} |
456
|
2 |
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Coroutine to walk the query and schema in DFS manner (see AbstractQueryVisitor docs for more info) and yield a |
460
|
|
|
* tuple of (queryNode, schemaNode, childScore) |
461
|
|
|
* |
462
|
|
|
* childScore costs are accumulated via values sent into the coroutine. |
463
|
|
|
* |
464
|
|
|
* Most of the branching in this function is just to handle the different types in a query: Queries, Unions, |
465
|
|
|
* Fragments (anonymous and named), and Fields. The core of the function is simple: recurse until we hit the base |
466
|
|
|
* case of a Field and yield that back up to the visitor up in `doVisit`. |
467
|
|
|
* |
468
|
|
|
* @param Query|Field|FragmentInterface $queryNode |
469
|
|
|
* @param AbstractField $currentLevelAST |
470
|
|
|
* |
471
|
|
|
* @return \Generator |
472
|
|
|
*/ |
473
|
2 |
|
protected function walkQuery($queryNode, AbstractField $currentLevelAST) |
474
|
|
|
{ |
475
|
2 |
|
$childrenScore = 0; |
476
|
2 |
|
if (!($queryNode instanceof FieldAst)) { |
477
|
2 |
|
foreach ($queryNode->getFields() as $queryField) { |
|
|
|
|
478
|
2 |
|
if ($queryField instanceof FragmentInterface) { |
479
|
1 |
|
if ($queryField instanceof FragmentReference) { |
480
|
|
|
$queryField = $this->executionContext->getRequest()->getFragment($queryField->getName()); |
481
|
|
|
} |
482
|
|
|
// the next 7 lines are essentially equivalent to `yield from $this->walkQuery(...)` in PHP7. |
483
|
|
|
// for backwards compatibility this is equivalent. |
484
|
|
|
// This pattern is repeated multiple times in this function, and unfortunately cannot be extracted or |
485
|
|
|
// made less verbose. |
486
|
1 |
|
$gen = $this->walkQuery($queryField, $currentLevelAST); |
|
|
|
|
487
|
1 |
|
$next = $gen->current(); |
488
|
1 |
|
while ($next) { |
489
|
1 |
|
$received = (yield $next); |
490
|
1 |
|
$childrenScore += (int)$received; |
491
|
1 |
|
$next = $gen->send($received); |
492
|
|
|
} |
493
|
|
|
} else { |
494
|
2 |
|
$fieldType = $currentLevelAST->getType()->getNamedType(); |
495
|
2 |
|
if ($fieldType instanceof AbstractUnionType) { |
496
|
1 |
|
foreach ($fieldType->getTypes() as $unionFieldType) { |
497
|
1 |
View Code Duplication |
if ($fieldAst = $unionFieldType->getField($queryField->getName())) { |
|
|
|
|
498
|
1 |
|
$gen = $this->walkQuery($queryField, $fieldAst); |
499
|
1 |
|
$next = $gen->current(); |
500
|
1 |
|
while ($next) { |
501
|
1 |
|
$received = (yield $next); |
502
|
1 |
|
$childrenScore += (int)$received; |
503
|
1 |
|
$next = $gen->send($received); |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
} |
507
|
1 |
View Code Duplication |
} elseif ($fieldType instanceof AbstractObjectType && $fieldAst = $fieldType->getField($queryField->getName())) { |
|
|
|
|
508
|
1 |
|
$gen = $this->walkQuery($queryField, $fieldAst); |
509
|
1 |
|
$next = $gen->current(); |
510
|
2 |
|
while ($next) { |
511
|
1 |
|
$received = (yield $next); |
512
|
1 |
|
$childrenScore += (int)$received; |
513
|
1 |
|
$next = $gen->send($received); |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
} |
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
// sanity check. don't yield fragments; they don't contribute to cost |
520
|
2 |
|
if ($queryNode instanceof Query || $queryNode instanceof FieldAst) { |
521
|
|
|
// BASE CASE. If we're here we're done recursing - |
522
|
|
|
// this node is either a field, or a query that we've finished recursing into. |
523
|
2 |
|
yield [$queryNode, $currentLevelAST, $childrenScore]; |
524
|
|
|
} |
525
|
2 |
|
} |
526
|
|
|
} |
527
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.