|
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
|
31 |
|
} |
|
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
|
1 |
|
} |
|
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
|
26 |
|
$this->data = array_merge($this->data, $operationResult); |
|
87
|
26 |
|
}; |
|
88
|
29 |
|
} |
|
89
|
|
|
|
|
90
|
29 |
|
} 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
|
2 |
|
} |
|
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 |
|
$namedItemType = $this->resolveValidator->resolveTypeIfAbstract($namedType, $resolvedValueItem); |
|
196
|
|
|
|
|
197
|
9 |
|
if (!$validItemStructure) { |
|
198
|
9 |
|
if (!$namedItemType->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
|
8 |
|
} |
|
205
|
|
|
|
|
206
|
8 |
|
$value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
207
|
10 |
|
} |
|
208
|
10 |
|
} 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
|
1 |
|
} |
|
238
|
|
|
|
|
239
|
1 |
|
$value = $listValue; |
|
240
|
1 |
|
} 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
|
15 |
|
} |
|
289
|
26 |
|
} |
|
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
|
21 |
|
$innerQueryType = $queryType; |
|
311
|
21 |
|
while ($innerQueryType instanceof CompositeTypeInterface) { |
|
312
|
4 |
|
$innerQueryType = $innerQueryType->getTypeOf(); |
|
313
|
4 |
|
} |
|
314
|
|
|
|
|
315
|
21 |
|
$innerOriginalType = $originalType; |
|
316
|
21 |
|
while ($innerOriginalType instanceof CompositeTypeInterface) { |
|
317
|
4 |
|
$innerOriginalType = $innerOriginalType->getTypeOf(); |
|
318
|
4 |
|
} |
|
319
|
|
|
|
|
320
|
21 |
View Code Duplication |
if ($currentType->getKind() == TypeMap::KIND_SCALAR) { |
|
|
|
|
|
|
321
|
1 |
|
if (!$query->hasFields()) { |
|
322
|
1 |
|
return $this->getOutputValue($currentType, $resolvedValue); |
|
323
|
|
|
} else { |
|
324
|
1 |
|
$this->executionContext->addError(new ResolveException(sprintf('Fields are not found in query "%s"', $query->getName()))); |
|
325
|
|
|
|
|
326
|
1 |
|
return null; |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
21 |
|
foreach ($query->getFields() as $fieldAst) { |
|
331
|
|
|
|
|
332
|
21 |
|
if ($fieldAst instanceof FragmentInterface) { |
|
333
|
|
|
|
|
334
|
|
|
/** @var TypedFragmentReference $fragment */ |
|
335
|
3 |
|
$fragment = $fieldAst; |
|
336
|
3 |
|
if ($fieldAst instanceof FragmentReference) { |
|
337
|
|
|
/** @var Fragment $fragment */ |
|
338
|
2 |
|
$fieldAstName = $fieldAst->getName(); |
|
339
|
2 |
|
$fragment = $this->executionContext->getRequest()->getFragment($fieldAstName); |
|
340
|
2 |
|
$this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $originalType); |
|
341
|
|
|
|
|
342
|
2 |
|
if ($fragment->getModel() !== $innerQueryType->getName() && $fragment->getModel() !== $innerOriginalType->getName()) { |
|
343
|
|
|
continue; |
|
344
|
|
|
} |
|
345
|
3 |
|
} elseif ($fragment->getTypeName() !== $innerQueryType->getName() && $fragment->getTypeName() !== $innerOriginalType->getName()) { |
|
346
|
1 |
|
continue; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
3 |
|
$fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value); |
|
350
|
3 |
|
$value = is_array($fragmentValue) ? $fragmentValue : []; |
|
351
|
3 |
|
} else { |
|
352
|
21 |
|
$fieldAstName = $fieldAst->getName(); |
|
353
|
21 |
|
$alias = $fieldAst->getAlias() ?: $fieldAstName; |
|
354
|
|
|
|
|
355
|
21 |
|
if ($fieldAstName == self::TYPE_NAME_QUERY) { |
|
356
|
1 |
|
$value[$alias] = $queryType->getName(); |
|
357
|
1 |
|
} else { |
|
358
|
21 |
|
$field = $currentType->getField($fieldAstName); |
|
359
|
21 |
|
if (!$field) { |
|
360
|
3 |
|
$this->executionContext->addError(new ResolveException(sprintf('Field "%s" is not found in type "%s"', $fieldAstName, $currentType->getName()))); |
|
361
|
|
|
|
|
362
|
3 |
|
return null; |
|
363
|
|
|
} |
|
364
|
21 |
|
if ($fieldAst instanceof Query) { |
|
365
|
11 |
|
$value[$alias] = $this->processQueryAST($fieldAst, $field, $resolvedValue); |
|
366
|
21 |
|
} elseif ($fieldAst instanceof FieldAst) { |
|
367
|
20 |
|
if (!TypeService::isLeafType($field->getType()->getNamedType()->getNullableType())) { |
|
368
|
1 |
|
throw new ResolveException(sprintf('You have to specify fields for "%s"', $field->getName())); |
|
369
|
|
|
} |
|
370
|
20 |
|
$value[$alias] = $this->processFieldAST($fieldAst, $field, $resolvedValue); |
|
371
|
20 |
|
} else { |
|
372
|
|
|
return $value; |
|
373
|
|
|
} |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
21 |
|
} |
|
378
|
|
|
|
|
379
|
21 |
|
return $value; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
protected function getFieldValidatedValue(FieldInterface $field, $value) |
|
383
|
|
|
{ |
|
384
|
|
|
return ($this->resolveValidator->isValidValueForField($field, $value)) ? $this->getOutputValue($field->getType(), $value) : null; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
23 |
|
protected function getOutputValue(AbstractType $type, $value) |
|
388
|
|
|
{ |
|
389
|
23 |
|
return in_array($type->getKind(), [TypeMap::KIND_OBJECT, TypeMap::KIND_NON_NULL]) ? $value : $type->serialize($value); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
29 |
|
public function getResponseData() |
|
393
|
|
|
{ |
|
394
|
29 |
|
$result = []; |
|
395
|
|
|
|
|
396
|
29 |
|
if (!empty($this->data)) { |
|
397
|
26 |
|
$result['data'] = $this->data; |
|
398
|
26 |
|
} |
|
399
|
|
|
|
|
400
|
29 |
|
if ($this->executionContext->hasErrors()) { |
|
401
|
10 |
|
$result['errors'] = $this->executionContext->getErrorsArray(); |
|
402
|
10 |
|
} |
|
403
|
|
|
|
|
404
|
29 |
|
return $result; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* You can access ExecutionContext to check errors and inject dependencies |
|
409
|
|
|
* |
|
410
|
|
|
* @return ExecutionContext |
|
411
|
|
|
*/ |
|
412
|
9 |
|
public function getExecutionContext() |
|
413
|
|
|
{ |
|
414
|
9 |
|
return $this->executionContext; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* Convenience function for attaching a MaxComplexityQueryVisitor($max) to the next processor run |
|
419
|
|
|
* |
|
420
|
|
|
* @param int $max |
|
421
|
|
|
*/ |
|
422
|
1 |
|
public function setMaxComplexity($max) |
|
423
|
|
|
{ |
|
424
|
1 |
|
$this->maxComplexity = $max; |
|
425
|
1 |
|
} |
|
426
|
|
|
|
|
427
|
|
|
/** |
|
428
|
|
|
* Apply all of $reducers to this query. Example reducer operations: checking for maximum query complexity, |
|
429
|
|
|
* performing look-ahead query planning, etc. |
|
430
|
|
|
* |
|
431
|
|
|
* @param AbstractType $queryType |
|
432
|
|
|
* @param AbstractType $mutationType |
|
433
|
|
|
* @param AbstractQueryVisitor[] $reducers |
|
434
|
|
|
*/ |
|
435
|
29 |
|
protected function reduceQuery($queryType, $mutationType, array $reducers) |
|
436
|
|
|
{ |
|
437
|
29 |
|
foreach ($reducers as $reducer) { |
|
438
|
2 |
|
foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
439
|
2 |
|
$this->doVisit($operation, $operation instanceof Mutation ? $mutationType : $queryType, $reducer); |
|
440
|
2 |
|
} |
|
441
|
29 |
|
} |
|
442
|
29 |
|
} |
|
443
|
|
|
|
|
444
|
|
|
/** |
|
445
|
|
|
* Entry point for the `walkQuery` routine. Execution bounces between here, where the reducer's ->visit() method |
|
446
|
|
|
* is invoked, and `walkQuery` where we send in the scores from the `visit` call. |
|
447
|
|
|
* |
|
448
|
|
|
* @param Query $query |
|
449
|
|
|
* @param AbstractType $currentLevelSchema |
|
450
|
|
|
* @param AbstractQueryVisitor $reducer |
|
451
|
|
|
*/ |
|
452
|
2 |
|
protected function doVisit(Query $query, $currentLevelSchema, $reducer) |
|
453
|
|
|
{ |
|
454
|
2 |
|
if (!($currentLevelSchema instanceof AbstractObjectType) || !$currentLevelSchema->hasField($query->getName())) { |
|
455
|
|
|
return; |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
2 |
|
if ($operationField = $currentLevelSchema->getField($query->getName())) { |
|
459
|
|
|
|
|
460
|
2 |
|
$coroutine = $this->walkQuery($query, $operationField); |
|
461
|
|
|
|
|
462
|
2 |
|
if ($results = $coroutine->current()) { |
|
463
|
2 |
|
$queryCost = 0; |
|
464
|
2 |
|
while ($results) { |
|
465
|
|
|
// initial values come from advancing the generator via ->current, subsequent values come from ->send() |
|
466
|
2 |
|
list($queryField, $astField, $childCost) = $results; |
|
467
|
|
|
|
|
468
|
|
|
/** |
|
469
|
|
|
* @var Query|FieldAst $queryField |
|
470
|
|
|
* @var Field $astField |
|
471
|
|
|
*/ |
|
472
|
2 |
|
$cost = $reducer->visit($queryField->getKeyValueArguments(), $astField->getConfig(), $childCost); |
|
473
|
2 |
|
$queryCost += $cost; |
|
474
|
2 |
|
$results = $coroutine->send($cost); |
|
475
|
2 |
|
} |
|
476
|
2 |
|
} |
|
477
|
2 |
|
} |
|
478
|
2 |
|
} |
|
479
|
|
|
|
|
480
|
|
|
/** |
|
481
|
|
|
* Coroutine to walk the query and schema in DFS manner (see AbstractQueryVisitor docs for more info) and yield a |
|
482
|
|
|
* tuple of (queryNode, schemaNode, childScore) |
|
483
|
|
|
* |
|
484
|
|
|
* childScore costs are accumulated via values sent into the coroutine. |
|
485
|
|
|
* |
|
486
|
|
|
* Most of the branching in this function is just to handle the different types in a query: Queries, Unions, |
|
487
|
|
|
* Fragments (anonymous and named), and Fields. The core of the function is simple: recurse until we hit the base |
|
488
|
|
|
* case of a Field and yield that back up to the visitor up in `doVisit`. |
|
489
|
|
|
* |
|
490
|
|
|
* @param Query|Field|FragmentInterface $queryNode |
|
491
|
|
|
* @param FieldInterface $currentLevelAST |
|
492
|
|
|
* |
|
493
|
|
|
* @return \Generator |
|
494
|
|
|
*/ |
|
495
|
2 |
|
protected function walkQuery($queryNode, FieldInterface $currentLevelAST) |
|
496
|
|
|
{ |
|
497
|
2 |
|
$childrenScore = 0; |
|
498
|
2 |
|
if (!($queryNode instanceof FieldAst)) { |
|
499
|
2 |
|
foreach ($queryNode->getFields() as $queryField) { |
|
|
|
|
|
|
500
|
2 |
|
if ($queryField instanceof FragmentInterface) { |
|
501
|
1 |
|
if ($queryField instanceof FragmentReference) { |
|
502
|
|
|
$queryField = $this->executionContext->getRequest()->getFragment($queryField->getName()); |
|
503
|
|
|
} |
|
504
|
|
|
// the next 7 lines are essentially equivalent to `yield from $this->walkQuery(...)` in PHP7. |
|
505
|
|
|
// for backwards compatibility this is equivalent. |
|
506
|
|
|
// This pattern is repeated multiple times in this function, and unfortunately cannot be extracted or |
|
507
|
|
|
// made less verbose. |
|
508
|
1 |
|
$gen = $this->walkQuery($queryField, $currentLevelAST); |
|
|
|
|
|
|
509
|
1 |
|
$next = $gen->current(); |
|
510
|
1 |
|
while ($next) { |
|
511
|
1 |
|
$received = (yield $next); |
|
512
|
1 |
|
$childrenScore += (int)$received; |
|
513
|
1 |
|
$next = $gen->send($received); |
|
514
|
1 |
|
} |
|
515
|
1 |
|
} else { |
|
516
|
2 |
|
$fieldType = $currentLevelAST->getType()->getNamedType(); |
|
517
|
2 |
|
if ($fieldType instanceof AbstractUnionType) { |
|
518
|
1 |
|
foreach ($fieldType->getTypes() as $unionFieldType) { |
|
519
|
1 |
View Code Duplication |
if ($fieldAst = $unionFieldType->getField($queryField->getName())) { |
|
|
|
|
|
|
520
|
1 |
|
$gen = $this->walkQuery($queryField, $fieldAst); |
|
521
|
1 |
|
$next = $gen->current(); |
|
522
|
1 |
|
while ($next) { |
|
523
|
1 |
|
$received = (yield $next); |
|
524
|
1 |
|
$childrenScore += (int)$received; |
|
525
|
1 |
|
$next = $gen->send($received); |
|
526
|
1 |
|
} |
|
527
|
1 |
|
} |
|
528
|
1 |
|
} |
|
529
|
2 |
View Code Duplication |
} elseif ($fieldType instanceof AbstractObjectType && $fieldAst = $fieldType->getField($queryField->getName())) { |
|
|
|
|
|
|
530
|
1 |
|
$gen = $this->walkQuery($queryField, $fieldAst); |
|
531
|
1 |
|
$next = $gen->current(); |
|
532
|
1 |
|
while ($next) { |
|
533
|
1 |
|
$received = (yield $next); |
|
534
|
1 |
|
$childrenScore += (int)$received; |
|
535
|
1 |
|
$next = $gen->send($received); |
|
536
|
1 |
|
} |
|
537
|
1 |
|
} |
|
538
|
|
|
} |
|
539
|
2 |
|
} |
|
540
|
2 |
|
} |
|
541
|
|
|
// sanity check. don't yield fragments; they don't contribute to cost |
|
542
|
2 |
|
if ($queryNode instanceof Query || $queryNode instanceof FieldAst) { |
|
543
|
|
|
// BASE CASE. If we're here we're done recursing - |
|
544
|
|
|
// this node is either a field, or a query that we've finished recursing into. |
|
545
|
2 |
|
yield [$queryNode, $currentLevelAST, $childrenScore]; |
|
546
|
2 |
|
} |
|
547
|
2 |
|
} |
|
548
|
|
|
} |
|
549
|
|
|
|
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.