Skip to content

Commit

Permalink
fix: graphQLErrors in Error Link if networkError.result is an empty s…
Browse files Browse the repository at this point in the history
…tring (#11329)
  • Loading branch information
PaLy authored Jun 12, 2024
1 parent 1aca223 commit 3d164ea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-hounds-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Fix graphQLErrors in Error Link if networkError.result is an empty string
34 changes: 34 additions & 0 deletions src/link/error/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
{
Expand Down
7 changes: 4 additions & 3 deletions src/link/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3d164ea

Please sign in to comment.