UNPKG

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