1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Validator\Rules; |
6
|
|
|
|
7
|
|
|
use GraphQL\Error\Error; |
8
|
|
|
use GraphQL\Language\AST\ArgumentNode; |
9
|
|
|
use GraphQL\Language\AST\FieldNode; |
10
|
|
|
use GraphQL\Language\AST\FragmentDefinitionNode; |
11
|
|
|
use GraphQL\Language\AST\FragmentSpreadNode; |
12
|
|
|
use GraphQL\Language\AST\InlineFragmentNode; |
13
|
|
|
use GraphQL\Language\AST\Node; |
14
|
|
|
use GraphQL\Language\AST\NodeKind; |
15
|
|
|
use GraphQL\Language\AST\SelectionSetNode; |
16
|
|
|
use GraphQL\Language\Printer; |
17
|
|
|
use GraphQL\Type\Definition\CompositeType; |
18
|
|
|
use GraphQL\Type\Definition\InterfaceType; |
19
|
|
|
use GraphQL\Type\Definition\ListOfType; |
20
|
|
|
use GraphQL\Type\Definition\NonNull; |
21
|
|
|
use GraphQL\Type\Definition\ObjectType; |
22
|
|
|
use GraphQL\Type\Definition\OutputType; |
23
|
|
|
use GraphQL\Type\Definition\Type; |
24
|
|
|
use GraphQL\Utils\PairSet; |
25
|
|
|
use GraphQL\Utils\TypeInfo; |
26
|
|
|
use GraphQL\Validator\ValidationContext; |
27
|
|
|
use function array_keys; |
28
|
|
|
use function array_map; |
29
|
|
|
use function array_merge; |
30
|
|
|
use function array_reduce; |
31
|
|
|
use function count; |
32
|
|
|
use function implode; |
33
|
|
|
use function is_array; |
34
|
|
|
use function sprintf; |
35
|
|
|
|
36
|
|
|
class OverlappingFieldsCanBeMerged extends ValidationRule |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* A memoization for when two fragments are compared "between" each other for |
40
|
|
|
* conflicts. Two fragments may be compared many times, so memoizing this can |
41
|
|
|
* dramatically improve the performance of this validator. |
42
|
|
|
* @var PairSet |
43
|
|
|
*/ |
44
|
|
|
private $comparedFragmentPairs; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* A cache for the "field map" and list of fragment names found in any given |
48
|
|
|
* selection set. Selection sets may be asked for this information multiple |
49
|
|
|
* times, so this improves the performance of this validator. |
50
|
|
|
* |
51
|
|
|
* @var \SplObjectStorage |
52
|
|
|
*/ |
53
|
|
|
private $cachedFieldsAndFragmentNames; |
54
|
|
|
|
55
|
142 |
|
public function getVisitor(ValidationContext $context) |
56
|
|
|
{ |
57
|
142 |
|
$this->comparedFragmentPairs = new PairSet(); |
58
|
142 |
|
$this->cachedFieldsAndFragmentNames = new \SplObjectStorage(); |
59
|
|
|
|
60
|
|
|
return [ |
61
|
|
|
NodeKind::SELECTION_SET => function (SelectionSetNode $selectionSet) use ($context) { |
62
|
142 |
|
$conflicts = $this->findConflictsWithinSelectionSet( |
63
|
142 |
|
$context, |
64
|
142 |
|
$context->getParentType(), |
|
|
|
|
65
|
142 |
|
$selectionSet |
66
|
|
|
); |
67
|
|
|
|
68
|
142 |
|
foreach ($conflicts as $conflict) { |
69
|
22 |
|
[[$responseName, $reason], $fields1, $fields2] = $conflict; |
|
|
|
|
70
|
|
|
|
71
|
22 |
|
$context->reportError(new Error( |
72
|
22 |
|
self::fieldsConflictMessage($responseName, $reason), |
73
|
22 |
|
array_merge($fields1, $fields2) |
74
|
|
|
)); |
75
|
|
|
} |
76
|
142 |
|
}, |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Find all conflicts found "within" a selection set, including those found |
82
|
|
|
* via spreading in fragments. Called when visiting each SelectionSet in the |
83
|
|
|
* GraphQL Document. |
84
|
|
|
* |
85
|
|
|
* @param CompositeType $parentType |
86
|
|
|
* @return mixed[] |
87
|
|
|
*/ |
88
|
142 |
|
private function findConflictsWithinSelectionSet( |
89
|
|
|
ValidationContext $context, |
90
|
|
|
$parentType, |
91
|
|
|
SelectionSetNode $selectionSet |
92
|
|
|
) { |
93
|
142 |
|
[$fieldMap, $fragmentNames] = $this->getFieldsAndFragmentNames( |
94
|
142 |
|
$context, |
95
|
142 |
|
$parentType, |
96
|
142 |
|
$selectionSet |
97
|
|
|
); |
98
|
|
|
|
99
|
142 |
|
$conflicts = []; |
100
|
|
|
|
101
|
|
|
// (A) Find find all conflicts "within" the fields of this selection set. |
102
|
|
|
// Note: this is the *only place* `collectConflictsWithin` is called. |
103
|
142 |
|
$this->collectConflictsWithin( |
104
|
142 |
|
$context, |
105
|
142 |
|
$conflicts, |
106
|
142 |
|
$fieldMap |
107
|
|
|
); |
108
|
|
|
|
109
|
142 |
|
$fragmentNamesLength = count($fragmentNames); |
110
|
142 |
|
if ($fragmentNamesLength !== 0) { |
111
|
|
|
// (B) Then collect conflicts between these fields and those represented by |
112
|
|
|
// each spread fragment name found. |
113
|
19 |
|
$comparedFragments = []; |
114
|
19 |
|
for ($i = 0; $i < $fragmentNamesLength; $i++) { |
115
|
19 |
|
$this->collectConflictsBetweenFieldsAndFragment( |
116
|
19 |
|
$context, |
117
|
19 |
|
$conflicts, |
118
|
19 |
|
$comparedFragments, |
119
|
19 |
|
false, |
120
|
19 |
|
$fieldMap, |
121
|
19 |
|
$fragmentNames[$i] |
122
|
|
|
); |
123
|
|
|
// (C) Then compare this fragment with all other fragments found in this |
124
|
|
|
// selection set to collect conflicts between fragments spread together. |
125
|
|
|
// This compares each item in the list of fragment names to every other item |
126
|
|
|
// in that same list (except for itself). |
127
|
19 |
|
for ($j = $i + 1; $j < $fragmentNamesLength; $j++) { |
128
|
4 |
|
$this->collectConflictsBetweenFragments( |
129
|
4 |
|
$context, |
130
|
4 |
|
$conflicts, |
131
|
4 |
|
false, |
132
|
4 |
|
$fragmentNames[$i], |
133
|
4 |
|
$fragmentNames[$j] |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
142 |
|
return $conflicts; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Given a selection set, return the collection of fields (a mapping of response |
144
|
|
|
* name to field ASTs and definitions) as well as a list of fragment names |
145
|
|
|
* referenced via fragment spreads. |
146
|
|
|
* |
147
|
|
|
* @param CompositeType $parentType |
148
|
|
|
* @return mixed[]|\SplObjectStorage |
149
|
|
|
*/ |
150
|
142 |
|
private function getFieldsAndFragmentNames( |
151
|
|
|
ValidationContext $context, |
152
|
|
|
$parentType, |
153
|
|
|
SelectionSetNode $selectionSet |
154
|
|
|
) { |
155
|
142 |
|
if (isset($this->cachedFieldsAndFragmentNames[$selectionSet])) { |
156
|
23 |
|
$cached = $this->cachedFieldsAndFragmentNames[$selectionSet]; |
157
|
|
|
} else { |
158
|
142 |
|
$astAndDefs = []; |
159
|
142 |
|
$fragmentNames = []; |
160
|
|
|
|
161
|
142 |
|
$this->internalCollectFieldsAndFragmentNames( |
162
|
142 |
|
$context, |
163
|
142 |
|
$parentType, |
164
|
142 |
|
$selectionSet, |
165
|
142 |
|
$astAndDefs, |
166
|
142 |
|
$fragmentNames |
167
|
|
|
); |
168
|
142 |
|
$cached = [$astAndDefs, array_keys($fragmentNames)]; |
169
|
142 |
|
$this->cachedFieldsAndFragmentNames[$selectionSet] = $cached; |
170
|
|
|
} |
171
|
|
|
|
172
|
142 |
|
return $cached; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Algorithm: |
177
|
|
|
* |
178
|
|
|
* Conflicts occur when two fields exist in a query which will produce the same |
179
|
|
|
* response name, but represent differing values, thus creating a conflict. |
180
|
|
|
* The algorithm below finds all conflicts via making a series of comparisons |
181
|
|
|
* between fields. In order to compare as few fields as possible, this makes |
182
|
|
|
* a series of comparisons "within" sets of fields and "between" sets of fields. |
183
|
|
|
* |
184
|
|
|
* Given any selection set, a collection produces both a set of fields by |
185
|
|
|
* also including all inline fragments, as well as a list of fragments |
186
|
|
|
* referenced by fragment spreads. |
187
|
|
|
* |
188
|
|
|
* A) Each selection set represented in the document first compares "within" its |
189
|
|
|
* collected set of fields, finding any conflicts between every pair of |
190
|
|
|
* overlapping fields. |
191
|
|
|
* Note: This is the *only time* that a the fields "within" a set are compared |
192
|
|
|
* to each other. After this only fields "between" sets are compared. |
193
|
|
|
* |
194
|
|
|
* B) Also, if any fragment is referenced in a selection set, then a |
195
|
|
|
* comparison is made "between" the original set of fields and the |
196
|
|
|
* referenced fragment. |
197
|
|
|
* |
198
|
|
|
* C) Also, if multiple fragments are referenced, then comparisons |
199
|
|
|
* are made "between" each referenced fragment. |
200
|
|
|
* |
201
|
|
|
* D) When comparing "between" a set of fields and a referenced fragment, first |
202
|
|
|
* a comparison is made between each field in the original set of fields and |
203
|
|
|
* each field in the the referenced set of fields. |
204
|
|
|
* |
205
|
|
|
* E) Also, if any fragment is referenced in the referenced selection set, |
206
|
|
|
* then a comparison is made "between" the original set of fields and the |
207
|
|
|
* referenced fragment (recursively referring to step D). |
208
|
|
|
* |
209
|
|
|
* F) When comparing "between" two fragments, first a comparison is made between |
210
|
|
|
* each field in the first referenced set of fields and each field in the the |
211
|
|
|
* second referenced set of fields. |
212
|
|
|
* |
213
|
|
|
* G) Also, any fragments referenced by the first must be compared to the |
214
|
|
|
* second, and any fragments referenced by the second must be compared to the |
215
|
|
|
* first (recursively referring to step F). |
216
|
|
|
* |
217
|
|
|
* H) When comparing two fields, if both have selection sets, then a comparison |
218
|
|
|
* is made "between" both selection sets, first comparing the set of fields in |
219
|
|
|
* the first selection set with the set of fields in the second. |
220
|
|
|
* |
221
|
|
|
* I) Also, if any fragment is referenced in either selection set, then a |
222
|
|
|
* comparison is made "between" the other set of fields and the |
223
|
|
|
* referenced fragment. |
224
|
|
|
* |
225
|
|
|
* J) Also, if two fragments are referenced in both selection sets, then a |
226
|
|
|
* comparison is made "between" the two fragments. |
227
|
|
|
* |
228
|
|
|
*/ |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Given a reference to a fragment, return the represented collection of fields |
232
|
|
|
* as well as a list of nested fragment names referenced via fragment spreads. |
233
|
|
|
* |
234
|
|
|
* @param CompositeType $parentType |
235
|
|
|
* @param mixed[][][] $astAndDefs |
236
|
|
|
* @param bool[] $fragmentNames |
237
|
|
|
*/ |
238
|
142 |
|
private function internalCollectFieldsAndFragmentNames( |
239
|
|
|
ValidationContext $context, |
240
|
|
|
$parentType, |
241
|
|
|
SelectionSetNode $selectionSet, |
242
|
|
|
array &$astAndDefs, |
243
|
|
|
array &$fragmentNames |
244
|
|
|
) { |
245
|
142 |
|
foreach ($selectionSet->selections as $selection) { |
246
|
|
|
switch (true) { |
247
|
142 |
|
case $selection instanceof FieldNode: |
248
|
142 |
|
$fieldName = $selection->name->value; |
249
|
142 |
|
$fieldDef = null; |
250
|
142 |
|
if ($parentType instanceof ObjectType || |
251
|
142 |
|
$parentType instanceof InterfaceType) { |
252
|
140 |
|
$tmp = $parentType->getFields(); |
253
|
140 |
|
if (isset($tmp[$fieldName])) { |
254
|
127 |
|
$fieldDef = $tmp[$fieldName]; |
255
|
|
|
} |
256
|
|
|
} |
257
|
142 |
|
$responseName = $selection->alias ? $selection->alias->value : $fieldName; |
258
|
|
|
|
259
|
142 |
|
if (! isset($astAndDefs[$responseName])) { |
260
|
142 |
|
$astAndDefs[$responseName] = []; |
261
|
|
|
} |
262
|
142 |
|
$astAndDefs[$responseName][] = [$parentType, $selection, $fieldDef]; |
263
|
142 |
|
break; |
264
|
46 |
|
case $selection instanceof FragmentSpreadNode: |
265
|
19 |
|
$fragmentNames[$selection->name->value] = true; |
266
|
19 |
|
break; |
267
|
30 |
|
case $selection instanceof InlineFragmentNode: |
268
|
30 |
|
$typeCondition = $selection->typeCondition; |
269
|
30 |
|
$inlineFragmentType = $typeCondition |
270
|
29 |
|
? TypeInfo::typeFromAST($context->getSchema(), $typeCondition) |
271
|
30 |
|
: $parentType; |
272
|
|
|
|
273
|
30 |
|
$this->internalCollectFieldsAndFragmentNames( |
274
|
30 |
|
$context, |
275
|
30 |
|
$inlineFragmentType, |
|
|
|
|
276
|
30 |
|
$selection->selectionSet, |
277
|
|
|
$astAndDefs, |
278
|
30 |
|
$fragmentNames |
279
|
|
|
); |
280
|
142 |
|
break; |
281
|
|
|
} |
282
|
|
|
} |
283
|
142 |
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Collect all Conflicts "within" one collection of fields. |
287
|
|
|
* |
288
|
|
|
* @param mixed[][] $conflicts |
289
|
|
|
* @param mixed[][] $fieldMap |
290
|
|
|
*/ |
291
|
142 |
|
private function collectConflictsWithin( |
292
|
|
|
ValidationContext $context, |
293
|
|
|
array &$conflicts, |
294
|
|
|
array $fieldMap |
295
|
|
|
) { |
296
|
|
|
// A field map is a keyed collection, where each key represents a response |
297
|
|
|
// name and the value at that key is a list of all fields which provide that |
298
|
|
|
// response name. For every response name, if there are multiple fields, they |
299
|
|
|
// must be compared to find a potential conflict. |
300
|
142 |
|
foreach ($fieldMap as $responseName => $fields) { |
301
|
|
|
// This compares every field in the list to every other field in this list |
302
|
|
|
// (except to itself). If the list only has one item, nothing needs to |
303
|
|
|
// be compared. |
304
|
142 |
|
$fieldsLength = count($fields); |
305
|
142 |
|
if ($fieldsLength <= 1) { |
306
|
132 |
|
continue; |
307
|
|
|
} |
308
|
|
|
|
309
|
33 |
|
for ($i = 0; $i < $fieldsLength; $i++) { |
310
|
33 |
|
for ($j = $i + 1; $j < $fieldsLength; $j++) { |
311
|
33 |
|
$conflict = $this->findConflict( |
312
|
33 |
|
$context, |
313
|
33 |
|
false, // within one collection is never mutually exclusive |
314
|
33 |
|
$responseName, |
315
|
33 |
|
$fields[$i], |
316
|
33 |
|
$fields[$j] |
317
|
|
|
); |
318
|
33 |
|
if (! $conflict) { |
319
|
18 |
|
continue; |
320
|
|
|
} |
321
|
|
|
|
322
|
19 |
|
$conflicts[] = $conflict; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
} |
326
|
142 |
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Determines if there is a conflict between two particular fields, including |
330
|
|
|
* comparing their sub-fields. |
331
|
|
|
* |
332
|
|
|
* @param bool $parentFieldsAreMutuallyExclusive |
333
|
|
|
* @param string $responseName |
334
|
|
|
* @param mixed[] $field1 |
335
|
|
|
* @param mixed[] $field2 |
336
|
|
|
* @return mixed[]|null |
337
|
|
|
*/ |
338
|
39 |
|
private function findConflict( |
339
|
|
|
ValidationContext $context, |
340
|
|
|
$parentFieldsAreMutuallyExclusive, |
341
|
|
|
$responseName, |
342
|
|
|
array $field1, |
343
|
|
|
array $field2 |
344
|
|
|
) { |
345
|
39 |
|
[$parentType1, $ast1, $def1] = $field1; |
346
|
39 |
|
[$parentType2, $ast2, $def2] = $field2; |
347
|
|
|
|
348
|
|
|
// If it is known that two fields could not possibly apply at the same |
349
|
|
|
// time, due to the parent types, then it is safe to permit them to diverge |
350
|
|
|
// in aliased field or arguments used as they will not present any ambiguity |
351
|
|
|
// by differing. |
352
|
|
|
// It is known that two parent types could never overlap if they are |
353
|
|
|
// different Object types. Interface or Union types might overlap - if not |
354
|
|
|
// in the current state of the schema, then perhaps in some future version, |
355
|
|
|
// thus may not safely diverge. |
356
|
|
|
$areMutuallyExclusive = |
357
|
39 |
|
$parentFieldsAreMutuallyExclusive || |
358
|
|
|
( |
359
|
39 |
|
$parentType1 !== $parentType2 && |
360
|
39 |
|
$parentType1 instanceof ObjectType && |
361
|
39 |
|
$parentType2 instanceof ObjectType |
362
|
|
|
); |
363
|
|
|
|
364
|
|
|
// The return type for each field. |
365
|
39 |
|
$type1 = $def1 ? $def1->getType() : null; |
366
|
39 |
|
$type2 = $def2 ? $def2->getType() : null; |
367
|
|
|
|
368
|
39 |
|
if (! $areMutuallyExclusive) { |
369
|
|
|
// Two aliases must refer to the same field. |
370
|
30 |
|
$name1 = $ast1->name->value; |
371
|
30 |
|
$name2 = $ast2->name->value; |
372
|
30 |
|
if ($name1 !== $name2) { |
373
|
|
|
return [ |
374
|
14 |
|
[$responseName, sprintf('%s and %s are different fields', $name1, $name2)], |
375
|
14 |
|
[$ast1], |
376
|
14 |
|
[$ast2], |
377
|
|
|
]; |
378
|
|
|
} |
379
|
|
|
|
380
|
24 |
|
if (! $this->sameArguments($ast1->arguments ?: [], $ast2->arguments ?: [])) { |
381
|
|
|
return [ |
382
|
3 |
|
[$responseName, 'they have differing arguments'], |
383
|
3 |
|
[$ast1], |
384
|
3 |
|
[$ast2], |
385
|
|
|
]; |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|
389
|
31 |
|
if ($type1 && $type2 && $this->doTypesConflict($type1, $type2)) { |
390
|
|
|
return [ |
391
|
5 |
|
[$responseName, sprintf('they return conflicting types %s and %s', $type1, $type2)], |
392
|
5 |
|
[$ast1], |
393
|
5 |
|
[$ast2], |
394
|
|
|
]; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
// Collect and compare sub-fields. Use the same "visited fragment names" list |
398
|
|
|
// for both collections so fields in a fragment reference are never |
399
|
|
|
// compared to themselves. |
400
|
27 |
|
$selectionSet1 = $ast1->selectionSet; |
401
|
27 |
|
$selectionSet2 = $ast2->selectionSet; |
402
|
27 |
|
if ($selectionSet1 && $selectionSet2) { |
403
|
12 |
|
$conflicts = $this->findConflictsBetweenSubSelectionSets( |
404
|
12 |
|
$context, |
405
|
12 |
|
$areMutuallyExclusive, |
406
|
12 |
|
Type::getNamedType($type1), |
|
|
|
|
407
|
12 |
|
$selectionSet1, |
408
|
12 |
|
Type::getNamedType($type2), |
|
|
|
|
409
|
12 |
|
$selectionSet2 |
410
|
|
|
); |
411
|
|
|
|
412
|
12 |
|
return $this->subfieldConflicts( |
413
|
12 |
|
$conflicts, |
414
|
12 |
|
$responseName, |
415
|
12 |
|
$ast1, |
416
|
12 |
|
$ast2 |
417
|
|
|
); |
418
|
|
|
} |
419
|
|
|
|
420
|
18 |
|
return null; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @param ArgumentNode[] $arguments1 |
425
|
|
|
* @param ArgumentNode[] $arguments2 |
426
|
|
|
* |
427
|
|
|
* @return bool |
428
|
|
|
*/ |
429
|
24 |
|
private function sameArguments($arguments1, $arguments2) |
430
|
|
|
{ |
431
|
24 |
|
if (count($arguments1) !== count($arguments2)) { |
432
|
2 |
|
return false; |
433
|
|
|
} |
434
|
22 |
|
foreach ($arguments1 as $argument1) { |
435
|
2 |
|
$argument2 = null; |
436
|
2 |
|
foreach ($arguments2 as $argument) { |
437
|
2 |
|
if ($argument->name->value === $argument1->name->value) { |
438
|
2 |
|
$argument2 = $argument; |
439
|
2 |
|
break; |
440
|
|
|
} |
441
|
|
|
} |
442
|
2 |
|
if (! $argument2) { |
443
|
|
|
return false; |
444
|
|
|
} |
445
|
|
|
|
446
|
2 |
|
if (! $this->sameValue($argument1->value, $argument2->value)) { |
447
|
2 |
|
return false; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|
451
|
21 |
|
return true; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* @return bool |
456
|
|
|
*/ |
457
|
2 |
|
private function sameValue(Node $value1, Node $value2) |
458
|
|
|
{ |
459
|
2 |
|
return (! $value1 && ! $value2) || (Printer::doPrint($value1) === Printer::doPrint($value2)); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Two types conflict if both types could not apply to a value simultaneously. |
464
|
|
|
* Composite types are ignored as their individual field types will be compared |
465
|
|
|
* later recursively. However List and Non-Null types must match. |
466
|
|
|
* |
467
|
|
|
* @return bool |
468
|
|
|
*/ |
469
|
23 |
|
private function doTypesConflict(OutputType $type1, OutputType $type2) |
470
|
|
|
{ |
471
|
23 |
|
if ($type1 instanceof ListOfType) { |
472
|
3 |
|
return $type2 instanceof ListOfType ? |
473
|
2 |
|
$this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : |
|
|
|
|
474
|
3 |
|
true; |
475
|
|
|
} |
476
|
23 |
|
if ($type2 instanceof ListOfType) { |
477
|
1 |
|
return $type1 instanceof ListOfType ? |
478
|
|
|
$this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : |
479
|
1 |
|
true; |
480
|
|
|
} |
481
|
22 |
|
if ($type1 instanceof NonNull) { |
482
|
2 |
|
return $type2 instanceof NonNull ? |
483
|
1 |
|
$this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : |
484
|
2 |
|
true; |
485
|
|
|
} |
486
|
21 |
|
if ($type2 instanceof NonNull) { |
487
|
1 |
|
return $type1 instanceof NonNull ? |
488
|
|
|
$this->doTypesConflict($type1->getWrappedType(), $type2->getWrappedType()) : |
489
|
1 |
|
true; |
490
|
|
|
} |
491
|
20 |
|
if (Type::isLeafType($type1) || Type::isLeafType($type2)) { |
492
|
17 |
|
return $type1 !== $type2; |
493
|
|
|
} |
494
|
|
|
|
495
|
6 |
|
return false; |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Find all conflicts found between two selection sets, including those found |
500
|
|
|
* via spreading in fragments. Called when determining if conflicts exist |
501
|
|
|
* between the sub-fields of two overlapping fields. |
502
|
|
|
* |
503
|
|
|
* @param bool $areMutuallyExclusive |
504
|
|
|
* @param CompositeType $parentType1 |
505
|
|
|
* @param CompositeType $parentType2 |
506
|
|
|
* @return mixed[][] |
507
|
|
|
*/ |
508
|
12 |
|
private function findConflictsBetweenSubSelectionSets( |
509
|
|
|
ValidationContext $context, |
510
|
|
|
$areMutuallyExclusive, |
511
|
|
|
$parentType1, |
512
|
|
|
SelectionSetNode $selectionSet1, |
513
|
|
|
$parentType2, |
514
|
|
|
SelectionSetNode $selectionSet2 |
515
|
|
|
) { |
516
|
12 |
|
$conflicts = []; |
517
|
|
|
|
518
|
12 |
|
[$fieldMap1, $fragmentNames1] = $this->getFieldsAndFragmentNames( |
519
|
12 |
|
$context, |
520
|
12 |
|
$parentType1, |
521
|
12 |
|
$selectionSet1 |
522
|
|
|
); |
523
|
12 |
|
[$fieldMap2, $fragmentNames2] = $this->getFieldsAndFragmentNames( |
524
|
12 |
|
$context, |
525
|
12 |
|
$parentType2, |
526
|
12 |
|
$selectionSet2 |
527
|
|
|
); |
528
|
|
|
|
529
|
|
|
// (H) First, collect all conflicts between these two collections of field. |
530
|
12 |
|
$this->collectConflictsBetween( |
531
|
12 |
|
$context, |
532
|
12 |
|
$conflicts, |
533
|
12 |
|
$areMutuallyExclusive, |
534
|
12 |
|
$fieldMap1, |
535
|
12 |
|
$fieldMap2 |
536
|
|
|
); |
537
|
|
|
|
538
|
|
|
// (I) Then collect conflicts between the first collection of fields and |
539
|
|
|
// those referenced by each fragment name associated with the second. |
540
|
12 |
|
$fragmentNames2Length = count($fragmentNames2); |
541
|
12 |
|
if ($fragmentNames2Length !== 0) { |
542
|
3 |
|
$comparedFragments = []; |
543
|
3 |
|
for ($j = 0; $j < $fragmentNames2Length; $j++) { |
544
|
3 |
|
$this->collectConflictsBetweenFieldsAndFragment( |
545
|
3 |
|
$context, |
546
|
3 |
|
$conflicts, |
547
|
3 |
|
$comparedFragments, |
548
|
3 |
|
$areMutuallyExclusive, |
549
|
3 |
|
$fieldMap1, |
550
|
3 |
|
$fragmentNames2[$j] |
551
|
|
|
); |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
// (I) Then collect conflicts between the second collection of fields and |
556
|
|
|
// those referenced by each fragment name associated with the first. |
557
|
12 |
|
$fragmentNames1Length = count($fragmentNames1); |
558
|
12 |
|
if ($fragmentNames1Length !== 0) { |
559
|
3 |
|
$comparedFragments = []; |
560
|
3 |
|
for ($i = 0; $i < $fragmentNames1Length; $i++) { |
561
|
3 |
|
$this->collectConflictsBetweenFieldsAndFragment( |
562
|
3 |
|
$context, |
563
|
3 |
|
$conflicts, |
564
|
3 |
|
$comparedFragments, |
565
|
3 |
|
$areMutuallyExclusive, |
566
|
3 |
|
$fieldMap2, |
567
|
3 |
|
$fragmentNames1[$i] |
568
|
|
|
); |
569
|
|
|
} |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
// (J) Also collect conflicts between any fragment names by the first and |
573
|
|
|
// fragment names by the second. This compares each item in the first set of |
574
|
|
|
// names to each item in the second set of names. |
575
|
12 |
|
for ($i = 0; $i < $fragmentNames1Length; $i++) { |
576
|
3 |
|
for ($j = 0; $j < $fragmentNames2Length; $j++) { |
577
|
3 |
|
$this->collectConflictsBetweenFragments( |
578
|
3 |
|
$context, |
579
|
3 |
|
$conflicts, |
580
|
3 |
|
$areMutuallyExclusive, |
581
|
3 |
|
$fragmentNames1[$i], |
582
|
3 |
|
$fragmentNames2[$j] |
583
|
|
|
); |
584
|
|
|
} |
585
|
|
|
} |
586
|
|
|
|
587
|
12 |
|
return $conflicts; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* Collect all Conflicts between two collections of fields. This is similar to, |
592
|
|
|
* but different from the `collectConflictsWithin` function above. This check |
593
|
|
|
* assumes that `collectConflictsWithin` has already been called on each |
594
|
|
|
* provided collection of fields. This is true because this validator traverses |
595
|
|
|
* each individual selection set. |
596
|
|
|
* |
597
|
|
|
* @param mixed[][] $conflicts |
598
|
|
|
* @param bool $parentFieldsAreMutuallyExclusive |
599
|
|
|
* @param mixed[] $fieldMap1 |
600
|
|
|
* @param mixed[] $fieldMap2 |
601
|
|
|
*/ |
602
|
24 |
|
private function collectConflictsBetween( |
603
|
|
|
ValidationContext $context, |
604
|
|
|
array &$conflicts, |
605
|
|
|
$parentFieldsAreMutuallyExclusive, |
606
|
|
|
array $fieldMap1, |
607
|
|
|
array $fieldMap2 |
608
|
|
|
) { |
609
|
|
|
// A field map is a keyed collection, where each key represents a response |
610
|
|
|
// name and the value at that key is a list of all fields which provide that |
611
|
|
|
// response name. For any response name which appears in both provided field |
612
|
|
|
// maps, each field from the first field map must be compared to every field |
613
|
|
|
// in the second field map to find potential conflicts. |
614
|
24 |
|
foreach ($fieldMap1 as $responseName => $fields1) { |
615
|
19 |
|
if (! isset($fieldMap2[$responseName])) { |
616
|
7 |
|
continue; |
617
|
|
|
} |
618
|
|
|
|
619
|
16 |
|
$fields2 = $fieldMap2[$responseName]; |
620
|
16 |
|
$fields1Length = count($fields1); |
621
|
16 |
|
$fields2Length = count($fields2); |
622
|
16 |
|
for ($i = 0; $i < $fields1Length; $i++) { |
623
|
16 |
|
for ($j = 0; $j < $fields2Length; $j++) { |
624
|
16 |
|
$conflict = $this->findConflict( |
625
|
16 |
|
$context, |
626
|
16 |
|
$parentFieldsAreMutuallyExclusive, |
627
|
16 |
|
$responseName, |
628
|
16 |
|
$fields1[$i], |
629
|
16 |
|
$fields2[$j] |
630
|
|
|
); |
631
|
16 |
|
if (! $conflict) { |
632
|
8 |
|
continue; |
633
|
|
|
} |
634
|
|
|
|
635
|
11 |
|
$conflicts[] = $conflict; |
636
|
|
|
} |
637
|
|
|
} |
638
|
|
|
} |
639
|
24 |
|
} |
640
|
|
|
|
641
|
|
|
/** |
642
|
|
|
* Collect all conflicts found between a set of fields and a fragment reference |
643
|
|
|
* including via spreading in any nested fragments. |
644
|
|
|
* |
645
|
|
|
* @param mixed[][] $conflicts |
646
|
|
|
* @param bool[] $comparedFragments |
647
|
|
|
* @param bool $areMutuallyExclusive |
648
|
|
|
* @param mixed[][] $fieldMap |
649
|
|
|
* @param string $fragmentName |
650
|
|
|
*/ |
651
|
19 |
|
private function collectConflictsBetweenFieldsAndFragment( |
652
|
|
|
ValidationContext $context, |
653
|
|
|
array &$conflicts, |
654
|
|
|
array &$comparedFragments, |
655
|
|
|
$areMutuallyExclusive, |
656
|
|
|
array $fieldMap, |
657
|
|
|
$fragmentName |
658
|
|
|
) { |
659
|
19 |
|
if (isset($comparedFragments[$fragmentName])) { |
660
|
|
|
return; |
661
|
|
|
} |
662
|
19 |
|
$comparedFragments[$fragmentName] = true; |
663
|
|
|
|
664
|
19 |
|
$fragment = $context->getFragment($fragmentName); |
665
|
19 |
|
if (! $fragment) { |
666
|
1 |
|
return; |
667
|
|
|
} |
668
|
|
|
|
669
|
19 |
|
[$fieldMap2, $fragmentNames2] = $this->getReferencedFieldsAndFragmentNames( |
670
|
19 |
|
$context, |
671
|
19 |
|
$fragment |
672
|
|
|
); |
673
|
|
|
|
674
|
19 |
|
if ($fieldMap === $fieldMap2) { |
675
|
3 |
|
return; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
// (D) First collect any conflicts between the provided collection of fields |
679
|
|
|
// and the collection of fields represented by the given fragment. |
680
|
17 |
|
$this->collectConflictsBetween( |
681
|
17 |
|
$context, |
682
|
|
|
$conflicts, |
683
|
17 |
|
$areMutuallyExclusive, |
684
|
17 |
|
$fieldMap, |
685
|
17 |
|
$fieldMap2 |
686
|
|
|
); |
687
|
|
|
|
688
|
|
|
// (E) Then collect any conflicts between the provided collection of fields |
689
|
|
|
// and any fragment names found in the given fragment. |
690
|
17 |
|
$fragmentNames2Length = count($fragmentNames2); |
691
|
17 |
|
for ($i = 0; $i < $fragmentNames2Length; $i++) { |
692
|
3 |
|
$this->collectConflictsBetweenFieldsAndFragment( |
693
|
3 |
|
$context, |
694
|
|
|
$conflicts, |
695
|
|
|
$comparedFragments, |
696
|
3 |
|
$areMutuallyExclusive, |
697
|
3 |
|
$fieldMap, |
698
|
3 |
|
$fragmentNames2[$i] |
699
|
|
|
); |
700
|
|
|
} |
701
|
17 |
|
} |
702
|
|
|
|
703
|
|
|
/** |
704
|
|
|
* Given a reference to a fragment, return the represented collection of fields |
705
|
|
|
* as well as a list of nested fragment names referenced via fragment spreads. |
706
|
|
|
* |
707
|
|
|
* @return mixed[]|\SplObjectStorage |
708
|
|
|
*/ |
709
|
19 |
|
private function getReferencedFieldsAndFragmentNames( |
710
|
|
|
ValidationContext $context, |
711
|
|
|
FragmentDefinitionNode $fragment |
712
|
|
|
) { |
713
|
|
|
// Short-circuit building a type from the AST if possible. |
714
|
19 |
|
if (isset($this->cachedFieldsAndFragmentNames[$fragment->selectionSet])) { |
715
|
14 |
|
return $this->cachedFieldsAndFragmentNames[$fragment->selectionSet]; |
716
|
|
|
} |
717
|
|
|
|
718
|
16 |
|
$fragmentType = TypeInfo::typeFromAST($context->getSchema(), $fragment->typeCondition); |
719
|
|
|
|
720
|
16 |
|
return $this->getFieldsAndFragmentNames( |
721
|
16 |
|
$context, |
722
|
16 |
|
$fragmentType, |
|
|
|
|
723
|
16 |
|
$fragment->selectionSet |
724
|
|
|
); |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
/** |
728
|
|
|
* Collect all conflicts found between two fragments, including via spreading in |
729
|
|
|
* any nested fragments. |
730
|
|
|
* |
731
|
|
|
* @param mixed[][] $conflicts |
732
|
|
|
* @param bool $areMutuallyExclusive |
733
|
|
|
* @param string $fragmentName1 |
734
|
|
|
* @param string $fragmentName2 |
735
|
|
|
*/ |
736
|
7 |
|
private function collectConflictsBetweenFragments( |
737
|
|
|
ValidationContext $context, |
738
|
|
|
array &$conflicts, |
739
|
|
|
$areMutuallyExclusive, |
740
|
|
|
$fragmentName1, |
741
|
|
|
$fragmentName2 |
742
|
|
|
) { |
743
|
|
|
// No need to compare a fragment to itself. |
744
|
7 |
|
if ($fragmentName1 === $fragmentName2) { |
745
|
1 |
|
return; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
// Memoize so two fragments are not compared for conflicts more than once. |
749
|
6 |
|
if ($this->comparedFragmentPairs->has( |
750
|
6 |
|
$fragmentName1, |
751
|
6 |
|
$fragmentName2, |
752
|
6 |
|
$areMutuallyExclusive |
753
|
|
|
) |
754
|
|
|
) { |
755
|
3 |
|
return; |
756
|
|
|
} |
757
|
6 |
|
$this->comparedFragmentPairs->add( |
758
|
6 |
|
$fragmentName1, |
759
|
6 |
|
$fragmentName2, |
760
|
6 |
|
$areMutuallyExclusive |
761
|
|
|
); |
762
|
|
|
|
763
|
6 |
|
$fragment1 = $context->getFragment($fragmentName1); |
764
|
6 |
|
$fragment2 = $context->getFragment($fragmentName2); |
765
|
6 |
|
if (! $fragment1 || ! $fragment2) { |
766
|
1 |
|
return; |
767
|
|
|
} |
768
|
|
|
|
769
|
5 |
|
[$fieldMap1, $fragmentNames1] = $this->getReferencedFieldsAndFragmentNames( |
770
|
5 |
|
$context, |
771
|
5 |
|
$fragment1 |
772
|
|
|
); |
773
|
5 |
|
[$fieldMap2, $fragmentNames2] = $this->getReferencedFieldsAndFragmentNames( |
774
|
5 |
|
$context, |
775
|
5 |
|
$fragment2 |
776
|
|
|
); |
777
|
|
|
|
778
|
|
|
// (F) First, collect all conflicts between these two collections of fields |
779
|
|
|
// (not including any nested fragments). |
780
|
5 |
|
$this->collectConflictsBetween( |
781
|
5 |
|
$context, |
782
|
|
|
$conflicts, |
783
|
5 |
|
$areMutuallyExclusive, |
784
|
5 |
|
$fieldMap1, |
785
|
5 |
|
$fieldMap2 |
786
|
|
|
); |
787
|
|
|
|
788
|
|
|
// (G) Then collect conflicts between the first fragment and any nested |
789
|
|
|
// fragments spread in the second fragment. |
790
|
5 |
|
$fragmentNames2Length = count($fragmentNames2); |
791
|
5 |
|
for ($j = 0; $j < $fragmentNames2Length; $j++) { |
792
|
1 |
|
$this->collectConflictsBetweenFragments( |
793
|
1 |
|
$context, |
794
|
|
|
$conflicts, |
795
|
1 |
|
$areMutuallyExclusive, |
796
|
1 |
|
$fragmentName1, |
797
|
1 |
|
$fragmentNames2[$j] |
798
|
|
|
); |
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
// (G) Then collect conflicts between the second fragment and any nested |
802
|
|
|
// fragments spread in the first fragment. |
803
|
5 |
|
$fragmentNames1Length = count($fragmentNames1); |
804
|
5 |
|
for ($i = 0; $i < $fragmentNames1Length; $i++) { |
805
|
1 |
|
$this->collectConflictsBetweenFragments( |
806
|
1 |
|
$context, |
807
|
|
|
$conflicts, |
808
|
1 |
|
$areMutuallyExclusive, |
809
|
1 |
|
$fragmentNames1[$i], |
810
|
1 |
|
$fragmentName2 |
811
|
|
|
); |
812
|
|
|
} |
813
|
5 |
|
} |
814
|
|
|
|
815
|
|
|
/** |
816
|
|
|
* Given a series of Conflicts which occurred between two sub-fields, generate |
817
|
|
|
* a single Conflict. |
818
|
|
|
* |
819
|
|
|
* @param mixed[][] $conflicts |
820
|
|
|
* @param string $responseName |
821
|
|
|
* @return mixed[]|null |
822
|
|
|
*/ |
823
|
12 |
|
private function subfieldConflicts( |
824
|
|
|
array $conflicts, |
825
|
|
|
$responseName, |
826
|
|
|
FieldNode $ast1, |
827
|
|
|
FieldNode $ast2 |
828
|
|
|
) { |
829
|
12 |
|
if (count($conflicts) === 0) { |
830
|
6 |
|
return null; |
831
|
|
|
} |
832
|
|
|
|
833
|
|
|
return [ |
834
|
|
|
[ |
835
|
9 |
|
$responseName, |
836
|
9 |
|
array_map( |
837
|
|
|
function ($conflict) { |
838
|
9 |
|
return $conflict[0]; |
839
|
9 |
|
}, |
840
|
9 |
|
$conflicts |
841
|
|
|
), |
842
|
|
|
], |
843
|
9 |
|
array_reduce( |
844
|
9 |
|
$conflicts, |
845
|
|
|
function ($allFields, $conflict) { |
846
|
9 |
|
return array_merge($allFields, $conflict[1]); |
847
|
9 |
|
}, |
848
|
9 |
|
[$ast1] |
849
|
|
|
), |
850
|
9 |
|
array_reduce( |
851
|
9 |
|
$conflicts, |
852
|
|
|
function ($allFields, $conflict) { |
853
|
9 |
|
return array_merge($allFields, $conflict[2]); |
854
|
9 |
|
}, |
855
|
9 |
|
[$ast2] |
856
|
|
|
), |
857
|
|
|
]; |
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
/** |
861
|
|
|
* @param string $responseName |
862
|
|
|
* @param string $reason |
863
|
|
|
*/ |
864
|
23 |
|
public static function fieldsConflictMessage($responseName, $reason) |
865
|
|
|
{ |
866
|
23 |
|
$reasonMessage = self::reasonMessage($reason); |
867
|
|
|
|
868
|
23 |
|
return sprintf( |
869
|
23 |
|
'Fields "%s" conflict because %s. Use different aliases on the fields to fetch both if this was intentional.', |
870
|
23 |
|
$responseName, |
871
|
23 |
|
$reasonMessage |
872
|
|
|
); |
873
|
|
|
} |
874
|
|
|
|
875
|
23 |
|
public static function reasonMessage($reason) |
876
|
|
|
{ |
877
|
23 |
|
if (is_array($reason)) { |
878
|
9 |
|
$tmp = array_map( |
879
|
|
|
function ($tmp) { |
880
|
9 |
|
[$responseName, $subReason] = $tmp; |
881
|
|
|
|
882
|
9 |
|
$reasonMessage = self::reasonMessage($subReason); |
883
|
|
|
|
884
|
9 |
|
return sprintf('subfields "%s" conflict because %s', $responseName, $reasonMessage); |
885
|
9 |
|
}, |
886
|
9 |
|
$reason |
887
|
|
|
); |
888
|
|
|
|
889
|
9 |
|
return implode(' and ', $tmp); |
890
|
|
|
} |
891
|
|
|
|
892
|
23 |
|
return $reason; |
893
|
|
|
} |
894
|
|
|
} |
895
|
|
|
|