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