1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Validation\Rule; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\ValidationException; |
6
|
|
|
use Digia\GraphQL\Language\Node\ArgumentNode; |
7
|
|
|
use Digia\GraphQL\Language\Node\DirectiveNode; |
8
|
|
|
use Digia\GraphQL\Language\Node\FieldNode; |
9
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
10
|
|
|
use Digia\GraphQL\Type\Definition\Argument; |
11
|
|
|
use function Digia\GraphQL\Util\suggestionList; |
12
|
|
|
use function Digia\GraphQL\Validation\unknownArgumentMessage; |
13
|
|
|
use function Digia\GraphQL\Validation\unknownDirectiveArgumentMessage; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Known argument names |
17
|
|
|
* |
18
|
|
|
* A GraphQL field is only valid if all supplied arguments are defined by |
19
|
|
|
* that field. |
20
|
|
|
*/ |
21
|
|
|
class KnownArgumentNamesRule extends AbstractRule |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
protected function enterArgument(ArgumentNode $node): ?NodeInterface |
28
|
|
|
{ |
29
|
|
|
$argumentDefinition = $this->context->getArgument(); |
30
|
|
|
|
31
|
|
|
if (null === $argumentDefinition) { |
32
|
|
|
$argumentOf = $node->getAncestor(); |
33
|
|
|
|
34
|
|
|
if ($argumentOf instanceof FieldNode) { |
35
|
|
|
return $this->validateField($node); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($argumentOf instanceof DirectiveNode) { |
39
|
|
|
return $this->validateDirective($node); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $node; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param NodeInterface $node |
48
|
|
|
* |
49
|
|
|
* @return NodeInterface|null |
50
|
|
|
*/ |
51
|
|
|
protected function validateField(NodeInterface $node): ?NodeInterface |
52
|
|
|
{ |
53
|
|
|
$fieldDefinition = $this->context->getFieldDefinition(); |
54
|
|
|
$parentType = $this->context->getParentType(); |
55
|
|
|
|
56
|
|
|
if (null !== $fieldDefinition && null !== $parentType) { |
57
|
|
|
$options = array_map(function (Argument $argument) { |
58
|
|
|
return $argument->getName(); |
59
|
|
|
}, $fieldDefinition->getArguments()); |
60
|
|
|
|
61
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
62
|
|
|
$suggestions = suggestionList($node->getNameValue(), $options); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$this->context->reportError( |
65
|
|
|
new ValidationException( |
66
|
|
|
unknownArgumentMessage((string)$node, |
67
|
|
|
(string)$fieldDefinition, |
68
|
|
|
(string)$parentType, $suggestions), |
69
|
|
|
[$node] |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $node; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param NodeInterface $node |
79
|
|
|
* |
80
|
|
|
* @return NodeInterface|null |
81
|
|
|
*/ |
82
|
|
|
protected function validateDirective(NodeInterface $node): ?NodeInterface |
83
|
|
|
{ |
84
|
|
|
$directive = $this->context->getDirective(); |
85
|
|
|
|
86
|
|
|
if (null !== $directive) { |
87
|
|
|
$options = array_map(function (Argument $argument) { |
88
|
|
|
return $argument->getName(); |
89
|
|
|
}, $directive->getArguments()); |
90
|
|
|
|
91
|
|
|
$suggestions = suggestionList((string)$node, $options); |
92
|
|
|
|
93
|
|
|
$this->context->reportError( |
94
|
|
|
new ValidationException( |
95
|
|
|
unknownDirectiveArgumentMessage((string)$node, |
96
|
|
|
(string)$directive, $suggestions), |
97
|
|
|
[$node] |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $node; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|