UNPKG

1.03 kBJavaScriptView Raw
1import { GraphQLError } from "../../error/GraphQLError.mjs";
2
3/**
4 * Unique directive names
5 *
6 * A GraphQL document is only valid if all defined directives have unique names.
7 */
8export function UniqueDirectiveNamesRule(context) {
9 var knownDirectiveNames = Object.create(null);
10 var schema = context.getSchema();
11 return {
12 DirectiveDefinition: function DirectiveDefinition(node) {
13 var directiveName = node.name.value;
14
15 if (schema !== null && schema !== void 0 && schema.getDirective(directiveName)) {
16 context.reportError(new GraphQLError("Directive \"@".concat(directiveName, "\" already exists in the schema. It cannot be redefined."), node.name));
17 return;
18 }
19
20 if (knownDirectiveNames[directiveName]) {
21 context.reportError(new GraphQLError("There can be only one directive named \"@".concat(directiveName, "\"."), [knownDirectiveNames[directiveName], node.name]));
22 } else {
23 knownDirectiveNames[directiveName] = node.name;
24 }
25
26 return false;
27 }
28 };
29}