coerceValue.mjs 1.1 KB

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