From cc2dc55fc7710b5d9a5f9a7f7a2899288eca9ad4 Mon Sep 17 00:00:00 2001 From: Jafar Rezaei Date: Sun, 21 Apr 2024 12:54:30 +0200 Subject: [PATCH] feat: response middleware extra info (#788) --- src/raw/classes/GraphQLClient.ts | 18 ++++++++++-- src/raw/helpers/types.ts | 3 +- tests/raw/general.test.ts | 50 ++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/raw/classes/GraphQLClient.ts b/src/raw/classes/GraphQLClient.ts index ce0056c92..adade7813 100644 --- a/src/raw/classes/GraphQLClient.ts +++ b/src/raw/classes/GraphQLClient.ts @@ -75,7 +75,11 @@ export class GraphQLClient { }) if (responseMiddleware) { - responseMiddleware(response) + responseMiddleware(response, { + operationName: document.operationName, + variables, + url: this.url, + }) } if (response instanceof Error) { @@ -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) { @@ -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) { diff --git a/src/raw/helpers/types.ts b/src/raw/helpers/types.ts index 949530222..ef46d3427 100644 --- a/src/raw/helpers/types.ts +++ b/src/raw/helpers/types.ts @@ -92,13 +92,14 @@ export type RequestOptions = export type ResponseMiddleware = ( response: GraphQLClientResponse | ClientError | Error, + request: RequestExtendedInit, ) => void export type RequestMiddleware = ( request: RequestExtendedInit, ) => RequestExtendedInit | Promise -type RequestExtendedInit = RequestInit & { +export type RequestExtendedInit = RequestInit & { url: string operationName?: string variables?: V diff --git a/tests/raw/general.test.ts b/tests/raw/general.test.ts index 9a0109a6a..7f5acc5f5 100644 --- a/tests/raw/general.test.ts +++ b/tests/raw/general.test.ts @@ -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`, () => {