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

feat: response middleware extra info #788

Merged
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
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
Loading