Skip to content

Commit 3d164ea

Browse files
authored
fix: graphQLErrors in Error Link if networkError.result is an empty string (#11329)
1 parent 1aca223 commit 3d164ea

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

.changeset/loud-hounds-heal.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Fix graphQLErrors in Error Link if networkError.result is an empty string

src/link/error/__tests__/index.ts

+34
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,40 @@ describe("error handling", () => {
145145
});
146146
}
147147
);
148+
itAsync(
149+
"sets graphQLErrors to undefined if networkError.result is an empty string",
150+
(resolve, reject) => {
151+
const query = gql`
152+
query Foo {
153+
foo {
154+
bar
155+
}
156+
}
157+
`;
158+
159+
let called: boolean;
160+
const errorLink = onError(({ graphQLErrors }) => {
161+
expect(graphQLErrors).toBeUndefined();
162+
called = true;
163+
});
164+
165+
const mockLink = new ApolloLink((operation) => {
166+
return new Observable((obs) => {
167+
const response = { status: 500, ok: false } as Response;
168+
throwServerError(response, "", "app is crashing");
169+
});
170+
});
171+
172+
const link = errorLink.concat(mockLink);
173+
174+
execute(link, { query }).subscribe({
175+
error: (e) => {
176+
expect(called).toBe(true);
177+
resolve();
178+
},
179+
});
180+
}
181+
);
148182
itAsync("completes if no errors", (resolve, reject) => {
149183
const query = gql`
150184
{

src/link/error/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ export function onError(errorHandler: ErrorHandler): ApolloLink {
6060
networkError,
6161
//Network errors can return GraphQL errors on for example a 403
6262
graphQLErrors:
63-
networkError &&
64-
networkError.result &&
65-
networkError.result.errors,
63+
(networkError &&
64+
networkError.result &&
65+
networkError.result.errors) ||
66+
void 0,
6667
forward,
6768
});
6869
if (retriedResult) {

0 commit comments

Comments
 (0)