* app tier over the Docker-internal network), the public value once
* running in the browser (must be reachable from the user's machine). */
export function useGraphqlUrl(): string {
const config = useRuntimeConfig()
return import.meta.server ? config.graphqlUrl : config.public.graphqlUrl
}
interface GraphqlResponse<T> {
data?: T
errors?: Array<{ message: string }>
}
export async function graphqlRequest<T>(query: string, variables?: Record<string, unknown>): Promise<T> {
const url = useGraphqlUrl()
const res = await $fetch<GraphqlResponse<T>>(url, {
method: 'POST',
body: { query, variables }
})
if (res.errors?.length) {
throw new Error(res.errors.map((e) => e.message).join('; '))
}
if (!res.data) {
throw new Error('GraphQL response had no data')
}
return res.data
}