UniqueArgumentNames.mjs 911 B

123456789101112131415161718192021222324252627282930313233
  1. import { GraphQLError } from '../../error/GraphQLError';
  2. export function duplicateArgMessage(argName) {
  3. return "There can be only one argument named \"".concat(argName, "\".");
  4. }
  5. /**
  6. * Unique argument names
  7. *
  8. * A GraphQL field or directive is only valid if all supplied arguments are
  9. * uniquely named.
  10. */
  11. export function UniqueArgumentNames(context) {
  12. var knownArgNames = Object.create(null);
  13. return {
  14. Field: function Field() {
  15. knownArgNames = Object.create(null);
  16. },
  17. Directive: function Directive() {
  18. knownArgNames = Object.create(null);
  19. },
  20. Argument: function Argument(node) {
  21. var argName = node.name.value;
  22. if (knownArgNames[argName]) {
  23. context.reportError(new GraphQLError(duplicateArgMessage(argName), [knownArgNames[argName], node.name]));
  24. } else {
  25. knownArgNames[argName] = node.name;
  26. }
  27. return false;
  28. }
  29. };
  30. }