1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.VariablesInAllowedPositionRule = VariablesInAllowedPositionRule;
|
7 |
|
8 | var _inspect = _interopRequireDefault(require("../../jsutils/inspect.js"));
|
9 |
|
10 | var _GraphQLError = require("../../error/GraphQLError.js");
|
11 |
|
12 | var _kinds = require("../../language/kinds.js");
|
13 |
|
14 | var _definition = require("../../type/definition.js");
|
15 |
|
16 | var _typeFromAST = require("../../utilities/typeFromAST.js");
|
17 |
|
18 | var _typeComparators = require("../../utilities/typeComparators.js");
|
19 |
|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function 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 |
|
45 |
|
46 |
|
47 |
|
48 |
|
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 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function 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 | }
|