VariablesInAllowedPositionRule.js 3.3 KB

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