UNPKG

5.59 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.coerceInputValue = coerceInputValue;
7
8var _objectValues3 = _interopRequireDefault(require("../polyfills/objectValues.js"));
9
10var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
11
12var _invariant = _interopRequireDefault(require("../jsutils/invariant.js"));
13
14var _didYouMean = _interopRequireDefault(require("../jsutils/didYouMean.js"));
15
16var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike.js"));
17
18var _safeArrayFrom = _interopRequireDefault(require("../jsutils/safeArrayFrom.js"));
19
20var _suggestionList = _interopRequireDefault(require("../jsutils/suggestionList.js"));
21
22var _printPathArray = _interopRequireDefault(require("../jsutils/printPathArray.js"));
23
24var _Path = require("../jsutils/Path.js");
25
26var _GraphQLError = require("../error/GraphQLError.js");
27
28var _definition = require("../type/definition.js");
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 var coercedList = (0, _safeArrayFrom.default)(inputValue, function (itemValue, index) {
69 var itemPath = (0, _Path.addPath)(path, index, undefined);
70 return coerceInputValueImpl(itemValue, itemType, onError, itemPath);
71 });
72
73 if (coercedList != null) {
74 return coercedList;
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, type.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 } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
120
121
122 if ((0, _definition.isLeafType)(type)) {
123 var parseResult; // Scalars and Enums determine if a input value is valid via parseValue(),
124 // which can throw to indicate failure. If it throws, maintain a reference
125 // to the original error.
126
127 try {
128 parseResult = type.parseValue(inputValue);
129 } catch (error) {
130 if (error instanceof _GraphQLError.GraphQLError) {
131 onError((0, _Path.pathToArray)(path), inputValue, error);
132 } else {
133 onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type \"".concat(type.name, "\". ") + error.message, undefined, undefined, undefined, undefined, error));
134 }
135
136 return;
137 }
138
139 if (parseResult === undefined) {
140 onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type \"".concat(type.name, "\".")));
141 }
142
143 return parseResult;
144 } // istanbul ignore next (Not reachable. All possible input types have been considered)
145
146
147 false || (0, _invariant.default)(0, 'Unexpected input type: ' + (0, _inspect.default)(type));
148}