astFromValue.mjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import isFinite from "../polyfills/isFinite.mjs";
  2. import objectValues from "../polyfills/objectValues.mjs";
  3. import inspect from "../jsutils/inspect.mjs";
  4. import invariant from "../jsutils/invariant.mjs";
  5. import isObjectLike from "../jsutils/isObjectLike.mjs";
  6. import safeArrayFrom from "../jsutils/safeArrayFrom.mjs";
  7. import { Kind } from "../language/kinds.mjs";
  8. import { GraphQLID } from "../type/scalars.mjs";
  9. import { isLeafType, isEnumType, isInputObjectType, isListType, isNonNullType } from "../type/definition.mjs";
  10. /**
  11. * Produces a GraphQL Value AST given a JavaScript object.
  12. * Function will match JavaScript/JSON values to GraphQL AST schema format
  13. * by using suggested GraphQLInputType. For example:
  14. *
  15. * astFromValue("value", GraphQLString)
  16. *
  17. * A GraphQL type must be provided, which will be used to interpret different
  18. * JavaScript values.
  19. *
  20. * | JSON Value | GraphQL Value |
  21. * | ------------- | -------------------- |
  22. * | Object | Input Object |
  23. * | Array | List |
  24. * | Boolean | Boolean |
  25. * | String | String / Enum Value |
  26. * | Number | Int / Float |
  27. * | Mixed | Enum Value |
  28. * | null | NullValue |
  29. *
  30. */
  31. export function astFromValue(value, type) {
  32. if (isNonNullType(type)) {
  33. var astValue = astFromValue(value, type.ofType);
  34. if ((astValue === null || astValue === void 0 ? void 0 : astValue.kind) === Kind.NULL) {
  35. return null;
  36. }
  37. return astValue;
  38. } // only explicit null, not undefined, NaN
  39. if (value === null) {
  40. return {
  41. kind: Kind.NULL
  42. };
  43. } // undefined
  44. if (value === undefined) {
  45. return null;
  46. } // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
  47. // the value is not an array, convert the value using the list's item type.
  48. if (isListType(type)) {
  49. var itemType = type.ofType;
  50. var items = safeArrayFrom(value);
  51. if (items != null) {
  52. var valuesNodes = [];
  53. for (var _i2 = 0; _i2 < items.length; _i2++) {
  54. var item = items[_i2];
  55. var itemNode = astFromValue(item, itemType);
  56. if (itemNode != null) {
  57. valuesNodes.push(itemNode);
  58. }
  59. }
  60. return {
  61. kind: Kind.LIST,
  62. values: valuesNodes
  63. };
  64. }
  65. return astFromValue(value, itemType);
  66. } // Populate the fields of the input object by creating ASTs from each value
  67. // in the JavaScript object according to the fields in the input type.
  68. if (isInputObjectType(type)) {
  69. if (!isObjectLike(value)) {
  70. return null;
  71. }
  72. var fieldNodes = [];
  73. for (var _i4 = 0, _objectValues2 = objectValues(type.getFields()); _i4 < _objectValues2.length; _i4++) {
  74. var field = _objectValues2[_i4];
  75. var fieldValue = astFromValue(value[field.name], field.type);
  76. if (fieldValue) {
  77. fieldNodes.push({
  78. kind: Kind.OBJECT_FIELD,
  79. name: {
  80. kind: Kind.NAME,
  81. value: field.name
  82. },
  83. value: fieldValue
  84. });
  85. }
  86. }
  87. return {
  88. kind: Kind.OBJECT,
  89. fields: fieldNodes
  90. };
  91. } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
  92. if (isLeafType(type)) {
  93. // Since value is an internally represented value, it must be serialized
  94. // to an externally represented value before converting into an AST.
  95. var serialized = type.serialize(value);
  96. if (serialized == null) {
  97. return null;
  98. } // Others serialize based on their corresponding JavaScript scalar types.
  99. if (typeof serialized === 'boolean') {
  100. return {
  101. kind: Kind.BOOLEAN,
  102. value: serialized
  103. };
  104. } // JavaScript numbers can be Int or Float values.
  105. if (typeof serialized === 'number' && isFinite(serialized)) {
  106. var stringNum = String(serialized);
  107. return integerStringRegExp.test(stringNum) ? {
  108. kind: Kind.INT,
  109. value: stringNum
  110. } : {
  111. kind: Kind.FLOAT,
  112. value: stringNum
  113. };
  114. }
  115. if (typeof serialized === 'string') {
  116. // Enum types use Enum literals.
  117. if (isEnumType(type)) {
  118. return {
  119. kind: Kind.ENUM,
  120. value: serialized
  121. };
  122. } // ID types can use Int literals.
  123. if (type === GraphQLID && integerStringRegExp.test(serialized)) {
  124. return {
  125. kind: Kind.INT,
  126. value: serialized
  127. };
  128. }
  129. return {
  130. kind: Kind.STRING,
  131. value: serialized
  132. };
  133. }
  134. throw new TypeError("Cannot convert value to AST: ".concat(inspect(serialized), "."));
  135. } // istanbul ignore next (Not reachable. All possible input types have been considered)
  136. false || invariant(0, 'Unexpected input type: ' + inspect(type));
  137. }
  138. /**
  139. * IntValue:
  140. * - NegativeSign? 0
  141. * - NegativeSign? NonZeroDigit ( Digit+ )?
  142. */
  143. var integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;