Skip to content

Commit

Permalink
fix: await response middleware (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Apr 29, 2024
1 parent d368c16 commit 553006b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/legacy/classes/GraphQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class GraphQLClient {
})

if (responseMiddleware) {
responseMiddleware(response, {
await responseMiddleware(response, {
operationName: document.operationName,
variables,
url: this.url,
Expand Down Expand Up @@ -146,7 +146,7 @@ export class GraphQLClient {
})

if (responseMiddleware) {
responseMiddleware(response, {
await responseMiddleware(response, {
operationName: analyzedDocument.operationName,
variables: requestOptions.variables,
url: this.url,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/legacy/helpers/types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -93,7 +93,7 @@ export type RequestOptions<V extends Variables = Variables, T = unknown> =
export type ResponseMiddleware = (
response: GraphQLClientResponse<unknown> | ClientError | Error,
request: RequestExtendedInit,
) => void
) => MaybePromise<void>

export type RequestMiddleware<V extends Variables = Variables> = (
request: RequestExtendedInit<V>,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/prelude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,5 @@ export function assertObject(v: unknown): asserts v is object {
}

export type StringKeyof<T> = keyof T & string

export type MaybePromise<T> = T | Promise<T>
16 changes: 16 additions & 0 deletions tests/legacy/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -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]`)
})

0 comments on commit 553006b

Please sign in to comment.