Skip to content

The open alternative to Salesforce, designed for AI.

731 B
Raw Blame
import { type ArgumentNode, valueFromASTUntyped } from 'graphql';
import { isDefined } from 'twenty-shared/utils';

// Converts GraphQL AST argument nodes into a plain JS object,
// resolving variable references from the variables map.
export const extractArgumentsFromAst = (
  argumentNodes: readonly ArgumentNode[] | undefined,
  variables: Record<string, unknown> | undefined,
): Record<string, unknown> => {
  if (!argumentNodes || argumentNodes.length === 0) {
    return {};
  }

  const result: Record<string, unknown> = {};

  for (const arg of argumentNodes) {
    const value = valueFromASTUntyped(arg.value, variables);

    if (!isDefined(value)) continue;
    result[arg.name.value] = value;
  }

  return result;
};