-
-
Notifications
You must be signed in to change notification settings - Fork 566
/
UniqueDirectiveNames.php
59 lines (50 loc) · 1.92 KB
/
UniqueDirectiveNames.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php declare(strict_types=1);
namespace GraphQL\Validator\Rules;
use GraphQL\Error\Error;
use GraphQL\Language\AST\NameNode;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\Visitor;
use GraphQL\Language\VisitorOperation;
use GraphQL\Validator\SDLValidationContext;
/**
* Unique directive names.
*
* A GraphQL document is only valid if all defined directives have unique names.
*/
class UniqueDirectiveNames extends ValidationRule
{
public function getSDLVisitor(SDLValidationContext $context): array
{
$schema = $context->getSchema();
/** @var array<string, NameNode> $knownDirectiveNames */
$knownDirectiveNames = [];
return [
NodeKind::DIRECTIVE_DEFINITION => static function ($node) use ($context, $schema, &$knownDirectiveNames): ?VisitorOperation {
$directiveName = $node->name->value;
if ($schema !== null && $schema->getDirective($directiveName) !== null) {
$context->reportError(
new Error(
'Directive "@' . $directiveName . '" already exists in the schema. It cannot be redefined.',
$node->name,
),
);
return null;
}
if (isset($knownDirectiveNames[$directiveName])) {
$context->reportError(
new Error(
'There can be only one directive named "@' . $directiveName . '".',
[
$knownDirectiveNames[$directiveName],
$node->name,
]
),
);
} else {
$knownDirectiveNames[$directiveName] = $node->name;
}
return Visitor::skipNode();
},
];
}
}