NoUndefinedVariablesRule.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.NoUndefinedVariablesRule = NoUndefinedVariablesRule;
  6. var _GraphQLError = require("../../error/GraphQLError.js");
  7. /**
  8. * No undefined variables
  9. *
  10. * A GraphQL operation is only valid if all variables encountered, both directly
  11. * and via fragment spreads, are defined by that operation.
  12. */
  13. function NoUndefinedVariablesRule(context) {
  14. var variableNameDefined = Object.create(null);
  15. return {
  16. OperationDefinition: {
  17. enter: function enter() {
  18. variableNameDefined = Object.create(null);
  19. },
  20. leave: function leave(operation) {
  21. var usages = context.getRecursiveVariableUsages(operation);
  22. for (var _i2 = 0; _i2 < usages.length; _i2++) {
  23. var _ref2 = usages[_i2];
  24. var node = _ref2.node;
  25. var varName = node.name.value;
  26. if (variableNameDefined[varName] !== true) {
  27. context.reportError(new _GraphQLError.GraphQLError(operation.name ? "Variable \"$".concat(varName, "\" is not defined by operation \"").concat(operation.name.value, "\".") : "Variable \"$".concat(varName, "\" is not defined."), [node, operation]));
  28. }
  29. }
  30. }
  31. },
  32. VariableDefinition: function VariableDefinition(node) {
  33. variableNameDefined[node.variable.name.value] = true;
  34. }
  35. };
  36. }