|
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; |
|
11
|
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Introspection\QueryType; |
|
13
|
|
|
use Youshido\GraphQL\Introspection\SchemaType; |
|
14
|
|
|
use Youshido\GraphQL\Introspection\TypeDefinitionType; |
|
15
|
|
|
use Youshido\GraphQL\Parser\Ast\FragmentReference; |
|
16
|
|
|
use Youshido\GraphQL\Parser\Ast\Mutation; |
|
17
|
|
|
use Youshido\GraphQL\Parser\Ast\Query; |
|
18
|
|
|
use Youshido\GraphQL\Parser\Ast\TypedFragmentReference; |
|
19
|
|
|
use Youshido\GraphQL\Parser\Parser; |
|
20
|
|
|
use Youshido\GraphQL\Type\AbstractType; |
|
21
|
|
|
use Youshido\GraphQL\Type\Field\Field; |
|
22
|
|
|
use Youshido\GraphQL\Type\Object\AbstractEnumType; |
|
23
|
|
|
use Youshido\GraphQL\Type\Object\AbstractInterfaceType; |
|
24
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
|
25
|
|
|
use Youshido\GraphQL\Type\Scalar\AbstractScalarType; |
|
26
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
|
27
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
|
28
|
|
|
use Youshido\GraphQL\Parser\Ast\Field as AstField; |
|
29
|
|
|
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerTrait; |
|
30
|
|
|
use Youshido\GraphQL\Validator\Exception\ConfigurationException; |
|
31
|
|
|
use Youshido\GraphQL\Validator\Exception\ResolveException; |
|
32
|
|
|
use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidator; |
|
33
|
|
|
use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidatorInterface; |
|
34
|
|
|
use Youshido\GraphQL\Validator\SchemaValidator\SchemaValidator; |
|
35
|
|
|
|
|
36
|
|
|
class Processor |
|
37
|
|
|
{ |
|
38
|
|
|
use ErrorContainerTrait; |
|
39
|
|
|
|
|
40
|
|
|
const TYPE_NAME_QUERY = '__typename'; |
|
41
|
|
|
|
|
42
|
|
|
/** @var array */ |
|
43
|
|
|
protected $data; |
|
44
|
|
|
|
|
45
|
|
|
/** @var ResolveValidatorInterface */ |
|
46
|
|
|
protected $resolveValidator; |
|
47
|
|
|
|
|
48
|
|
|
/** @var SchemaValidator */ |
|
49
|
|
|
protected $schemaValidator; |
|
50
|
|
|
|
|
51
|
|
|
/** @var AbstractSchema */ |
|
52
|
|
|
protected $schema; |
|
53
|
|
|
|
|
54
|
|
|
/** @var Request */ |
|
55
|
|
|
protected $request; |
|
56
|
|
|
|
|
57
|
19 |
|
public function __construct() |
|
58
|
|
|
{ |
|
59
|
19 |
|
$this->resolveValidator = new ResolveValidator(); |
|
60
|
19 |
|
$this->schemaValidator = new SchemaValidator(); |
|
61
|
19 |
|
} |
|
62
|
|
|
|
|
63
|
19 |
|
public function setSchema(AbstractSchema $schema) |
|
64
|
|
|
{ |
|
65
|
19 |
|
if (!$this->schemaValidator->validate($schema)) { |
|
66
|
|
|
$this->mergeErrors($this->schemaValidator); |
|
67
|
|
|
|
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
19 |
|
$this->schema = $schema; |
|
72
|
|
|
|
|
73
|
19 |
|
$__schema = new SchemaType(); |
|
74
|
19 |
|
$__schema->setSchema($schema); |
|
75
|
|
|
|
|
76
|
19 |
|
$__type = new TypeDefinitionType(); |
|
77
|
|
|
|
|
78
|
19 |
|
$this->schema->addQuery('__schema', $__schema); |
|
79
|
19 |
|
$this->schema->addQuery('__type', $__type); |
|
80
|
19 |
|
} |
|
81
|
|
|
|
|
82
|
19 |
|
public function processRequest($payload, $variables = []) |
|
83
|
|
|
{ |
|
84
|
19 |
|
if (!$this->getSchema()) { |
|
85
|
|
|
$this->addError(new ConfigurationException('You have to set GraphQL Schema to process')); |
|
86
|
|
|
} |
|
87
|
19 |
|
if (empty($payload) || $this->hasErrors()) return $this; |
|
88
|
|
|
|
|
89
|
19 |
|
$this->data = []; |
|
90
|
|
|
|
|
91
|
|
|
try { |
|
92
|
19 |
|
$this->parseAndCreateRequest($payload, $variables); |
|
93
|
|
|
|
|
94
|
19 |
|
foreach ($this->request->getQueries() as $query) { |
|
95
|
19 |
|
if ($queryResult = $this->executeQuery($query, $this->getSchema()->getQueryType())) { |
|
96
|
19 |
|
$this->data = array_merge($this->data, $queryResult); |
|
97
|
|
|
}; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
19 |
|
foreach ($this->request->getMutations() as $mutation) { |
|
101
|
|
|
if ($mutationResult = $this->executeMutation($mutation, $this->getSchema()->getMutationType())) { |
|
102
|
19 |
|
$this->data = array_merge($this->data, $mutationResult); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} catch (\Exception $e) { |
|
107
|
|
|
$this->resolveValidator->clearErrors(); |
|
108
|
|
|
|
|
109
|
|
|
$this->resolveValidator->addError($e); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
19 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
19 |
|
protected function parseAndCreateRequest($query, $variables = []) |
|
116
|
|
|
{ |
|
117
|
19 |
|
$parser = new Parser(); |
|
118
|
|
|
|
|
119
|
19 |
|
$data = $parser->parse($query); |
|
120
|
|
|
|
|
121
|
19 |
|
$this->request = new Request($data); |
|
122
|
19 |
|
$this->request->setVariables($variables); |
|
123
|
19 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param Query|Field $query |
|
127
|
|
|
* @param ObjectType|QueryType $currentLevelSchema |
|
128
|
|
|
* @param null $contextValue |
|
129
|
|
|
* @return array|bool|mixed |
|
130
|
|
|
*/ |
|
131
|
19 |
|
protected function executeQuery($query, $currentLevelSchema, $contextValue = null) |
|
132
|
|
|
{ |
|
133
|
19 |
|
if (!$this->resolveValidator->checkFieldExist($currentLevelSchema, $query)) { |
|
134
|
|
|
return null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** @var Field $field */ |
|
138
|
19 |
|
$field = $currentLevelSchema->getField($query->getName()); |
|
|
|
|
|
|
139
|
19 |
|
$alias = $query->getAlias() ?: $query->getName(); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
19 |
|
if ($query instanceof AstField) { |
|
142
|
17 |
|
$value = $this->processAstFieldQuery($query, $contextValue, $field); |
|
143
|
|
|
} else { |
|
144
|
19 |
|
if (!$this->resolveValidator->validateArguments($field, $query, $this->request)) { |
|
145
|
2 |
|
return null; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
17 |
|
$value = $this->processFieldTypeQuery($query, $contextValue, $field); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
17 |
|
return [$alias => $value]; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param Mutation $mutation |
|
157
|
|
|
* @param ObjectType $currentLevelSchema |
|
158
|
|
|
* @return array|null |
|
159
|
|
|
* @throws ConfigurationException |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function executeMutation(Mutation $mutation, $currentLevelSchema) |
|
162
|
|
|
{ |
|
163
|
|
|
if (!$currentLevelSchema) throw new ConfigurationException('There is no mutation ' . $mutation->getName()); |
|
164
|
|
|
|
|
165
|
|
|
if (!$this->resolveValidator->checkFieldExist($currentLevelSchema, $mutation)) { |
|
166
|
|
|
return null; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** @var Field $field */ |
|
170
|
|
|
$field = $currentLevelSchema->getConfig()->getField($mutation->getName()); |
|
171
|
|
|
$alias = $mutation->getAlias() ?: $mutation->getName(); |
|
172
|
|
|
|
|
173
|
|
|
if (!$this->resolveValidator->validateArguments($field, $mutation, $this->request)) { |
|
174
|
|
|
return null; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$resolvedValue = $this->resolveValue($field, null, $mutation); |
|
178
|
|
|
|
|
179
|
|
View Code Duplication |
if (!$this->resolveValidator->validateResolvedValue($resolvedValue, $field->getType())) { |
|
|
|
|
|
|
180
|
|
|
$this->resolveValidator->addError(new ResolveException(sprintf('Not valid resolved value for mutation "%s"', $field->getType()->getName()))); |
|
181
|
|
|
|
|
182
|
|
|
return [$alias => null]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$value = $resolvedValue; |
|
186
|
|
|
if ($mutation->hasFields()) { |
|
187
|
|
|
if ($field->getType()->isAbstractType()) { |
|
188
|
|
|
$outputType = $field->getType()->getConfig()->resolveType($resolvedValue); |
|
|
|
|
|
|
189
|
|
|
} else { |
|
190
|
|
|
/** @var AbstractType $outputType */ |
|
191
|
|
|
$outputType = $field->getType(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$value = $this->collectListOrSingleValue($outputType, $resolvedValue, $mutation); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return [$alias => $value]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param AstField $astField |
|
202
|
|
|
* @param mixed $contextValue |
|
203
|
|
|
* @param Field $field |
|
204
|
|
|
* @return array|mixed|null |
|
205
|
|
|
* @throws \Exception |
|
206
|
|
|
*/ |
|
207
|
17 |
|
protected function processAstFieldQuery(AstField $astField, $contextValue, Field $field) |
|
208
|
|
|
{ |
|
209
|
17 |
|
$value = null; |
|
210
|
17 |
|
$preResolvedValue = $this->getPreResolvedValue($contextValue, $astField, $field); |
|
211
|
|
|
|
|
212
|
17 |
|
if ($field->getConfig()->getType()->getKind() == TypeMap::KIND_LIST) { |
|
213
|
1 |
|
if (!is_array($preResolvedValue)) { |
|
214
|
|
|
$this->resolveValidator->addError(new ResolveException('Not valid resolve value for list type')); |
|
215
|
|
|
|
|
216
|
|
|
return null; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
1 |
|
$listValue = []; |
|
220
|
1 |
|
foreach ($preResolvedValue as $resolvedValueItem) { |
|
221
|
|
|
/** @var TypeInterface $type */ |
|
222
|
1 |
|
$type = $field->getType()->getConfig()->getItem(); |
|
|
|
|
|
|
223
|
|
|
|
|
224
|
1 |
|
if ($type->getKind() == TypeMap::KIND_ENUM) { |
|
225
|
|
|
/** @var $type AbstractEnumType */ |
|
226
|
1 |
|
if (!$type->isValidValue($resolvedValueItem)) { |
|
227
|
|
|
$this->resolveValidator->addError(new ResolveException('Not valid value for enum type')); |
|
228
|
|
|
|
|
229
|
|
|
$listValue = null; |
|
230
|
|
|
break; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
1 |
|
$listValue[] = $type->resolve($resolvedValueItem); |
|
234
|
|
|
} else { |
|
235
|
|
|
/** @var AbstractScalarType $type */ |
|
236
|
1 |
|
$listValue[] = $type->serialize($preResolvedValue); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
1 |
|
$value = $listValue; |
|
241
|
|
|
} else { |
|
242
|
17 |
|
if ($field->getType()->getKind() == TypeMap::KIND_ENUM) { |
|
243
|
|
|
if (!$field->getType()->isValidValue($preResolvedValue)) { |
|
244
|
|
|
$this->resolveValidator->addError(new ResolveException(sprintf('Not valid value for %s type', ($field->getType()->getKind())))); |
|
245
|
|
|
$value = null; |
|
246
|
|
|
} else { |
|
247
|
|
|
$value = $preResolvedValue; |
|
248
|
|
|
/** $field->getType()->resolve($preResolvedValue); */ |
|
249
|
|
|
} |
|
250
|
17 |
|
} elseif ($field->getType()->getKind() == TypeMap::KIND_NON_NULL) { |
|
251
|
|
|
if (!$field->getType()->isValidValue($preResolvedValue)) { |
|
252
|
|
|
$this->resolveValidator->addError(new ResolveException(sprintf('Cannot return null for non-nullable field %s', $astField->getName() . '.' . $field->getName()))); |
|
|
|
|
|
|
253
|
|
|
} elseif (!$field->getType()->getNullableType()->isValidValue($preResolvedValue)) { |
|
254
|
|
|
$this->resolveValidator->addError(new ResolveException(sprintf('Not valid value for %s field %s', $field->getType()->getNullableType()->getKind(), $field->getName()))); |
|
|
|
|
|
|
255
|
|
|
$value = null; |
|
256
|
|
|
} else { |
|
257
|
|
|
$value = $preResolvedValue; |
|
258
|
|
|
} |
|
259
|
|
|
} else { |
|
260
|
17 |
|
$value = $field->getType()->serialize($preResolvedValue); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
17 |
|
return $value; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @param $query |
|
269
|
|
|
* @param mixed $contextValue |
|
270
|
|
|
* @param Field $field |
|
271
|
|
|
* @return null |
|
272
|
|
|
* @throws \Exception |
|
273
|
|
|
*/ |
|
274
|
17 |
|
protected function processFieldTypeQuery($query, $contextValue, $field) |
|
275
|
|
|
{ |
|
276
|
17 |
|
if (!($resolvedValue = $this->resolveValue($field, $contextValue, $query))) { |
|
277
|
3 |
|
return $resolvedValue; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
17 |
View Code Duplication |
if (!$this->resolveValidator->validateResolvedValue($resolvedValue, $field->getType())) { |
|
|
|
|
|
|
281
|
|
|
$this->resolveValidator->addError(new ResolveException(sprintf('Not valid resolved value for query "%s"', $field->getType()->getName()))); |
|
282
|
|
|
|
|
283
|
|
|
return null; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
17 |
|
return $this->collectListOrSingleValue($field->getType(), $resolvedValue, $query); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @param AbstractType $fieldType |
|
291
|
|
|
* @param mixed $resolvedValue |
|
292
|
|
|
* @param Query|Mutation $query |
|
293
|
|
|
* @return array|mixed |
|
294
|
|
|
* @throws \Exception |
|
295
|
|
|
*/ |
|
296
|
17 |
|
protected function collectListOrSingleValue(AbstractType $fieldType, $resolvedValue, $query) |
|
297
|
|
|
{ |
|
298
|
17 |
|
$value = []; |
|
299
|
17 |
|
if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
300
|
7 |
|
foreach ($resolvedValue as $resolvedValueItem) { |
|
301
|
7 |
|
$value[] = []; |
|
302
|
7 |
|
$index = count($value) - 1; |
|
303
|
7 |
|
$namedType = $fieldType->getNamedType(); |
|
304
|
|
|
|
|
305
|
7 |
|
if ($namedType->isAbstractType()) { |
|
306
|
4 |
|
$resolvedType = $namedType->getConfig()->resolveType($resolvedValueItem); |
|
|
|
|
|
|
307
|
4 |
|
if ($namedType instanceof AbstractInterfaceType) { |
|
308
|
2 |
|
$this->resolveValidator->assertTypeImplementsInterface($resolvedType, $namedType); |
|
309
|
|
|
} |
|
310
|
4 |
|
$namedType = $resolvedType; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
7 |
|
$value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
314
|
|
|
} |
|
315
|
|
|
} else { |
|
316
|
15 |
|
$value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
17 |
|
return $value; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* @param $value |
|
324
|
|
|
* @param AstField $astField |
|
325
|
|
|
* @param Field $field |
|
326
|
|
|
* |
|
327
|
|
|
* @throws \Exception |
|
328
|
|
|
* |
|
329
|
|
|
* @return mixed |
|
330
|
|
|
*/ |
|
331
|
17 |
|
protected function getPreResolvedValue($value, AstField $astField, Field $field) |
|
332
|
|
|
{ |
|
333
|
17 |
|
$resolved = false; |
|
334
|
17 |
|
$resolverValue = null; |
|
335
|
|
|
|
|
336
|
17 |
|
if (is_array($value) && array_key_exists($astField->getName(), $value)) { |
|
337
|
12 |
|
$resolverValue = $value[$astField->getName()]; |
|
338
|
12 |
|
$resolved = true; |
|
339
|
5 |
|
} elseif (is_object($value)) { |
|
340
|
|
|
try { |
|
341
|
5 |
|
$resolverValue = $this->getPropertyValue($value, $astField->getName()); |
|
342
|
5 |
|
$resolved = true; |
|
343
|
5 |
|
} catch (\Exception $e) { |
|
|
|
|
|
|
344
|
|
|
} |
|
345
|
|
|
} elseif ($field->getNamedType()->getKind() == TypeMap::KIND_SCALAR) { |
|
346
|
|
|
$resolved = true; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
17 |
|
if ($resolved) { |
|
350
|
17 |
|
if ($field->getConfig() && ($field->getConfig()->issetResolve())) { |
|
351
|
1 |
|
$resolverValue = $field->resolve($resolverValue, $astField->getKeyValueArguments(), $field->getType()); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
17 |
|
return $resolverValue; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
throw new \Exception(sprintf('Property "%s" not found in resolve result', $astField->getName())); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
5 |
|
protected function getPropertyValue($data, $path) |
|
361
|
|
|
{ |
|
362
|
5 |
|
if (is_object($data)) { |
|
363
|
5 |
|
$getter = 'get' . $this->classify($path); |
|
364
|
|
|
|
|
365
|
5 |
|
return is_callable([$data, $getter]) ? $data->$getter() : null; |
|
366
|
|
|
} elseif (is_array($data)) { |
|
367
|
|
|
return array_key_exists($path, $data) ? $data[$path] : null; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
return null; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
5 |
|
protected function classify($text) |
|
374
|
|
|
{ |
|
375
|
5 |
|
$text = explode(' ', str_replace(['_', '/', '-', '.'], ' ', $text)); |
|
376
|
5 |
|
$textLength = count($text); |
|
377
|
5 |
|
for ($i = 0; $i < $textLength; $i++) { |
|
378
|
5 |
|
$text[$i] = ucfirst($text[$i]); |
|
379
|
|
|
} |
|
380
|
5 |
|
$text = ucfirst(implode('', $text)); |
|
381
|
|
|
|
|
382
|
5 |
|
return $text; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* @param Field $field |
|
387
|
|
|
* @param mixed $contextValue |
|
388
|
|
|
* @param Query $query |
|
389
|
|
|
* |
|
390
|
|
|
* @return mixed |
|
391
|
|
|
*/ |
|
392
|
17 |
|
protected function resolveValue($field, $contextValue, $query) |
|
393
|
|
|
{ |
|
394
|
17 |
|
$resolvedValue = $field->resolve($contextValue, $this->parseArgumentsValues($field, $query), $field->getType()); |
|
395
|
|
|
|
|
396
|
17 |
|
if ($field->getType()->isAbstractType()) { |
|
397
|
6 |
|
$resolvedType = $field->getType()->resolveType($resolvedValue); |
|
|
|
|
|
|
398
|
6 |
|
$field->setType($resolvedType); |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
17 |
|
return $resolvedValue; |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* @param $field Field |
|
406
|
|
|
* @param $query Query |
|
407
|
|
|
* |
|
408
|
|
|
* @return array |
|
409
|
|
|
*/ |
|
410
|
17 |
|
public function parseArgumentsValues($field, $query) |
|
411
|
|
|
{ |
|
412
|
17 |
|
if ($query instanceof \Youshido\GraphQL\Parser\Ast\Field) { |
|
413
|
|
|
return []; |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
17 |
|
$args = []; |
|
417
|
17 |
|
foreach ($query->getArguments() as $argument) { |
|
418
|
5 |
|
if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
|
419
|
5 |
|
$args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
|
420
|
|
|
} |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
17 |
|
return $args; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
/** |
|
427
|
|
|
* @param $query Query |
|
428
|
|
|
* @param $queryType ObjectType|TypeInterface|Field |
|
429
|
|
|
* @param $resolvedValue mixed |
|
430
|
|
|
* @param $value array |
|
431
|
|
|
* |
|
432
|
|
|
* @throws \Exception |
|
433
|
|
|
* |
|
434
|
|
|
* @return array |
|
435
|
|
|
*/ |
|
436
|
17 |
|
protected function processQueryFields($query, $queryType, $resolvedValue, $value) |
|
437
|
|
|
{ |
|
438
|
17 |
|
foreach ($query->getFields() as $field) { |
|
439
|
17 |
|
if ($field instanceof FragmentReference) { |
|
440
|
1 |
|
if (!$fragment = $this->request->getFragment($field->getName())) { |
|
441
|
|
|
throw new \Exception(sprintf('Fragment reference "%s" not found', $field->getName())); |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
1 |
|
if ($fragment->getModel() !== $queryType->getName()) { |
|
445
|
|
|
throw new \Exception(sprintf('Fragment reference "%s" not found on model "%s"', $field->getName(), $queryType->getName())); |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
1 |
|
foreach ($fragment->getFields() as $fragmentField) { |
|
449
|
1 |
|
$value = $this->collectValue($value, $this->executeQuery($fragmentField, $queryType, $resolvedValue)); |
|
450
|
|
|
} |
|
451
|
|
|
} elseif ($field instanceof TypedFragmentReference) { |
|
452
|
2 |
|
if ($field->getTypeName() !== $queryType->getName()) { |
|
453
|
1 |
|
continue; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
2 |
|
foreach ($field->getFields() as $fragmentField) { |
|
457
|
2 |
|
$value = $this->collectValue($value, $this->executeQuery($fragmentField, $queryType, $resolvedValue)); |
|
|
|
|
|
|
458
|
|
|
} |
|
459
|
17 |
|
} elseif ($field->getName() == self::TYPE_NAME_QUERY) { |
|
460
|
1 |
|
$value = $this->collectValue($value, [$field->getAlias() ?: $field->getName() => $queryType->getName()]); |
|
461
|
|
|
} else { |
|
462
|
17 |
|
$value = $this->collectValue($value, $this->executeQuery($field, $queryType, $resolvedValue)); |
|
463
|
|
|
} |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
17 |
|
return $value; |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
17 |
|
protected function collectValue($value, $queryValue) |
|
470
|
|
|
{ |
|
471
|
17 |
|
if ($queryValue && is_array($queryValue)) { |
|
472
|
17 |
|
$value = array_merge(is_array($value) ? $value : [], $queryValue); |
|
473
|
|
|
} else { |
|
474
|
|
|
$value = $queryValue; |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
17 |
|
return $value; |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
19 |
|
public function getSchema() |
|
481
|
|
|
{ |
|
482
|
19 |
|
return $this->schema; |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
19 |
|
public function getResponseData() |
|
486
|
|
|
{ |
|
487
|
19 |
|
$result = []; |
|
488
|
|
|
|
|
489
|
19 |
|
if (!empty($this->data)) { |
|
490
|
17 |
|
$result['data'] = $this->data; |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
19 |
|
$this->mergeErrors($this->resolveValidator); |
|
|
|
|
|
|
494
|
19 |
|
if ($this->hasErrors()) { |
|
495
|
2 |
|
$result['errors'] = $this->getErrorsArray(); |
|
496
|
|
|
} |
|
497
|
19 |
|
$this->clearErrors(); |
|
498
|
19 |
|
$this->resolveValidator->clearErrors(); |
|
499
|
19 |
|
$this->schemaValidator->clearErrors(); |
|
500
|
|
|
|
|
501
|
19 |
|
return $result; |
|
502
|
|
|
} |
|
503
|
|
|
} |
|
504
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: