-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathUniqueDirectivesPerLocation.js
82 lines (75 loc) · 2.55 KB
/
UniqueDirectivesPerLocation.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
import type {
SDLValidationContext,
ValidationContext,
} from '../ValidationContext';
import { GraphQLError } from '../../error/GraphQLError';
import type { DirectiveNode } from '../../language/ast';
import type { ASTVisitor } from '../../language/visitor';
import { Kind } from '../../language/kinds';
import { specifiedDirectives } from '../../type/directives';
export function duplicateDirectiveMessage(directiveName: string): string {
return (
`The directive "${directiveName}" can only be used once at ` +
'this location.'
);
}
/**
* Unique directive names per location
*
* A GraphQL document is only valid if all directives at a given location
* are uniquely named.
*/
export function UniqueDirectivesPerLocation(
context: ValidationContext | SDLValidationContext,
): ASTVisitor {
const uniqueDirectiveMap = Object.create(null);
const schema = context.getSchema();
const definedDirectives = schema
? schema.getDirectives()
: specifiedDirectives;
for (const directive of definedDirectives) {
uniqueDirectiveMap[directive.name] = !directive.isRepeatable;
}
const astDefinitions = context.getDocument().definitions;
for (const def of astDefinitions) {
if (def.kind === Kind.DIRECTIVE_DEFINITION) {
uniqueDirectiveMap[def.name.value] = !def.isRepeatable;
}
}
return {
// Many different AST nodes may contain directives. Rather than listing
// them all, just listen for entering any node, and check to see if it
// defines any directives.
enter(node) {
// Flow can't refine that node.directives will only contain directives,
// so we cast so the rest of the code is well typed.
const directives: ?$ReadOnlyArray<DirectiveNode> = (node: any).directives;
if (directives) {
const knownDirectives = Object.create(null);
for (const directive of directives) {
const directiveName = directive.name.value;
if (uniqueDirectiveMap[directiveName]) {
if (knownDirectives[directiveName]) {
context.reportError(
new GraphQLError(duplicateDirectiveMessage(directiveName), [
knownDirectives[directiveName],
directive,
]),
);
} else {
knownDirectives[directiveName] = directive;
}
}
}
}
},
};
}