Skip to content

The open alternative to Salesforce, designed for AI.

1.2 KB
Raw Blame
import { getApplicationApiConfigOrThrow } from '@/sdk/logic-function/utils/get-application-api-config-or-throw.util';

export const postGraphqlRequest = async <TVariables, TData>({
  query,
  variables,
  caller,
  signal,
  isActingAsCaller = false,
}: {
  query: string;
  variables: TVariables;
  caller: string;
  signal?: AbortSignal;
  isActingAsCaller?: boolean;
}): Promise<TData> => {
  const { apiUrl, accessToken } = getApplicationApiConfigOrThrow({
    caller,
    isActingAsCaller,
  });

  const response = await fetch(`${apiUrl}/metadata`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${accessToken}`,
    },
    body: JSON.stringify({ query, variables }),
    signal,
  });

  if (!response.ok) {
    throw new Error(
      `${caller}() failed: HTTP ${response.status} ${response.statusText}`,
    );
  }

  const body = (await response.json()) as {
    data?: TData;
    errors?: { message: string }[];
  };

  if (body.errors && body.errors.length > 0) {
    throw new Error(
      `${caller}() failed: ${body.errors.map((error) => error.message).join(', ')}`,
    );
  }

  if (!body.data) {
    throw new Error(`${caller}() failed: response contained no data.`);
  }

  return body.data;
};