1 | import objectValues from "../polyfills/objectValues.mjs";
|
2 | import inspect from "../jsutils/inspect.mjs";
|
3 | import invariant from "../jsutils/invariant.mjs";
|
4 | import didYouMean from "../jsutils/didYouMean.mjs";
|
5 | import isObjectLike from "../jsutils/isObjectLike.mjs";
|
6 | import safeArrayFrom from "../jsutils/safeArrayFrom.mjs";
|
7 | import suggestionList from "../jsutils/suggestionList.mjs";
|
8 | import printPathArray from "../jsutils/printPathArray.mjs";
|
9 | import { addPath, pathToArray } from "../jsutils/Path.mjs";
|
10 | import { GraphQLError } from "../error/GraphQLError.mjs";
|
11 | import { isLeafType, isInputObjectType, isListType, isNonNullType } from "../type/definition.mjs";
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export function coerceInputValue(inputValue, type) {
|
17 | var onError = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultOnError;
|
18 | return coerceInputValueImpl(inputValue, type, onError);
|
19 | }
|
20 |
|
21 | function 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 |
|
32 | function 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 |
|
44 | return null;
|
45 | }
|
46 |
|
47 | if (isListType(type)) {
|
48 | var itemType = type.ofType;
|
49 | var coercedList = safeArrayFrom(inputValue, function (itemValue, index) {
|
50 | var itemPath = addPath(path, index, undefined);
|
51 | return coerceInputValueImpl(itemValue, itemType, onError, itemPath);
|
52 | });
|
53 |
|
54 | if (coercedList != null) {
|
55 | return coercedList;
|
56 | }
|
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, type.name));
|
87 | }
|
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 |
|
103 | if (isLeafType(type)) {
|
104 | var parseResult;
|
105 |
|
106 |
|
107 |
|
108 | try {
|
109 | parseResult = type.parseValue(inputValue);
|
110 | } catch (error) {
|
111 | if (error instanceof GraphQLError) {
|
112 | onError(pathToArray(path), inputValue, error);
|
113 | } else {
|
114 | onError(pathToArray(path), inputValue, new GraphQLError("Expected type \"".concat(type.name, "\". ") + error.message, undefined, undefined, undefined, undefined, error));
|
115 | }
|
116 |
|
117 | return;
|
118 | }
|
119 |
|
120 | if (parseResult === undefined) {
|
121 | onError(pathToArray(path), inputValue, new GraphQLError("Expected type \"".concat(type.name, "\".")));
|
122 | }
|
123 |
|
124 | return parseResult;
|
125 | }
|
126 |
|
127 |
|
128 | false || invariant(0, 'Unexpected input type: ' + inspect(type));
|
129 | }
|