Skip to content

Commit

Permalink
feat: response middleware extra info (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
sayjeyhi authored Apr 21, 2024
1 parent 6f07a0e commit cc2dc55
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/raw/classes/GraphQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ export class GraphQLClient {
})

if (responseMiddleware) {
responseMiddleware(response)
responseMiddleware(response, {
operationName: document.operationName,
variables,
url: this.url,
})
}

if (response instanceof Error) {
Expand Down Expand Up @@ -142,7 +146,11 @@ export class GraphQLClient {
})

if (responseMiddleware) {
responseMiddleware(response)
responseMiddleware(response, {
operationName: analyzedDocument.operationName,
variables: requestOptions.variables,
url: this.url,
})
}

if (response instanceof Error) {
Expand Down Expand Up @@ -214,7 +222,11 @@ export class GraphQLClient {
})

if (this.requestConfig.responseMiddleware) {
this.requestConfig.responseMiddleware(response)
this.requestConfig.responseMiddleware(response, {
operationName: undefined,
variables,
url: this.url,
})
}

if (response instanceof Error) {
Expand Down
3 changes: 2 additions & 1 deletion src/raw/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ export type RequestOptions<V extends Variables = Variables, T = unknown> =

export type ResponseMiddleware = (
response: GraphQLClientResponse<unknown> | ClientError | Error,
request: RequestExtendedInit,
) => void

export type RequestMiddleware<V extends Variables = Variables> = (
request: RequestExtendedInit<V>,
) => RequestExtendedInit | Promise<RequestExtendedInit>

type RequestExtendedInit<V extends Variables = Variables> = RequestInit & {
export type RequestExtendedInit<V extends Variables = Variables> = RequestInit & {
url: string
operationName?: string
variables?: V
Expand Down
50 changes: 50 additions & 0 deletions tests/raw/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,56 @@ describe(`middleware`, () => {
expect(requestMiddleware).toBeCalledTimes(1)
expect(res.result).toBe(123)
})

describe(`when response middleware needs second req arg`, () => {
it(`request`, async () => {
await client.request({
document: `query x($foo: String) { foo(foo: $foo) }`,
variables: { foo: `bar` },
})
expect(responseMiddleware).toBeCalledTimes(1)

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const [_, res] = responseMiddleware.mock.calls[0]
expect(res).toMatchObject({
operationName: `x`,
url: ctx.url,
variables: { foo: `bar` },
})
})
it(`rawRequest`, async () => {
await client.rawRequest(`query x($foo: String) { foo(foo: $foo) }`, {
foo: `bar`,
})
expect(responseMiddleware).toBeCalledTimes(1)

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const [_, res] = responseMiddleware.mock.calls[0]
expect(res).toMatchObject({
operationName: `x`,
url: ctx.url,
variables: { foo: `bar` },
})
})

it(`batchRequests`, async () => {
await client.batchRequests([
{
document: `query x($foo: String) { foo(foo: $foo) }`,
variables: {foo: `bar`},
},
])
expect(responseMiddleware).toBeCalledTimes(1)

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const [_, res] = responseMiddleware.mock.calls[0]
expect(res).toMatchObject({
operationName: undefined,
url: ctx.url,
variables: [{ foo: `bar` }],
})
})
});
})

describe(`async request middleware`, () => {
Expand Down

0 comments on commit cc2dc55

Please sign in to comment.