1 | <?php |
||
2 | |||
3 | namespace Digia\GraphQL\Validation\Rule; |
||
4 | |||
5 | use Digia\GraphQL\Language\Node\ArgumentNode; |
||
6 | use Digia\GraphQL\Language\Node\DirectiveNode; |
||
7 | use Digia\GraphQL\Language\Node\FieldNode; |
||
8 | use Digia\GraphQL\Language\Node\NameNode; |
||
9 | use Digia\GraphQL\Language\Visitor\VisitorResult; |
||
10 | use Digia\GraphQL\Validation\ValidationException; |
||
11 | use function Digia\GraphQL\Validation\duplicateArgumentMessage; |
||
12 | |||
13 | /** |
||
14 | * Unique argument names |
||
15 | * |
||
16 | * A GraphQL field or directive is only valid if all supplied arguments are |
||
17 | * uniquely named. |
||
18 | */ |
||
19 | class UniqueArgumentNamesRule extends AbstractRule |
||
20 | { |
||
21 | /** |
||
22 | * @var NameNode[] |
||
23 | */ |
||
24 | protected $knownArgumentNames = []; |
||
25 | |||
26 | /** |
||
27 | * @inheritdoc |
||
28 | */ |
||
29 | protected function enterField(FieldNode $node): VisitorResult |
||
30 | { |
||
31 | $this->knownArgumentNames = []; |
||
32 | |||
33 | return new VisitorResult($node); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @inheritdoc |
||
38 | */ |
||
39 | protected function enterDirective(DirectiveNode $node): VisitorResult |
||
40 | { |
||
41 | $this->knownArgumentNames = []; |
||
42 | |||
43 | return new VisitorResult($node); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @inheritdoc |
||
48 | */ |
||
49 | protected function enterArgument(ArgumentNode $node): VisitorResult |
||
50 | { |
||
51 | $argumentName = $node->getNameValue(); |
||
52 | |||
53 | if (isset($this->knownArgumentNames[$argumentName])) { |
||
54 | $this->context->reportError( |
||
55 | new ValidationException( |
||
56 | duplicateArgumentMessage($argumentName), |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
57 | [$this->knownArgumentNames[$argumentName], $node->getName()] |
||
58 | ) |
||
59 | ); |
||
60 | } else { |
||
61 | $this->knownArgumentNames[$argumentName] = $node->getName(); |
||
62 | } |
||
63 | |||
64 | return new VisitorResult(null); |
||
65 | } |
||
66 | } |
||
67 |