1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Validation\Rule; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\ValidationException; |
6
|
|
|
use Digia\GraphQL\Language\AST\Node\ArgumentNode; |
7
|
|
|
use Digia\GraphQL\Language\AST\Node\DirectiveNode; |
8
|
|
|
use Digia\GraphQL\Language\AST\Node\FieldNode; |
9
|
|
|
use Digia\GraphQL\Language\AST\Node\NodeInterface; |
10
|
|
|
use Digia\GraphQL\Type\Definition\Argument; |
11
|
|
|
use function Digia\GraphQL\Util\suggestionList; |
12
|
|
|
|
13
|
|
|
class KnownArgumentNamesRule extends AbstractRule |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @inheritdoc |
17
|
|
|
*/ |
18
|
|
|
public function enterNode(NodeInterface $node): ?NodeInterface |
19
|
|
|
{ |
20
|
|
|
if ($node instanceof ArgumentNode) { |
21
|
|
|
$argumentDefinition = $this->context->getArgument(); |
22
|
|
|
|
23
|
|
|
if (null === $argumentDefinition) { |
24
|
|
|
$argumentOf = $node->getClosestAncestor(); |
25
|
|
|
if ($argumentOf instanceof FieldNode) { |
26
|
|
|
$fieldDefinition = $this->context->getFieldDefinition(); |
27
|
|
|
$parentType = $this->context->getParentType(); |
28
|
|
|
|
29
|
|
|
if (null !== $fieldDefinition && null !== $parentType) { |
30
|
|
|
$this->context->reportError( |
31
|
|
|
new ValidationException( |
32
|
|
|
unknownArgumentMessage( |
33
|
|
|
(string)$node, |
34
|
|
|
(string)$fieldDefinition, |
35
|
|
|
(string)$parentType, |
36
|
|
|
suggestionList( |
37
|
|
|
$node->getNameValue(), |
38
|
|
|
array_map(function (Argument $argument) { |
39
|
|
|
return $argument->getName(); |
40
|
|
|
}, $fieldDefinition->getArguments()) |
41
|
|
|
) |
42
|
|
|
), |
43
|
|
|
[$node] |
44
|
|
|
) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
} elseif ($argumentOf instanceof DirectiveNode) { |
48
|
|
|
$directive = $this->context->getDirective(); |
49
|
|
|
|
50
|
|
|
if (null !== $directive) { |
51
|
|
|
$this->context->reportError( |
52
|
|
|
new ValidationException( |
53
|
|
|
unknownDirectiveArgumentMessage( |
54
|
|
|
(string)$node, |
55
|
|
|
(string)$directive, |
56
|
|
|
suggestionList( |
57
|
|
|
(string)$node, |
58
|
|
|
array_map(function (Argument $argument) { |
59
|
|
|
return $argument->getName(); |
60
|
|
|
}, $directive->getArguments()) |
61
|
|
|
) |
62
|
|
|
), |
63
|
|
|
[$node] |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $node; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|