123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- import { inspect } from '../jsutils/inspect.mjs';
- import { invariant } from '../jsutils/invariant.mjs';
- import { isIterableObject } from '../jsutils/isIterableObject.mjs';
- import { isObjectLike } from '../jsutils/isObjectLike.mjs';
- import { Kind } from '../language/kinds.mjs';
- import {
- isEnumType,
- isInputObjectType,
- isLeafType,
- isListType,
- isNonNullType,
- } from '../type/definition.mjs';
- import { GraphQLID } from '../type/scalars.mjs';
- export function astFromValue(value, type) {
- if (isNonNullType(type)) {
- const astValue = astFromValue(value, type.ofType);
- if (
- (astValue === null || astValue === void 0 ? void 0 : astValue.kind) ===
- Kind.NULL
- ) {
- return null;
- }
- return astValue;
- }
- if (value === null) {
- return {
- kind: Kind.NULL,
- };
- }
- if (value === undefined) {
- return null;
- }
-
- if (isListType(type)) {
- const itemType = type.ofType;
- if (isIterableObject(value)) {
- const valuesNodes = [];
- for (const item of value) {
- const itemNode = astFromValue(item, itemType);
- if (itemNode != null) {
- valuesNodes.push(itemNode);
- }
- }
- return {
- kind: Kind.LIST,
- values: valuesNodes,
- };
- }
- return astFromValue(value, itemType);
- }
-
- if (isInputObjectType(type)) {
- if (!isObjectLike(value)) {
- return null;
- }
- const fieldNodes = [];
- for (const field of Object.values(type.getFields())) {
- const fieldValue = astFromValue(value[field.name], field.type);
- if (fieldValue) {
- fieldNodes.push({
- kind: Kind.OBJECT_FIELD,
- name: {
- kind: Kind.NAME,
- value: field.name,
- },
- value: fieldValue,
- });
- }
- }
- return {
- kind: Kind.OBJECT,
- fields: fieldNodes,
- };
- }
- if (isLeafType(type)) {
-
-
- const serialized = type.serialize(value);
- if (serialized == null) {
- return null;
- }
- if (typeof serialized === 'boolean') {
- return {
- kind: Kind.BOOLEAN,
- value: serialized,
- };
- }
- if (typeof serialized === 'number' && Number.isFinite(serialized)) {
- const stringNum = String(serialized);
- return integerStringRegExp.test(stringNum)
- ? {
- kind: Kind.INT,
- value: stringNum,
- }
- : {
- kind: Kind.FLOAT,
- value: stringNum,
- };
- }
- if (typeof serialized === 'string') {
-
- if (isEnumType(type)) {
- return {
- kind: Kind.ENUM,
- value: serialized,
- };
- }
- if (type === GraphQLID && integerStringRegExp.test(serialized)) {
- return {
- kind: Kind.INT,
- value: serialized,
- };
- }
- return {
- kind: Kind.STRING,
- value: serialized,
- };
- }
- throw new TypeError(`Cannot convert value to AST: ${inspect(serialized)}.`);
- }
-
-
- false || invariant(false, 'Unexpected input type: ' + inspect(type));
- }
- const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
|