coerceValue.js 1.39 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.coerceValue = coerceValue;

var _inspect = _interopRequireDefault(require("../jsutils/inspect"));

var _printPathArray = _interopRequireDefault(require("../jsutils/printPathArray"));

var _Path = require("../jsutils/Path");

var _GraphQLError = require("../error/GraphQLError");

var _coerceInputValue = require("./coerceInputValue");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* istanbul ignore file */

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

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

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