diff --git a/src/legacy/classes/GraphQLClient.ts b/src/legacy/classes/GraphQLClient.ts index adade7813..bf844163c 100644 --- a/src/legacy/classes/GraphQLClient.ts +++ b/src/legacy/classes/GraphQLClient.ts @@ -75,7 +75,7 @@ export class GraphQLClient { }) if (responseMiddleware) { - responseMiddleware(response, { + await responseMiddleware(response, { operationName: document.operationName, variables, url: this.url, @@ -146,7 +146,7 @@ export class GraphQLClient { }) if (responseMiddleware) { - responseMiddleware(response, { + await responseMiddleware(response, { operationName: analyzedDocument.operationName, variables: requestOptions.variables, url: this.url, @@ -222,7 +222,7 @@ export class GraphQLClient { }) if (this.requestConfig.responseMiddleware) { - this.requestConfig.responseMiddleware(response, { + await this.requestConfig.responseMiddleware(response, { operationName: undefined, variables, url: this.url, diff --git a/src/legacy/helpers/types.ts b/src/legacy/helpers/types.ts index ef46d3427..c440a9dff 100644 --- a/src/legacy/helpers/types.ts +++ b/src/legacy/helpers/types.ts @@ -1,7 +1,7 @@ import type { TypedDocumentNode } from '@graphql-typed-document-node/core' import type { GraphQLError } from 'graphql/error/GraphQLError.js' import type { DocumentNode } from 'graphql/language/ast.js' -import type { MaybeLazy, RemoveIndex } from '../../lib/prelude.js' +import type { MaybeLazy, MaybePromise, RemoveIndex } from '../../lib/prelude.js' import type { ClientError } from '../classes/ClientError.js' export type Fetch = typeof fetch @@ -93,7 +93,7 @@ export type RequestOptions = export type ResponseMiddleware = ( response: GraphQLClientResponse | ClientError | Error, request: RequestExtendedInit, -) => void +) => MaybePromise export type RequestMiddleware = ( request: RequestExtendedInit, diff --git a/src/lib/prelude.ts b/src/lib/prelude.ts index 78832225c..3f8f04201 100644 --- a/src/lib/prelude.ts +++ b/src/lib/prelude.ts @@ -247,3 +247,5 @@ export function assertObject(v: unknown): asserts v is object { } export type StringKeyof = keyof T & string + +export type MaybePromise = T | Promise diff --git a/tests/legacy/middleware.test.ts b/tests/legacy/middleware.test.ts new file mode 100644 index 000000000..3a3d9bc8c --- /dev/null +++ b/tests/legacy/middleware.test.ts @@ -0,0 +1,16 @@ +import { expect, test } from 'vitest' +import { GraphQLClient } from '../../src/entrypoints/main.js' +import { setupMockServer } from './__helpers.js' + +const ctx = setupMockServer() + +test(`throwing an error in response middleware is not swalled`, async () => { + const client = new GraphQLClient(ctx.url, { + // eslint-disable-next-line + responseMiddleware: async () => { + throw new Error(`TEST ERROR`) + }, + }) + ctx.res() + await expect(client.request(`{ me { id } }`)).rejects.toMatchInlineSnapshot(`[Error: TEST ERROR]`) +})