const GRAPHQL_URL = process.env.NEXT_PUBLIC_WP_GRAPHQL_URL!;

export async function fetchGraphQL(
  query: string,
  variables = {},
  options?: { cache?: RequestCache; revalidate?: number }
) {
  const res = await fetch(GRAPHQL_URL, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ query, variables }),

    // 👇 IMPORTANT
    ...(options?.cache === "no-store"
      ? { cache: "no-store" }
      : { next: { revalidate: options?.revalidate ?? 60 } }),
  });

  const json = await res.json();
  return json.data;
}
