UniqueDirectiveNamesRule.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. const knownDirectiveNames = Object.create(null);
  14. const schema = context.getSchema();
  15. return {
  16. DirectiveDefinition(node) {
  17. const directiveName = node.name.value;
  18. if (
  19. schema !== null &&
  20. schema !== void 0 &&
  21. schema.getDirective(directiveName)
  22. ) {
  23. context.reportError(
  24. new _GraphQLError.GraphQLError(
  25. `Directive "@${directiveName}" already exists in the schema. It cannot be redefined.`,
  26. {
  27. nodes: node.name,
  28. },
  29. ),
  30. );
  31. return;
  32. }
  33. if (knownDirectiveNames[directiveName]) {
  34. context.reportError(
  35. new _GraphQLError.GraphQLError(
  36. `There can be only one directive named "@${directiveName}".`,
  37. {
  38. nodes: [knownDirectiveNames[directiveName], node.name],
  39. },
  40. ),
  41. );
  42. } else {
  43. knownDirectiveNames[directiveName] = node.name;
  44. }
  45. return false;
  46. },
  47. };
  48. }