44// SPDX-License-Identifier: AGPL-3.0-only
55// Please see LICENSE in the repository root for full details.
66
7+ import type { ExecutionResult } from "graphql" ;
78import appConfig from "./config" ;
89import type { TypedDocumentString } from "./gql/graphql" ;
910
@@ -16,19 +17,19 @@ if (import.meta.env.TEST && !window) {
1617
1718const graphqlEndpoint = new URL ( appConfig . graphqlEndpoint , base ) . toString ( ) ;
1819
19- type RequestOptions < TResult , TVariables > = {
20- query : TypedDocumentString < TResult , TVariables > ;
20+ type RequestOptions < TData , TVariables > = {
21+ query : TypedDocumentString < TData , TVariables > ;
2122 signal ?: AbortSignal ;
2223 // biome-ignore lint/suspicious/noExplicitAny: this is for inference
2324} & ( TVariables extends Record < any , never >
2425 ? { variables ?: TVariables }
2526 : { variables : TVariables } ) ;
2627
27- export const graphqlRequest = async < TResult , TVariables > ( {
28+ export const graphqlRequest = async < TData , TVariables > ( {
2829 query,
2930 variables,
3031 signal,
31- } : RequestOptions < TResult , TVariables > ) : Promise < TResult > => {
32+ } : RequestOptions < TData , TVariables > ) : Promise < TData > => {
3233 const response = await fetch ( graphqlEndpoint , {
3334 method : "POST" ,
3435 headers : {
@@ -45,10 +46,14 @@ export const graphqlRequest = async <TResult, TVariables>({
4546 throw new Error ( `GraphQL request failed: ${ response . status } ` ) ;
4647 }
4748
48- const json = await response . json ( ) ;
49+ const json : ExecutionResult < TData > = await response . json ( ) ;
4950 if ( json . errors ) {
5051 throw new Error ( JSON . stringify ( json . errors ) ) ;
5152 }
5253
54+ if ( ! json . data ) {
55+ throw new Error ( "GraphQL request returned no data" ) ;
56+ }
57+
5358 return json . data ;
5459} ;
0 commit comments