NoUndefinedVariables.js 1.5 KB

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