1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Validation\Rule; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\InvalidTypeException; |
6
|
|
|
use Digia\GraphQL\Error\ValidationException; |
7
|
|
|
use Digia\GraphQL\Language\Node\FragmentSpreadNode; |
8
|
|
|
use Digia\GraphQL\Language\Node\InlineFragmentNode; |
9
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
10
|
|
|
use Digia\GraphQL\Type\Definition\CompositeTypeInterface; |
11
|
|
|
use Digia\GraphQL\Type\Definition\TypeInterface; |
12
|
|
|
use function Digia\GraphQL\Util\doTypesOverlap; |
13
|
|
|
use function Digia\GraphQL\Util\typeFromAST; |
14
|
|
|
use function Digia\GraphQL\Validation\typeIncompatibleAnonymousSpreadMessage; |
15
|
|
|
use function Digia\GraphQL\Validation\typeIncompatibleSpreadMessage; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Possible fragment spread |
19
|
|
|
* |
20
|
|
|
* A fragment spread is only valid if the type condition could ever possibly |
21
|
|
|
* be true: if there is a non-empty intersection of the possible parent types, |
22
|
|
|
* and possible types which pass the type condition. |
23
|
|
|
*/ |
24
|
|
|
class PossibleFragmentSpreadsRule extends AbstractRule |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
public function enterNode(NodeInterface $node): ?NodeInterface |
30
|
|
|
{ |
31
|
|
|
if ($node instanceof InlineFragmentNode) { |
32
|
|
|
$fragmentType = $this->validationContext->getType(); |
33
|
|
|
$parentType = $this->validationContext->getParentType(); |
34
|
|
|
|
35
|
|
|
if ($fragmentType instanceof CompositeTypeInterface && |
36
|
|
|
$parentType instanceof CompositeTypeInterface && |
37
|
|
|
!doTypesOverlap($this->validationContext->getSchema(), $fragmentType, $parentType)) { |
38
|
|
|
$this->validationContext->reportError( |
39
|
|
|
new ValidationException( |
40
|
|
|
typeIncompatibleAnonymousSpreadMessage($parentType, $fragmentType), |
|
|
|
|
41
|
|
|
[$node] |
42
|
|
|
) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($node instanceof FragmentSpreadNode) { |
48
|
|
|
$fragmentName = $node->getNameValue(); |
49
|
|
|
$fragmentType = $this->getFragmentType($fragmentName); |
50
|
|
|
$parentType = $this->validationContext->getParentType(); |
51
|
|
|
|
52
|
|
|
if (null !== $fragmentType && |
53
|
|
|
null !== $parentType && |
54
|
|
|
!doTypesOverlap($this->validationContext->getSchema(), $fragmentType, $parentType)) { |
55
|
|
|
$this->validationContext->reportError( |
56
|
|
|
new ValidationException( |
57
|
|
|
typeIncompatibleSpreadMessage($fragmentName, $parentType, $fragmentType), |
|
|
|
|
58
|
|
|
[$node] |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $node; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* @return TypeInterface|null |
70
|
|
|
* @throws InvalidTypeException |
71
|
|
|
*/ |
72
|
|
|
protected function getFragmentType(string $name): ?TypeInterface |
73
|
|
|
{ |
74
|
|
|
$fragment = $this->validationContext->getFragment($name); |
75
|
|
|
|
76
|
|
|
if (null === $fragment) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$type = typeFromAST($this->validationContext->getSchema(), $fragment->getTypeCondition()); |
81
|
|
|
|
82
|
|
|
return $type instanceof CompositeTypeInterface ? $type : null; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|