coerceInputValue.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.coerceInputValue = coerceInputValue;
  6. var _didYouMean = require('../jsutils/didYouMean.js');
  7. var _inspect = require('../jsutils/inspect.js');
  8. var _invariant = require('../jsutils/invariant.js');
  9. var _isIterableObject = require('../jsutils/isIterableObject.js');
  10. var _isObjectLike = require('../jsutils/isObjectLike.js');
  11. var _Path = require('../jsutils/Path.js');
  12. var _printPathArray = require('../jsutils/printPathArray.js');
  13. var _suggestionList = require('../jsutils/suggestionList.js');
  14. var _GraphQLError = require('../error/GraphQLError.js');
  15. var _definition = require('../type/definition.js');
  16. /**
  17. * Coerces a JavaScript value given a GraphQL Input Type.
  18. */
  19. function coerceInputValue(inputValue, type, onError = defaultOnError) {
  20. return coerceInputValueImpl(inputValue, type, onError, undefined);
  21. }
  22. function defaultOnError(path, invalidValue, error) {
  23. let errorPrefix = 'Invalid value ' + (0, _inspect.inspect)(invalidValue);
  24. if (path.length > 0) {
  25. errorPrefix += ` at "value${(0, _printPathArray.printPathArray)(path)}"`;
  26. }
  27. error.message = errorPrefix + ': ' + error.message;
  28. throw error;
  29. }
  30. function coerceInputValueImpl(inputValue, type, onError, path) {
  31. if ((0, _definition.isNonNullType)(type)) {
  32. if (inputValue != null) {
  33. return coerceInputValueImpl(inputValue, type.ofType, onError, path);
  34. }
  35. onError(
  36. (0, _Path.pathToArray)(path),
  37. inputValue,
  38. new _GraphQLError.GraphQLError(
  39. `Expected non-nullable type "${(0, _inspect.inspect)(
  40. type,
  41. )}" not to be null.`,
  42. ),
  43. );
  44. return;
  45. }
  46. if (inputValue == null) {
  47. // Explicitly return the value null.
  48. return null;
  49. }
  50. if ((0, _definition.isListType)(type)) {
  51. const itemType = type.ofType;
  52. if ((0, _isIterableObject.isIterableObject)(inputValue)) {
  53. return Array.from(inputValue, (itemValue, index) => {
  54. const itemPath = (0, _Path.addPath)(path, index, undefined);
  55. return coerceInputValueImpl(itemValue, itemType, onError, itemPath);
  56. });
  57. } // Lists accept a non-list value as a list of one.
  58. return [coerceInputValueImpl(inputValue, itemType, onError, path)];
  59. }
  60. if ((0, _definition.isInputObjectType)(type)) {
  61. if (!(0, _isObjectLike.isObjectLike)(inputValue)) {
  62. onError(
  63. (0, _Path.pathToArray)(path),
  64. inputValue,
  65. new _GraphQLError.GraphQLError(
  66. `Expected type "${type.name}" to be an object.`,
  67. ),
  68. );
  69. return;
  70. }
  71. const coercedValue = {};
  72. const fieldDefs = type.getFields();
  73. for (const field of Object.values(fieldDefs)) {
  74. const fieldValue = inputValue[field.name];
  75. if (fieldValue === undefined) {
  76. if (field.defaultValue !== undefined) {
  77. coercedValue[field.name] = field.defaultValue;
  78. } else if ((0, _definition.isNonNullType)(field.type)) {
  79. const typeStr = (0, _inspect.inspect)(field.type);
  80. onError(
  81. (0, _Path.pathToArray)(path),
  82. inputValue,
  83. new _GraphQLError.GraphQLError(
  84. `Field "${field.name}" of required type "${typeStr}" was not provided.`,
  85. ),
  86. );
  87. }
  88. continue;
  89. }
  90. coercedValue[field.name] = coerceInputValueImpl(
  91. fieldValue,
  92. field.type,
  93. onError,
  94. (0, _Path.addPath)(path, field.name, type.name),
  95. );
  96. } // Ensure every provided field is defined.
  97. for (const fieldName of Object.keys(inputValue)) {
  98. if (!fieldDefs[fieldName]) {
  99. const suggestions = (0, _suggestionList.suggestionList)(
  100. fieldName,
  101. Object.keys(type.getFields()),
  102. );
  103. onError(
  104. (0, _Path.pathToArray)(path),
  105. inputValue,
  106. new _GraphQLError.GraphQLError(
  107. `Field "${fieldName}" is not defined by type "${type.name}".` +
  108. (0, _didYouMean.didYouMean)(suggestions),
  109. ),
  110. );
  111. }
  112. }
  113. return coercedValue;
  114. }
  115. if ((0, _definition.isLeafType)(type)) {
  116. let parseResult; // Scalars and Enums determine if a input value is valid via parseValue(),
  117. // which can throw to indicate failure. If it throws, maintain a reference
  118. // to the original error.
  119. try {
  120. parseResult = type.parseValue(inputValue);
  121. } catch (error) {
  122. if (error instanceof _GraphQLError.GraphQLError) {
  123. onError((0, _Path.pathToArray)(path), inputValue, error);
  124. } else {
  125. onError(
  126. (0, _Path.pathToArray)(path),
  127. inputValue,
  128. new _GraphQLError.GraphQLError(
  129. `Expected type "${type.name}". ` + error.message,
  130. {
  131. originalError: error,
  132. },
  133. ),
  134. );
  135. }
  136. return;
  137. }
  138. if (parseResult === undefined) {
  139. onError(
  140. (0, _Path.pathToArray)(path),
  141. inputValue,
  142. new _GraphQLError.GraphQLError(`Expected type "${type.name}".`),
  143. );
  144. }
  145. return parseResult;
  146. }
  147. /* c8 ignore next 3 */
  148. // Not reachable, all possible types have been considered.
  149. false ||
  150. (0, _invariant.invariant)(
  151. false,
  152. 'Unexpected input type: ' + (0, _inspect.inspect)(type),
  153. );
  154. }