1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Validation\Rule; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\ValidationException; |
6
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
7
|
|
|
use Digia\GraphQL\Language\Node\SelectionSetNode; |
8
|
|
|
use Digia\GraphQL\Validation\Conflict\FindsConflictsTrait; |
9
|
|
|
use Digia\GraphQL\Validation\Conflict\Map; |
10
|
|
|
use Digia\GraphQL\Validation\Conflict\PairSet; |
11
|
|
|
use function Digia\GraphQL\Validation\fieldsConflictMessage; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Overlapping fields can be merged |
15
|
|
|
* |
16
|
|
|
* A selection set is only valid if all fields (including spreading any |
17
|
|
|
* fragments) either correspond to distinct response names or can be merged |
18
|
|
|
* without ambiguity. |
19
|
|
|
*/ |
20
|
|
|
class OverlappingFieldsCanBeMergedRule extends AbstractRule |
21
|
|
|
{ |
22
|
|
|
use FindsConflictsTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* OverlappingFieldsCanBeMergedRule constructor. |
26
|
|
|
*/ |
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->cachedFieldsAndFragmentNames = new Map(); |
30
|
|
|
$this->comparedFragmentPairs = new PairSet(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function enterNode(NodeInterface $node): ?NodeInterface |
38
|
|
|
{ |
39
|
|
|
if ($node instanceof SelectionSetNode) { |
40
|
|
|
$parentType = $this->validationContext->getParentType(); |
41
|
|
|
$conflicts = $this->findConflictsWithinSelectionSet( |
42
|
|
|
$this->cachedFieldsAndFragmentNames, |
43
|
|
|
$this->comparedFragmentPairs, |
44
|
|
|
$node, |
45
|
|
|
$parentType |
|
|
|
|
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
foreach ($conflicts as $conflict) { |
49
|
|
|
$this->validationContext->reportError( |
50
|
|
|
new ValidationException( |
51
|
|
|
fieldsConflictMessage($conflict->getResponseName(), $conflict->getReason()), |
52
|
|
|
$conflict->getAllFields() |
53
|
|
|
) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $node; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|