UniqueInputFieldNames.js 1.2 KB

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