UNPKG

1.09 kBJavaScriptView Raw
1/* istanbul ignore file */
2import inspect from '../jsutils/inspect';
3import printPathArray from '../jsutils/printPathArray';
4import { pathToArray } from '../jsutils/Path';
5import { GraphQLError } from '../error/GraphQLError';
6import { coerceInputValue } from './coerceInputValue';
7
8/**
9 * Deprecated. Use coerceInputValue() directly for richer information.
10 *
11 * This function will be removed in v15
12 */
13export function coerceValue(inputValue, type, blameNode, path) {
14 var errors = [];
15 var value = coerceInputValue(inputValue, type, function (errorPath, invalidValue, error) {
16 var errorPrefix = 'Invalid value ' + inspect(invalidValue);
17 var pathArray = [].concat(pathToArray(path), errorPath);
18
19 if (pathArray.length > 0) {
20 errorPrefix += " at \"value".concat(printPathArray(pathArray), "\"");
21 }
22
23 errors.push(new GraphQLError(errorPrefix + ': ' + error.message, blameNode, undefined, undefined, undefined, error.originalError));
24 });
25 return errors.length > 0 ? {
26 errors: errors,
27 value: undefined
28 } : {
29 errors: undefined,
30 value: value
31 };
32}