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