Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: await response middleware #810

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]`)
})