UniqueInputFieldNamesRule.mjs 911 B

1234567891011121314151617181920212223242526272829303132
  1. import { GraphQLError } from "../../error/GraphQLError.mjs";
  2. /**
  3. * Unique input field names
  4. *
  5. * A GraphQL input object value is only valid if all supplied fields are
  6. * uniquely named.
  7. */
  8. export function UniqueInputFieldNamesRule(context) {
  9. var knownNameStack = [];
  10. var knownNames = Object.create(null);
  11. return {
  12. ObjectValue: {
  13. enter: function enter() {
  14. knownNameStack.push(knownNames);
  15. knownNames = Object.create(null);
  16. },
  17. leave: function leave() {
  18. knownNames = knownNameStack.pop();
  19. }
  20. },
  21. ObjectField: function ObjectField(node) {
  22. var fieldName = node.name.value;
  23. if (knownNames[fieldName]) {
  24. context.reportError(new GraphQLError("There can be only one input field named \"".concat(fieldName, "\"."), [knownNames[fieldName], node.name]));
  25. } else {
  26. knownNames[fieldName] = node.name;
  27. }
  28. }
  29. };
  30. }