UNPKG

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