UNPKG

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