coerceInputValue.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.coerceInputValue = coerceInputValue;
  6. var _objectValues3 = _interopRequireDefault(require("../polyfills/objectValues.js"));
  7. var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
  8. var _invariant = _interopRequireDefault(require("../jsutils/invariant.js"));
  9. var _didYouMean = _interopRequireDefault(require("../jsutils/didYouMean.js"));
  10. var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike.js"));
  11. var _safeArrayFrom = _interopRequireDefault(require("../jsutils/safeArrayFrom.js"));
  12. var _suggestionList = _interopRequireDefault(require("../jsutils/suggestionList.js"));
  13. var _printPathArray = _interopRequireDefault(require("../jsutils/printPathArray.js"));
  14. var _Path = require("../jsutils/Path.js");
  15. var _GraphQLError = require("../error/GraphQLError.js");
  16. var _definition = require("../type/definition.js");
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. /**
  19. * Coerces a JavaScript value given a GraphQL Input Type.
  20. */
  21. function coerceInputValue(inputValue, type) {
  22. var onError = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultOnError;
  23. return coerceInputValueImpl(inputValue, type, onError);
  24. }
  25. function defaultOnError(path, invalidValue, error) {
  26. var errorPrefix = 'Invalid value ' + (0, _inspect.default)(invalidValue);
  27. if (path.length > 0) {
  28. errorPrefix += " at \"value".concat((0, _printPathArray.default)(path), "\"");
  29. }
  30. error.message = errorPrefix + ': ' + error.message;
  31. throw error;
  32. }
  33. function coerceInputValueImpl(inputValue, type, onError, path) {
  34. if ((0, _definition.isNonNullType)(type)) {
  35. if (inputValue != null) {
  36. return coerceInputValueImpl(inputValue, type.ofType, onError, path);
  37. }
  38. onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected non-nullable type \"".concat((0, _inspect.default)(type), "\" not to be null.")));
  39. return;
  40. }
  41. if (inputValue == null) {
  42. // Explicitly return the value null.
  43. return null;
  44. }
  45. if ((0, _definition.isListType)(type)) {
  46. var itemType = type.ofType;
  47. var coercedList = (0, _safeArrayFrom.default)(inputValue, function (itemValue, index) {
  48. var itemPath = (0, _Path.addPath)(path, index, undefined);
  49. return coerceInputValueImpl(itemValue, itemType, onError, itemPath);
  50. });
  51. if (coercedList != null) {
  52. return coercedList;
  53. } // Lists accept a non-list value as a list of one.
  54. return [coerceInputValueImpl(inputValue, itemType, onError, path)];
  55. }
  56. if ((0, _definition.isInputObjectType)(type)) {
  57. if (!(0, _isObjectLike.default)(inputValue)) {
  58. onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type \"".concat(type.name, "\" to be an object.")));
  59. return;
  60. }
  61. var coercedValue = {};
  62. var fieldDefs = type.getFields();
  63. for (var _i2 = 0, _objectValues2 = (0, _objectValues3.default)(fieldDefs); _i2 < _objectValues2.length; _i2++) {
  64. var field = _objectValues2[_i2];
  65. var fieldValue = inputValue[field.name];
  66. if (fieldValue === undefined) {
  67. if (field.defaultValue !== undefined) {
  68. coercedValue[field.name] = field.defaultValue;
  69. } else if ((0, _definition.isNonNullType)(field.type)) {
  70. var typeStr = (0, _inspect.default)(field.type);
  71. onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Field \"".concat(field.name, "\" of required type \"").concat(typeStr, "\" was not provided.")));
  72. }
  73. continue;
  74. }
  75. coercedValue[field.name] = coerceInputValueImpl(fieldValue, field.type, onError, (0, _Path.addPath)(path, field.name, type.name));
  76. } // Ensure every provided field is defined.
  77. for (var _i4 = 0, _Object$keys2 = Object.keys(inputValue); _i4 < _Object$keys2.length; _i4++) {
  78. var fieldName = _Object$keys2[_i4];
  79. if (!fieldDefs[fieldName]) {
  80. var suggestions = (0, _suggestionList.default)(fieldName, Object.keys(type.getFields()));
  81. onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Field \"".concat(fieldName, "\" is not defined by type \"").concat(type.name, "\".") + (0, _didYouMean.default)(suggestions)));
  82. }
  83. }
  84. return coercedValue;
  85. } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
  86. if ((0, _definition.isLeafType)(type)) {
  87. var parseResult; // Scalars and Enums determine if a input value is valid via parseValue(),
  88. // which can throw to indicate failure. If it throws, maintain a reference
  89. // to the original error.
  90. try {
  91. parseResult = type.parseValue(inputValue);
  92. } catch (error) {
  93. if (error instanceof _GraphQLError.GraphQLError) {
  94. onError((0, _Path.pathToArray)(path), inputValue, error);
  95. } else {
  96. onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type \"".concat(type.name, "\". ") + error.message, undefined, undefined, undefined, undefined, error));
  97. }
  98. return;
  99. }
  100. if (parseResult === undefined) {
  101. onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type \"".concat(type.name, "\".")));
  102. }
  103. return parseResult;
  104. } // istanbul ignore next (Not reachable. All possible input types have been considered)
  105. false || (0, _invariant.default)(0, 'Unexpected input type: ' + (0, _inspect.default)(type));
  106. }