UniqueInputFieldNames.mjs 1009 B

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