UniqueDirectiveNames.js 1.5 KB

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