From 18247056d1107395ce5e47a76e7c6d3b30431113 Mon Sep 17 00:00:00 2001 From: sayjeyhi Date: Fri, 19 Apr 2024 16:48:55 +0200 Subject: [PATCH 1/5] feat: add request to response middleware --- src/raw/classes/GraphQLClient.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/raw/classes/GraphQLClient.ts b/src/raw/classes/GraphQLClient.ts index ce0056c92..fb58acee4 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, + 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) { From ab99652c4cd65a91dab02f6f268a19783ce9d31e Mon Sep 17 00:00:00 2001 From: sayjeyhi Date: Fri, 19 Apr 2024 16:49:12 +0200 Subject: [PATCH 2/5] fix: types for response middleware --- src/raw/helpers/types.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/raw/helpers/types.ts b/src/raw/helpers/types.ts index 949530222..3f63de51d 100644 --- a/src/raw/helpers/types.ts +++ b/src/raw/helpers/types.ts @@ -92,6 +92,7 @@ export type RequestOptions = export type ResponseMiddleware = ( response: GraphQLClientResponse | ClientError | Error, + request: RequestExtendedInit, ) => void export type RequestMiddleware = ( From cf0f90e6a679934eebf2269a34b849b1c1b3e623 Mon Sep 17 00:00:00 2001 From: sayjeyhi Date: Fri, 19 Apr 2024 17:41:43 +0200 Subject: [PATCH 3/5] fix: export RequestExtendedInit --- src/raw/helpers/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/raw/helpers/types.ts b/src/raw/helpers/types.ts index 3f63de51d..ef46d3427 100644 --- a/src/raw/helpers/types.ts +++ b/src/raw/helpers/types.ts @@ -99,7 +99,7 @@ export type RequestMiddleware = ( request: RequestExtendedInit, ) => RequestExtendedInit | Promise -type RequestExtendedInit = RequestInit & { +export type RequestExtendedInit = RequestInit & { url: string operationName?: string variables?: V From 87084f3e2e530286336a6b110529ebcb47d23b55 Mon Sep 17 00:00:00 2001 From: sayjeyhi Date: Sun, 21 Apr 2024 11:23:53 +0200 Subject: [PATCH 4/5] fix: issue with reading variables --- src/raw/classes/GraphQLClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/raw/classes/GraphQLClient.ts b/src/raw/classes/GraphQLClient.ts index fb58acee4..adade7813 100644 --- a/src/raw/classes/GraphQLClient.ts +++ b/src/raw/classes/GraphQLClient.ts @@ -148,7 +148,7 @@ export class GraphQLClient { if (responseMiddleware) { responseMiddleware(response, { operationName: analyzedDocument.operationName, - variables, + variables: requestOptions.variables, url: this.url, }) } From 64ef7547d3c2c10bcb1dabad71ae9e8b0899b68f Mon Sep 17 00:00:00 2001 From: sayjeyhi Date: Sun, 21 Apr 2024 11:24:26 +0200 Subject: [PATCH 5/5] feat: add tests for response middleware and increase cov --- tests/raw/general.test.ts | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) 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`, () => {