From 3d164ea16c17d271f6fa9e5ad8f013623eec23a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavol=20Luk=C4=8Da?= Date: Wed, 12 Jun 2024 20:08:48 +0200 Subject: [PATCH] fix: graphQLErrors in Error Link if networkError.result is an empty string (#11329) --- .changeset/loud-hounds-heal.md | 5 +++++ src/link/error/__tests__/index.ts | 34 +++++++++++++++++++++++++++++++ src/link/error/index.ts | 7 ++++--- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 .changeset/loud-hounds-heal.md diff --git a/.changeset/loud-hounds-heal.md b/.changeset/loud-hounds-heal.md new file mode 100644 index 00000000000..f80a0a34105 --- /dev/null +++ b/.changeset/loud-hounds-heal.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Fix graphQLErrors in Error Link if networkError.result is an empty string diff --git a/src/link/error/__tests__/index.ts b/src/link/error/__tests__/index.ts index a6189ee3b87..5e886ff58d3 100644 --- a/src/link/error/__tests__/index.ts +++ b/src/link/error/__tests__/index.ts @@ -145,6 +145,40 @@ describe("error handling", () => { }); } ); + itAsync( + "sets graphQLErrors to undefined if networkError.result is an empty string", + (resolve, reject) => { + const query = gql` + query Foo { + foo { + bar + } + } + `; + + let called: boolean; + const errorLink = onError(({ graphQLErrors }) => { + expect(graphQLErrors).toBeUndefined(); + called = true; + }); + + const mockLink = new ApolloLink((operation) => { + return new Observable((obs) => { + const response = { status: 500, ok: false } as Response; + throwServerError(response, "", "app is crashing"); + }); + }); + + const link = errorLink.concat(mockLink); + + execute(link, { query }).subscribe({ + error: (e) => { + expect(called).toBe(true); + resolve(); + }, + }); + } + ); itAsync("completes if no errors", (resolve, reject) => { const query = gql` { diff --git a/src/link/error/index.ts b/src/link/error/index.ts index 7122ff792e8..00b0701ab6f 100644 --- a/src/link/error/index.ts +++ b/src/link/error/index.ts @@ -60,9 +60,10 @@ export function onError(errorHandler: ErrorHandler): ApolloLink { networkError, //Network errors can return GraphQL errors on for example a 403 graphQLErrors: - networkError && - networkError.result && - networkError.result.errors, + (networkError && + networkError.result && + networkError.result.errors) || + void 0, forward, }); if (retriedResult) {