1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Validation\Rule; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\ValidationException; |
6
|
|
|
use Digia\GraphQL\Language\AST\Node\FragmentDefinitionNode; |
7
|
|
|
use Digia\GraphQL\Language\AST\Node\InlineFragmentNode; |
8
|
|
|
use Digia\GraphQL\Language\AST\Node\NodeInterface; |
9
|
|
|
use Digia\GraphQL\Type\Definition\CompositeTypeInterface; |
10
|
|
|
use function Digia\GraphQL\Util\typeFromAST; |
11
|
|
|
|
12
|
|
|
class FragmentsOnCompositeTypesRule extends AbstractRule |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @inheritdoc |
16
|
|
|
*/ |
17
|
|
|
public function enterNode(NodeInterface $node): ?NodeInterface |
18
|
|
|
{ |
19
|
|
|
if ($node instanceof InlineFragmentNode) { |
20
|
|
|
$this->validateFragementNode($node, function (NodeInterface $node) { |
21
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
22
|
|
|
return inlineFragmentOnNonCompositeMessage((string)$node->getTypeCondition()); |
|
|
|
|
23
|
|
|
}); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if ($node instanceof FragmentDefinitionNode) { |
27
|
|
|
$this->validateFragementNode($node, function (NodeInterface $node) { |
28
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
29
|
|
|
return fragmentOnNonCompositeMessage((string)$node, (string)$node->getTypeCondition()); |
30
|
|
|
}); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $node; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param NodeInterface|InlineFragmentNode|FragmentDefinitionNode $node |
38
|
|
|
* @param callable $errorMessageFunction |
39
|
|
|
* @throws \Exception |
40
|
|
|
* @throws \TypeError |
41
|
|
|
*/ |
42
|
|
|
protected function validateFragementNode(NodeInterface $node, callable $errorMessageFunction) |
43
|
|
|
{ |
44
|
|
|
$typeCondition = $node->getTypeCondition(); |
45
|
|
|
|
46
|
|
|
if (null !== $typeCondition) { |
47
|
|
|
$type = typeFromAST($this->context->getSchema(), $typeCondition); |
48
|
|
|
|
49
|
|
|
if (null !== $type && !($type instanceof CompositeTypeInterface)) { |
50
|
|
|
$this->context->reportError(new ValidationException($errorMessageFunction($node), [$typeCondition])); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|