VariablesInAllowedPosition.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.badVarPosMessage = badVarPosMessage;
  6. exports.VariablesInAllowedPosition = VariablesInAllowedPosition;
  7. var _inspect = _interopRequireDefault(require("../../jsutils/inspect"));
  8. var _GraphQLError = require("../../error/GraphQLError");
  9. var _kinds = require("../../language/kinds");
  10. var _definition = require("../../type/definition");
  11. var _typeFromAST = require("../../utilities/typeFromAST");
  12. var _typeComparators = require("../../utilities/typeComparators");
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. function badVarPosMessage(varName, varType, expectedType) {
  15. return "Variable \"$".concat(varName, "\" of type \"").concat(varType, "\" used in position expecting type \"").concat(expectedType, "\".");
  16. }
  17. /**
  18. * Variables passed to field arguments conform to type
  19. */
  20. function VariablesInAllowedPosition(context) {
  21. var varDefMap = Object.create(null);
  22. return {
  23. OperationDefinition: {
  24. enter: function enter() {
  25. varDefMap = Object.create(null);
  26. },
  27. leave: function leave(operation) {
  28. var usages = context.getRecursiveVariableUsages(operation);
  29. for (var _i2 = 0; _i2 < usages.length; _i2++) {
  30. var _ref2 = usages[_i2];
  31. var node = _ref2.node;
  32. var type = _ref2.type;
  33. var defaultValue = _ref2.defaultValue;
  34. var varName = node.name.value;
  35. var varDef = varDefMap[varName];
  36. if (varDef && type) {
  37. // A var type is allowed if it is the same or more strict (e.g. is
  38. // a subtype of) than the expected type. It can be more strict if
  39. // the variable type is non-null when the expected type is nullable.
  40. // If both are list types, the variable item type can be more strict
  41. // than the expected item type (contravariant).
  42. var schema = context.getSchema();
  43. var varType = (0, _typeFromAST.typeFromAST)(schema, varDef.type);
  44. if (varType && !allowedVariableUsage(schema, varType, varDef.defaultValue, type, defaultValue)) {
  45. context.reportError(new _GraphQLError.GraphQLError(badVarPosMessage(varName, (0, _inspect.default)(varType), (0, _inspect.default)(type)), [varDef, node]));
  46. }
  47. }
  48. }
  49. }
  50. },
  51. VariableDefinition: function VariableDefinition(node) {
  52. varDefMap[node.variable.name.value] = node;
  53. }
  54. };
  55. }
  56. /**
  57. * Returns true if the variable is allowed in the location it was found,
  58. * which includes considering if default values exist for either the variable
  59. * or the location at which it is located.
  60. */
  61. function allowedVariableUsage(schema, varType, varDefaultValue, locationType, locationDefaultValue) {
  62. if ((0, _definition.isNonNullType)(locationType) && !(0, _definition.isNonNullType)(varType)) {
  63. var hasNonNullVariableDefaultValue = varDefaultValue != null && varDefaultValue.kind !== _kinds.Kind.NULL;
  64. var hasLocationDefaultValue = locationDefaultValue !== undefined;
  65. if (!hasNonNullVariableDefaultValue && !hasLocationDefaultValue) {
  66. return false;
  67. }
  68. var nullableLocationType = locationType.ofType;
  69. return (0, _typeComparators.isTypeSubTypeOf)(schema, varType, nullableLocationType);
  70. }
  71. return (0, _typeComparators.isTypeSubTypeOf)(schema, varType, locationType);
  72. }