import { inspect } from '../jsutils/inspect.js';
import { invariant } from '../jsutils/invariant.js';
import { keyMap } from '../jsutils/keyMap.js';
import type { Maybe } from '../jsutils/Maybe.js';
import type { ObjMap } from '../jsutils/ObjMap.js';
import type { ValueNode } from '../language/ast.js';
import { Kind } from '../language/kinds.js';
import type { GraphQLInputType } from '../type/definition.js';
import {
isInputObjectType,
isLeafType,
isListType,
isNonNullType,
} from '../type/definition.js';
export function valueFromAST(
valueNode: Maybe<ValueNode>,
type: GraphQLInputType,
variables?: Maybe<ObjMap<unknown>>,
): unknown {
if (!valueNode) {
return;
}
if (valueNode.kind === Kind.VARIABLE) {
const variableName = valueNode.name.value;
if (variables == null || variables[variableName] === undefined) {
return;
}
const variableValue = variables[variableName];
if (variableValue === null && isNonNullType(type)) {
return;
}
return variableValue;
}
if (isNonNullType(type)) {
if (valueNode.kind === Kind.NULL) {
return;
}
return valueFromAST(valueNode, type.ofType, variables);
}
if (valueNode.kind === Kind.NULL) {
return null;
}
if (isListType(type)) {
const itemType = type.ofType;
if (valueNode.kind === Kind.LIST) {
const coercedValues = [];
for (const itemNode of valueNode.values) {
if (isMissingVariable(itemNode, variables)) {
if (isNonNullType(itemType)) {
return;
}
coercedValues.push(null);
} else {
const itemValue = valueFromAST(itemNode, itemType, variables);
if (itemValue === undefined) {
return;
}
coercedValues.push(itemValue);
}
}
return coercedValues;
}
const coercedValue = valueFromAST(valueNode, itemType, variables);
if (coercedValue === undefined) {
return;
}
return [coercedValue];
}
if (isInputObjectType(type)) {
if (valueNode.kind !== Kind.OBJECT) {
return;
}
const coercedObj = Object.create(null);
const fieldNodes = keyMap(valueNode.fields, (field) => field.name.value);
for (const field of Object.values(type.getFields())) {
const fieldNode = fieldNodes[field.name];
if (!fieldNode || isMissingVariable(fieldNode.value, variables)) {
if (field.defaultValue !== undefined) {
coercedObj[field.name] = field.defaultValue;
} else if (isNonNullType(field.type)) {
return;
}
continue;
}
const fieldValue = valueFromAST(fieldNode.value, field.type, variables);
if (fieldValue === undefined) {
return;
}
coercedObj[field.name] = fieldValue;
}
return coercedObj;
}
if (isLeafType(type)) {
let result;
try {
result = type.parseLiteral(valueNode, variables);
} catch (_error) {
return;
}
if (result === undefined) {
return;
}
return result;
}
invariant(false, 'Unexpected input type: ' + inspect(type));
}
function isMissingVariable(
valueNode: ValueNode,
variables: Maybe<ObjMap<unknown>>,
): boolean {
return (
valueNode.kind === Kind.VARIABLE &&
(variables == null || variables[valueNode.name.value] === undefined)
);
}