fix(core): add timeout params to graphql request options (#10919)

Close [BS-2813](https://linear.app/affine-design/issue/BS-2813).
This commit is contained in:
akumatus
2025-03-18 03:53:47 +00:00
parent 9f3cf271e3
commit 05329e96c7
2 changed files with 10 additions and 1 deletions

View File

@@ -8,6 +8,8 @@ import type { Mutations, Queries } from './schema';
export type NotArray<T> = T extends Array<unknown> ? never : T;
export type FetchInit = RequestInit & { timeout?: number };
export type _QueryVariables<Q extends GraphQLQuery> =
Q['id'] extends Queries['name']
? Extract<Queries, { name: Q['id'] }>['variables']
@@ -75,6 +77,11 @@ export type RequestOptions<Q extends GraphQLQuery> = QueryVariablesOption<Q> & {
* @default true
*/
keepNilVariables?: boolean;
/**
* Request timeout in milliseconds
* @default 15000
*/
timeout?: number;
};
export type QueryOptions<Q extends GraphQLQuery> = RequestOptions<Q> & {
@@ -169,7 +176,7 @@ function formatRequestBody<Q extends GraphQLQuery>({
export const gqlFetcherFactory = (
endpoint: string,
fetcher: (input: string, init?: RequestInit) => Promise<Response> = fetch
fetcher: (input: string, init?: FetchInit) => Promise<Response> = fetch
) => {
const logger = new DebugLogger('GraphQL');
const gqlFetch = async <Query extends GraphQLQuery>(
@@ -199,6 +206,7 @@ export const gqlFetcherFactory = (
method: 'POST',
headers,
body: isFormData ? body : JSON.stringify(body),
timeout: options.timeout,
})
).then(async res => {
if (res.headers.get('content-type')?.startsWith('application/json')) {