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