UniqueVariableNames.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.duplicateVariableMessage = duplicateVariableMessage;
  6. exports.UniqueVariableNames = UniqueVariableNames;
  7. var _GraphQLError = require("../../error/GraphQLError");
  8. function duplicateVariableMessage(variableName) {
  9. return "There can be only one variable named \"".concat(variableName, "\".");
  10. }
  11. /**
  12. * Unique variable names
  13. *
  14. * A GraphQL operation is only valid if all its variables are uniquely named.
  15. */
  16. function UniqueVariableNames(context) {
  17. var knownVariableNames = Object.create(null);
  18. return {
  19. OperationDefinition: function OperationDefinition() {
  20. knownVariableNames = Object.create(null);
  21. },
  22. VariableDefinition: function VariableDefinition(node) {
  23. var variableName = node.variable.name.value;
  24. if (knownVariableNames[variableName]) {
  25. context.reportError(new _GraphQLError.GraphQLError(duplicateVariableMessage(variableName), [knownVariableNames[variableName], node.variable.name]));
  26. } else {
  27. knownVariableNames[variableName] = node.variable.name;
  28. }
  29. }
  30. };
  31. }