UniqueArgumentNamesRule.mjs 831 B

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