UniqueDirectiveNamesRule.js 1.2 KB

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