-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathResolveInfo.php
459 lines (419 loc) · 13.2 KB
/
ResolveInfo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php declare(strict_types=1);
namespace GraphQL\Type\Definition;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Values;
use GraphQL\Language\AST\FieldNode;
use GraphQL\Language\AST\FragmentDefinitionNode;
use GraphQL\Language\AST\FragmentSpreadNode;
use GraphQL\Language\AST\InlineFragmentNode;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\SelectionSetNode;
use GraphQL\Type\Introspection;
use GraphQL\Type\Schema;
/**
* Structure containing information useful for field resolution process.
*
* Passed as 4th argument to every field resolver. See [docs on field resolving (data fetching)](data-fetching.md).
*
* @phpstan-import-type QueryPlanOptions from QueryPlan
*
* @phpstan-type Path list<string|int>
*/
class ResolveInfo
{
/**
* The definition of the field being resolved.
*
* @api
*/
public FieldDefinition $fieldDefinition;
/**
* The name of the field being resolved.
*
* @api
*/
public string $fieldName;
/**
* Expected return type of the field being resolved.
*
* @api
*/
public Type $returnType;
/**
* AST of all nodes referencing this field in the query.
*
* @api
*
* @var \ArrayObject<int, FieldNode>
*/
public \ArrayObject $fieldNodes;
/**
* Parent type of the field being resolved.
*
* @api
*/
public ObjectType $parentType;
/**
* Path to this field from the very root value. When fields are aliased, the path includes aliases.
*
* @api
*
* @var list<string|int>
*
* @phpstan-var Path
*/
public array $path;
/**
* Path to this field from the very root value. This will never include aliases.
*
* @api
*
* @var list<string|int>
*
* @phpstan-var Path
*/
public array $unaliasedPath;
/**
* Instance of a schema used for execution.
*
* @api
*/
public Schema $schema;
/**
* AST of all fragments defined in query.
*
* @api
*
* @var array<string, FragmentDefinitionNode>
*/
public array $fragments = [];
/**
* Root value passed to query execution.
*
* @api
*
* @var mixed
*/
public $rootValue;
/**
* AST of operation definition node (query, mutation).
*
* @api
*/
public OperationDefinitionNode $operation;
/**
* Array of variables passed to query execution.
*
* @api
*
* @var array<string, mixed>
*/
public array $variableValues = [];
/**
* @param \ArrayObject<int, FieldNode> $fieldNodes
* @param list<string|int> $path
*
* @phpstan-param Path $path
*
* @param array<string, FragmentDefinitionNode> $fragments
* @param mixed|null $rootValue
* @param array<string, mixed> $variableValues
* @param list<string|int> $unaliasedPath
*
* @phpstan-param Path $unaliasedPath
*/
public function __construct(
FieldDefinition $fieldDefinition,
\ArrayObject $fieldNodes,
ObjectType $parentType,
array $path,
Schema $schema,
array $fragments,
$rootValue,
OperationDefinitionNode $operation,
array $variableValues,
array $unaliasedPath = []
) {
$this->fieldDefinition = $fieldDefinition;
$this->fieldName = $fieldDefinition->name;
$this->returnType = $fieldDefinition->getType();
$this->fieldNodes = $fieldNodes;
$this->parentType = $parentType;
$this->path = $path;
$this->unaliasedPath = $unaliasedPath;
$this->schema = $schema;
$this->fragments = $fragments;
$this->rootValue = $rootValue;
$this->operation = $operation;
$this->variableValues = $variableValues;
}
/**
* Returns names of all fields selected in query for `$this->fieldName` up to `$depth` levels.
*
* Example:
* {
* root {
* id
* nested {
* nested1
* nested2 {
* nested3
* }
* }
* }
* }
*
* Given this ResolveInfo instance is a part of root field resolution, and $depth === 1,
* this method will return:
* [
* 'id' => true,
* 'nested' => [
* 'nested1' => true,
* 'nested2' => true,
* ],
* ]
*
* This method does not consider conditional typed fragments.
* Use it with care for fields of interface and union types.
*
* @param int $depth How many levels to include in the output beyond the first
*
* @return array<string, mixed>
*
* @api
*/
public function getFieldSelection(int $depth = 0): array
{
$fields = [];
foreach ($this->fieldNodes as $fieldNode) {
$selectionSet = $fieldNode->selectionSet;
if ($selectionSet !== null) {
$fields = \array_merge_recursive(
$fields,
$this->foldSelectionSet($selectionSet, $depth)
);
}
}
return $fields;
}
/**
* Returns names and args of all fields selected in query for `$this->fieldName` up to `$depth` levels, including aliases.
*
* The result maps original field names to a map of selections for that field, including aliases.
* For each of those selections, you can find the following keys:
* - "args" contains the passed arguments for this field/alias
* - "selectionSet" contains potential nested fields of this field/alias. The structure is recursive from here.
*
* Example:
* {
* root {
* id
* nested {
* nested1(myArg: 1)
* nested1Bis: nested1
* }
* alias1: nested {
* nested1(myArg: 2, mySecondAg: "test")
* }
* }
* }
*
* Given this ResolveInfo instance is a part of "root" field resolution, and $depth === 1,
* this method will return:
* [
* 'id' => [
* 'id' => [
* 'args' => [],
* ],
* ],
* 'nested' => [
* 'nested' => [
* 'args' => [],
* 'selectionSet' => [
* 'nested1' => [
* 'nested1' => [
* 'args' => [
* 'myArg' => 1,
* ],
* ],
* 'nested1Bis' => [
* 'args' => [],
* ],
* ],
* ],
* ],
* 'alias1' => [
* 'args' => [],
* 'selectionSet' => [
* 'nested1' => [
* 'nested1' => [
* 'args' => [
* 'myArg' => 2,
* 'mySecondAg' => "test,
* ],
* ],
* ],
* ],
* ],
* ],
* ]
*
* This method does not consider conditional typed fragments.
* Use it with care for fields of interface and union types.
* You can still alias the union type fields with the same name in order to extract their corresponding args.
*
* Example:
* {
* root {
* id
* unionPerson {
* ...on Child {
* name
* birthdate(format: "d/m/Y")
* }
* ...on Adult {
* adultName: name
* adultBirthDate: birthdate(format: "Y-m-d")
* job
* }
* }
* }
* }
*
* @param int $depth How many levels to include in the output beyond the first
*
* @throws \Exception
* @throws Error
* @throws InvariantViolation
*
* @return array<string, mixed>
*
* @api
*/
public function getFieldSelectionWithAliases(int $depth = 0): array
{
$fields = [];
foreach ($this->fieldNodes as $fieldNode) {
$selectionSet = $fieldNode->selectionSet;
if ($selectionSet !== null) {
$fieldType = $this->parentType->getField($fieldNode->name->value)
->getType();
$fields = \array_merge_recursive(
$fields,
$this->foldSelectionWithAlias($selectionSet, $depth, $fieldType)
);
}
}
return $fields;
}
/**
* @param QueryPlanOptions $options
*
* @throws \Exception
* @throws Error
* @throws InvariantViolation
*/
public function lookAhead(array $options = []): QueryPlan
{
return new QueryPlan(
$this->parentType,
$this->schema,
$this->fieldNodes,
$this->variableValues,
$this->fragments,
$options
);
}
/** @return array<string, bool> */
private function foldSelectionSet(SelectionSetNode $selectionSet, int $descend): array
{
/** @var array<string, bool> $fields */
$fields = [];
foreach ($selectionSet->selections as $selection) {
if ($selection instanceof FieldNode) {
$fields[$selection->name->value] = $descend > 0 && $selection->selectionSet !== null
? \array_merge_recursive(
$fields[$selection->name->value] ?? [],
$this->foldSelectionSet($selection->selectionSet, $descend - 1)
)
: true;
} elseif ($selection instanceof FragmentSpreadNode) {
$spreadName = $selection->name->value;
$fragment = $this->fragments[$spreadName] ?? null;
if ($fragment === null) {
continue;
}
$fields = \array_merge_recursive(
$this->foldSelectionSet($fragment->selectionSet, $descend),
$fields
);
} elseif ($selection instanceof InlineFragmentNode) {
$fields = \array_merge_recursive(
$this->foldSelectionSet($selection->selectionSet, $descend),
$fields
);
}
}
return $fields;
}
/**
* @throws \Exception
* @throws Error
* @throws InvariantViolation
*
* @return array<string>
*/
private function foldSelectionWithAlias(SelectionSetNode $selectionSet, int $descend, Type $parentType): array
{
/** @var array<string, bool> $fields */
$fields = [];
if ($parentType instanceof WrappingType) {
$parentType = $parentType->getInnermostType();
}
foreach ($selectionSet->selections as $selection) {
if ($selection instanceof FieldNode) {
$fieldName = $selection->name->value;
$aliasName = $selection->alias->value ?? $fieldName;
if ($fieldName === Introspection::TYPE_NAME_FIELD_NAME) {
continue;
}
assert($parentType instanceof HasFieldsType, 'ensured by query validation');
$fieldDef = $parentType->getField($fieldName);
$fieldType = $fieldDef->getType();
$fields[$fieldName][$aliasName]['args'] = Values::getArgumentValues($fieldDef, $selection, $this->variableValues);
if ($descend <= 0) {
continue;
}
$nestedSelectionSet = $selection->selectionSet;
if ($nestedSelectionSet === null) {
continue;
}
$fields[$fieldName][$aliasName]['selectionSet'] = $this->foldSelectionWithAlias($nestedSelectionSet, $descend - 1, $fieldType);
} elseif ($selection instanceof FragmentSpreadNode) {
$spreadName = $selection->name->value;
$fragment = $this->fragments[$spreadName] ?? null;
if ($fragment === null) {
continue;
}
$fieldType = $this->schema->getType($fragment->typeCondition->name->value);
assert($fieldType instanceof Type, 'ensured by query validation');
$fields = \array_merge_recursive(
$this->foldSelectionWithAlias($fragment->selectionSet, $descend, $fieldType),
$fields
);
} elseif ($selection instanceof InlineFragmentNode) {
$typeCondition = $selection->typeCondition;
$fieldType = $typeCondition === null
? $parentType
: $this->schema->getType($typeCondition->name->value);
assert($fieldType instanceof Type, 'ensured by query validation');
$fields = \array_merge_recursive(
$this->foldSelectionWithAlias($selection->selectionSet, $descend, $fieldType),
$fields
);
}
}
return $fields;
}
}