UniqueDirectiveNames.mjs 1.2 KB

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