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) {