UNPKG

5.82 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.coerceInputValue = coerceInputValue;
7
8var _iterall = require("iterall");
9
10var _objectValues3 = _interopRequireDefault(require("../polyfills/objectValues"));
11
12var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
13
14var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
15
16var _didYouMean = _interopRequireDefault(require("../jsutils/didYouMean"));
17
18var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike"));
19
20var _suggestionList = _interopRequireDefault(require("../jsutils/suggestionList"));
21
22var _printPathArray = _interopRequireDefault(require("../jsutils/printPathArray"));
23
24var _Path = require("../jsutils/Path");
25
26var _GraphQLError = require("../error/GraphQLError");
27
28var _definition = require("../type/definition");
29
30function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
32/**
33 * Coerces a JavaScript value given a GraphQL Input Type.
34 */
35function coerceInputValue(inputValue, type) {
36 var onError = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultOnError;
37 return coerceInputValueImpl(inputValue, type, onError);
38}
39
40function defaultOnError(path, invalidValue, error) {
41 var errorPrefix = 'Invalid value ' + (0, _inspect.default)(invalidValue);
42
43 if (path.length > 0) {
44 errorPrefix += " at \"value".concat((0, _printPathArray.default)(path), "\": ");
45 }
46
47 error.message = errorPrefix + ': ' + error.message;
48 throw error;
49}
50
51function coerceInputValueImpl(inputValue, type, onError, path) {
52 if ((0, _definition.isNonNullType)(type)) {
53 if (inputValue != null) {
54 return coerceInputValueImpl(inputValue, type.ofType, onError, path);
55 }
56
57 onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected non-nullable type ".concat((0, _inspect.default)(type), " not to be null.")));
58 return;
59 }
60
61 if (inputValue == null) {
62 // Explicitly return the value null.
63 return null;
64 }
65
66 if ((0, _definition.isListType)(type)) {
67 var itemType = type.ofType;
68
69 if ((0, _iterall.isCollection)(inputValue)) {
70 var coercedValue = [];
71 (0, _iterall.forEach)(inputValue, function (itemValue, index) {
72 coercedValue.push(coerceInputValueImpl(itemValue, itemType, onError, (0, _Path.addPath)(path, index)));
73 });
74 return coercedValue;
75 } // Lists accept a non-list value as a list of one.
76
77
78 return [coerceInputValueImpl(inputValue, itemType, onError, path)];
79 }
80
81 if ((0, _definition.isInputObjectType)(type)) {
82 if (!(0, _isObjectLike.default)(inputValue)) {
83 onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type ".concat(type.name, " to be an object.")));
84 return;
85 }
86
87 var _coercedValue = {};
88 var fieldDefs = type.getFields();
89
90 for (var _i2 = 0, _objectValues2 = (0, _objectValues3.default)(fieldDefs); _i2 < _objectValues2.length; _i2++) {
91 var field = _objectValues2[_i2];
92 var fieldValue = inputValue[field.name];
93
94 if (fieldValue === undefined) {
95 if (field.defaultValue !== undefined) {
96 _coercedValue[field.name] = field.defaultValue;
97 } else if ((0, _definition.isNonNullType)(field.type)) {
98 var typeStr = (0, _inspect.default)(field.type);
99 onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Field ".concat(field.name, " of required type ").concat(typeStr, " was not provided.")));
100 }
101
102 continue;
103 }
104
105 _coercedValue[field.name] = coerceInputValueImpl(fieldValue, field.type, onError, (0, _Path.addPath)(path, field.name));
106 } // Ensure every provided field is defined.
107
108
109 for (var _i4 = 0, _Object$keys2 = Object.keys(inputValue); _i4 < _Object$keys2.length; _i4++) {
110 var fieldName = _Object$keys2[_i4];
111
112 if (!fieldDefs[fieldName]) {
113 var suggestions = (0, _suggestionList.default)(fieldName, Object.keys(type.getFields()));
114 onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Field \"".concat(fieldName, "\" is not defined by type ").concat(type.name, ".") + (0, _didYouMean.default)(suggestions)));
115 }
116 }
117
118 return _coercedValue;
119 }
120
121 if ((0, _definition.isScalarType)(type)) {
122 var parseResult; // Scalars determine if a input value is valid via parseValue(), which can
123 // throw to indicate failure. If it throws, maintain a reference to
124 // the original error.
125
126 try {
127 parseResult = type.parseValue(inputValue);
128 } catch (error) {
129 onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type ".concat(type.name, ". ") + error.message, undefined, undefined, undefined, undefined, error));
130 return;
131 }
132
133 if (parseResult === undefined) {
134 onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type ".concat(type.name, ".")));
135 }
136
137 return parseResult;
138 }
139
140 /* istanbul ignore else */
141 if ((0, _definition.isEnumType)(type)) {
142 if (typeof inputValue === 'string') {
143 var enumValue = type.getValue(inputValue);
144
145 if (enumValue) {
146 return enumValue.value;
147 }
148 }
149
150 var _suggestions = (0, _suggestionList.default)(String(inputValue), type.getValues().map(function (enumValue) {
151 return enumValue.name;
152 }));
153
154 onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type ".concat(type.name, ".") + (0, _didYouMean.default)(_suggestions)));
155 return;
156 } // Not reachable. All possible input types have been considered.
157
158
159 /* istanbul ignore next */
160 (0, _invariant.default)(false, 'Unexpected input type: ' + (0, _inspect.default)(type));
161}