UniqueArgumentNamesRule.js.flow 999 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // @flow strict
  2. import { GraphQLError } from '../../error/GraphQLError';
  3. import type { ASTVisitor } from '../../language/visitor';
  4. import type { ASTValidationContext } from '../ValidationContext';
  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 UniqueArgumentNamesRule(
  12. context: ASTValidationContext,
  13. ): ASTVisitor {
  14. let knownArgNames = Object.create(null);
  15. return {
  16. Field() {
  17. knownArgNames = Object.create(null);
  18. },
  19. Directive() {
  20. knownArgNames = Object.create(null);
  21. },
  22. Argument(node) {
  23. const argName = node.name.value;
  24. if (knownArgNames[argName]) {
  25. context.reportError(
  26. new GraphQLError(
  27. `There can be only one argument named "${argName}".`,
  28. [knownArgNames[argName], node.name],
  29. ),
  30. );
  31. } else {
  32. knownArgNames[argName] = node.name;
  33. }
  34. return false;
  35. },
  36. };
  37. }