coerceInputValue.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.coerceInputValue = coerceInputValue;
  6. var _iterall = require("iterall");
  7. var _objectValues3 = _interopRequireDefault(require("../polyfills/objectValues"));
  8. var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
  9. var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
  10. var _didYouMean = _interopRequireDefault(require("../jsutils/didYouMean"));
  11. var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike"));
  12. var _suggestionList = _interopRequireDefault(require("../jsutils/suggestionList"));
  13. var _printPathArray = _interopRequireDefault(require("../jsutils/printPathArray"));
  14. var _Path = require("../jsutils/Path");
  15. var _GraphQLError = require("../error/GraphQLError");
  16. var _definition = require("../type/definition");
  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. if ((0, _iterall.isCollection)(inputValue)) {
  48. var coercedValue = [];
  49. (0, _iterall.forEach)(inputValue, function (itemValue, index) {
  50. coercedValue.push(coerceInputValueImpl(itemValue, itemType, onError, (0, _Path.addPath)(path, index)));
  51. });
  52. return coercedValue;
  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));
  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. }
  86. if ((0, _definition.isScalarType)(type)) {
  87. var parseResult; // Scalars determine if a input value is valid via parseValue(), which can
  88. // throw to indicate failure. If it throws, maintain a reference to
  89. // the original error.
  90. try {
  91. parseResult = type.parseValue(inputValue);
  92. } catch (error) {
  93. onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type ".concat(type.name, ". ") + error.message, undefined, undefined, undefined, undefined, error));
  94. return;
  95. }
  96. if (parseResult === undefined) {
  97. onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type ".concat(type.name, ".")));
  98. }
  99. return parseResult;
  100. }
  101. /* istanbul ignore else */
  102. if ((0, _definition.isEnumType)(type)) {
  103. if (typeof inputValue === 'string') {
  104. var enumValue = type.getValue(inputValue);
  105. if (enumValue) {
  106. return enumValue.value;
  107. }
  108. }
  109. var _suggestions = (0, _suggestionList.default)(String(inputValue), type.getValues().map(function (enumValue) {
  110. return enumValue.name;
  111. }));
  112. onError((0, _Path.pathToArray)(path), inputValue, new _GraphQLError.GraphQLError("Expected type ".concat(type.name, ".") + (0, _didYouMean.default)(_suggestions)));
  113. return;
  114. } // Not reachable. All possible input types have been considered.
  115. /* istanbul ignore next */
  116. (0, _invariant.default)(false, 'Unexpected input type: ' + (0, _inspect.default)(type));
  117. }