mirror of
https://github.com/supabase/supabase.git
synced 2026-05-08 01:40:13 -04:00
35 lines
806 B
TypeScript
35 lines
806 B
TypeScript
import type { TypedDocumentString } from './graphql'
|
|
import { handleError } from '@/data/fetchers'
|
|
|
|
export async function executeGraphQL<TResult, TVariables>(
|
|
query: TypedDocumentString<TResult, TVariables>,
|
|
{ variables, signal }: { variables?: TVariables; signal?: AbortSignal }
|
|
) {
|
|
try {
|
|
const response = await fetch('/api/content/graphql', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
query,
|
|
variables,
|
|
}),
|
|
signal,
|
|
})
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed network response from Content API')
|
|
}
|
|
|
|
const { data, errors } = await response.json()
|
|
if (errors) {
|
|
throw errors
|
|
}
|
|
|
|
return data as TResult
|
|
} catch (err) {
|
|
handleError(err)
|
|
}
|
|
}
|