UniqueInputFieldNamesRule.js 1.0 KB

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