Files
Dani Akash 290ee91a8b Add 'packages/browseros-agent/' from commit '90bd4be3008285bf3825aad3702aff98f872671a'
git-subtree-dir: packages/browseros-agent
git-subtree-mainline: 8f148d0918
git-subtree-split: 90bd4be300
2026-03-13 21:22:09 +05:30

40 lines
1.1 KiB
TypeScript

import type { TypedDocumentString } from '@/generated/graphql/graphql'
import { env } from '../env'
export async function execute<TResult, TVariables = undefined>(
query: TypedDocumentString<TResult, TVariables>,
variables?: TVariables,
): Promise<TResult> {
const headers = new Headers()
headers.set('Content-Type', 'application/json')
headers.set('Accept', 'application/graphql-response+json')
const response = await fetch(`${env.VITE_PUBLIC_BROWSEROS_API}/graphql`, {
method: 'POST',
headers,
body: JSON.stringify({
query,
variables,
}),
credentials: 'include',
})
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`)
}
const body: { data?: TResult; errors?: { message: string }[] } =
await response.json()
if (body.errors && body.errors.length > 0) {
const messages = body.errors.map((e) => e.message)
throw new Error(`GraphQL error: ${messages.join(', ')}`)
}
if (!body.data) {
throw new Error('GraphQL response is missing data.')
}
return body.data
}