1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// @flow strict
/* istanbul ignore file */
import inspect from '../jsutils/inspect';
import printPathArray from '../jsutils/printPathArray';
import { type Path, pathToArray } from '../jsutils/Path';
import { GraphQLError } from '../error/GraphQLError';
import { type ASTNode } from '../language/ast';
import { type GraphQLInputType } from '../type/definition';
import { coerceInputValue } from './coerceInputValue';
type CoercedValue = {|
+errors: $ReadOnlyArray<GraphQLError> | void,
+value: mixed,
|};
/**
* Deprecated. Use coerceInputValue() directly for richer information.
*
* This function will be removed in v15
*/
export function coerceValue(
inputValue: mixed,
type: GraphQLInputType,
blameNode?: ASTNode,
path?: Path,
): CoercedValue {
const errors = [];
const value = coerceInputValue(
inputValue,
type,
(errorPath, invalidValue, error) => {
let errorPrefix = 'Invalid value ' + inspect(invalidValue);
const pathArray = [...pathToArray(path), ...errorPath];
if (pathArray.length > 0) {
errorPrefix += ` at "value${printPathArray(pathArray)}"`;
}
errors.push(
new GraphQLError(
errorPrefix + ': ' + error.message,
blameNode,
undefined,
undefined,
undefined,
error.originalError,
),
);
},
);
return errors.length > 0
? { errors, value: undefined }
: { errors: undefined, value };
}