1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.ValuesOfCorrectTypeRule = ValuesOfCorrectTypeRule;
|
7 |
|
8 | var _objectValues3 = _interopRequireDefault(require("../../polyfills/objectValues.js"));
|
9 |
|
10 | var _keyMap = _interopRequireDefault(require("../../jsutils/keyMap.js"));
|
11 |
|
12 | var _inspect = _interopRequireDefault(require("../../jsutils/inspect.js"));
|
13 |
|
14 | var _didYouMean = _interopRequireDefault(require("../../jsutils/didYouMean.js"));
|
15 |
|
16 | var _suggestionList = _interopRequireDefault(require("../../jsutils/suggestionList.js"));
|
17 |
|
18 | var _GraphQLError = require("../../error/GraphQLError.js");
|
19 |
|
20 | var _printer = require("../../language/printer.js");
|
21 |
|
22 | var _definition = require("../../type/definition.js");
|
23 |
|
24 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function ValuesOfCorrectTypeRule(context) {
|
33 | return {
|
34 | ListValue: function ListValue(node) {
|
35 |
|
36 |
|
37 | var type = (0, _definition.getNullableType)(context.getParentInputType());
|
38 |
|
39 | if (!(0, _definition.isListType)(type)) {
|
40 | isValidValueNode(context, node);
|
41 | return false;
|
42 | }
|
43 | },
|
44 | ObjectValue: function ObjectValue(node) {
|
45 | var type = (0, _definition.getNamedType)(context.getInputType());
|
46 |
|
47 | if (!(0, _definition.isInputObjectType)(type)) {
|
48 | isValidValueNode(context, node);
|
49 | return false;
|
50 | }
|
51 |
|
52 |
|
53 | var fieldNodeMap = (0, _keyMap.default)(node.fields, function (field) {
|
54 | return field.name.value;
|
55 | });
|
56 |
|
57 | for (var _i2 = 0, _objectValues2 = (0, _objectValues3.default)(type.getFields()); _i2 < _objectValues2.length; _i2++) {
|
58 | var fieldDef = _objectValues2[_i2];
|
59 | var fieldNode = fieldNodeMap[fieldDef.name];
|
60 |
|
61 | if (!fieldNode && (0, _definition.isRequiredInputField)(fieldDef)) {
|
62 | var typeStr = (0, _inspect.default)(fieldDef.type);
|
63 | context.reportError(new _GraphQLError.GraphQLError("Field \"".concat(type.name, ".").concat(fieldDef.name, "\" of required type \"").concat(typeStr, "\" was not provided."), node));
|
64 | }
|
65 | }
|
66 | },
|
67 | ObjectField: function ObjectField(node) {
|
68 | var parentType = (0, _definition.getNamedType)(context.getParentInputType());
|
69 | var fieldType = context.getInputType();
|
70 |
|
71 | if (!fieldType && (0, _definition.isInputObjectType)(parentType)) {
|
72 | var suggestions = (0, _suggestionList.default)(node.name.value, Object.keys(parentType.getFields()));
|
73 | context.reportError(new _GraphQLError.GraphQLError("Field \"".concat(node.name.value, "\" is not defined by type \"").concat(parentType.name, "\".") + (0, _didYouMean.default)(suggestions), node));
|
74 | }
|
75 | },
|
76 | NullValue: function NullValue(node) {
|
77 | var type = context.getInputType();
|
78 |
|
79 | if ((0, _definition.isNonNullType)(type)) {
|
80 | context.reportError(new _GraphQLError.GraphQLError("Expected value of type \"".concat((0, _inspect.default)(type), "\", found ").concat((0, _printer.print)(node), "."), node));
|
81 | }
|
82 | },
|
83 | EnumValue: function EnumValue(node) {
|
84 | return isValidValueNode(context, node);
|
85 | },
|
86 | IntValue: function IntValue(node) {
|
87 | return isValidValueNode(context, node);
|
88 | },
|
89 | FloatValue: function FloatValue(node) {
|
90 | return isValidValueNode(context, node);
|
91 | },
|
92 | StringValue: function StringValue(node) {
|
93 | return isValidValueNode(context, node);
|
94 | },
|
95 | BooleanValue: function BooleanValue(node) {
|
96 | return isValidValueNode(context, node);
|
97 | }
|
98 | };
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | function isValidValueNode(context, node) {
|
107 |
|
108 | var locationType = context.getInputType();
|
109 |
|
110 | if (!locationType) {
|
111 | return;
|
112 | }
|
113 |
|
114 | var type = (0, _definition.getNamedType)(locationType);
|
115 |
|
116 | if (!(0, _definition.isLeafType)(type)) {
|
117 | var typeStr = (0, _inspect.default)(locationType);
|
118 | context.reportError(new _GraphQLError.GraphQLError("Expected value of type \"".concat(typeStr, "\", found ").concat((0, _printer.print)(node), "."), node));
|
119 | return;
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 | try {
|
125 | var parseResult = type.parseLiteral(node, undefined
|
126 |
|
127 | );
|
128 |
|
129 | if (parseResult === undefined) {
|
130 | var _typeStr = (0, _inspect.default)(locationType);
|
131 |
|
132 | context.reportError(new _GraphQLError.GraphQLError("Expected value of type \"".concat(_typeStr, "\", found ").concat((0, _printer.print)(node), "."), node));
|
133 | }
|
134 | } catch (error) {
|
135 | var _typeStr2 = (0, _inspect.default)(locationType);
|
136 |
|
137 | if (error instanceof _GraphQLError.GraphQLError) {
|
138 | context.reportError(error);
|
139 | } else {
|
140 | context.reportError(new _GraphQLError.GraphQLError("Expected value of type \"".concat(_typeStr2, "\", found ").concat((0, _printer.print)(node), "; ") + error.message, node, undefined, undefined, undefined, error));
|
141 | }
|
142 | }
|
143 | }
|