UniqueVariableNamesRule.js 977 B

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