NoUndefinedVariables.mjs 1.3 KB

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