UniqueDirectiveNamesRule.mjs 1.0 KB

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