coerceValue.mjs 1.07 KB
Newer Older
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
/* istanbul ignore file */
import inspect from '../jsutils/inspect';
import printPathArray from '../jsutils/printPathArray';
import { pathToArray } from '../jsutils/Path';
import { GraphQLError } from '../error/GraphQLError';
import { coerceInputValue } from './coerceInputValue';

/**
 * Deprecated. Use coerceInputValue() directly for richer information.
 *
 * This function will be removed in v15
 */
export function coerceValue(inputValue, type, blameNode, path) {
  var errors = [];
  var value = coerceInputValue(inputValue, type, function (errorPath, invalidValue, error) {
    var errorPrefix = 'Invalid value ' + inspect(invalidValue);
    var pathArray = [].concat(pathToArray(path), errorPath);

    if (pathArray.length > 0) {
      errorPrefix += " at \"value".concat(printPathArray(pathArray), "\"");
    }

    errors.push(new GraphQLError(errorPrefix + ': ' + error.message, blameNode, undefined, undefined, undefined, error.originalError));
  });
  return errors.length > 0 ? {
    errors: errors,
    value: undefined
  } : {
    errors: undefined,
    value: value
  };
}