UNPKG

5.42 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.ValuesOfCorrectTypeRule = ValuesOfCorrectTypeRule;
7
8var _objectValues3 = _interopRequireDefault(require("../../polyfills/objectValues.js"));
9
10var _keyMap = _interopRequireDefault(require("../../jsutils/keyMap.js"));
11
12var _inspect = _interopRequireDefault(require("../../jsutils/inspect.js"));
13
14var _didYouMean = _interopRequireDefault(require("../../jsutils/didYouMean.js"));
15
16var _suggestionList = _interopRequireDefault(require("../../jsutils/suggestionList.js"));
17
18var _GraphQLError = require("../../error/GraphQLError.js");
19
20var _printer = require("../../language/printer.js");
21
22var _definition = require("../../type/definition.js");
23
24function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26/**
27 * Value literals of correct type
28 *
29 * A GraphQL document is only valid if all value literals are of the type
30 * expected at their position.
31 */
32function ValuesOfCorrectTypeRule(context) {
33 return {
34 ListValue: function ListValue(node) {
35 // Note: TypeInfo will traverse into a list's item type, so look to the
36 // parent input type to check if it is a list.
37 var type = (0, _definition.getNullableType)(context.getParentInputType());
38
39 if (!(0, _definition.isListType)(type)) {
40 isValidValueNode(context, node);
41 return false; // Don't traverse further.
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; // Don't traverse further.
50 } // Ensure every required field exists.
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 * Any value literal may be a valid representation of a Scalar, depending on
102 * that scalar type.
103 */
104
105
106function isValidValueNode(context, node) {
107 // Report any error at the full type expected by the location.
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 } // Scalars and Enums determine if a literal value is valid via parseLiteral(),
121 // which may throw or return an invalid value to indicate failure.
122
123
124 try {
125 var parseResult = type.parseLiteral(node, undefined
126 /* variables */
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}