Skip to content

Commit f6d387c

Browse files
authored
Fix networkStatus incorrectly reported as ready when using errorPolicy: 'all' with errors (#12362)
1 parent 50f15a3 commit f6d387c

File tree

6 files changed

+35
-11
lines changed

6 files changed

+35
-11
lines changed

.changeset/khaki-cheetahs-lick.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Fixes an issue where calling `observableQuery.getCurrentResult()` when the `errorPolicy` was set to `all` would return the `networkStatus` as `NetworkStatus.ready` when there were errors returned in the result. This has been corrected to report `NetworkStatus.error`.
6+
7+
This bug also affected the `useQuery` and `useLazyQuery` hooks and may affect you if you check for `networkStatus` in your component.

.size-limits.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"dist/apollo-client.min.cjs": 42243,
3-
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34437
2+
"dist/apollo-client.min.cjs": 42260,
3+
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34450
44
}

src/core/ObservableQuery.ts

+11
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ export class ObservableQuery<
303303
result.partial = true;
304304
}
305305

306+
// We need to check for both both `error` and `errors` field because there
307+
// are cases where sometimes `error` is set, but not `errors` and
308+
// vice-versa. This will be updated in the next major version when
309+
// `errors` is deprecated in favor of `error`.
310+
if (
311+
result.networkStatus === NetworkStatus.ready &&
312+
(result.error || result.errors)
313+
) {
314+
result.networkStatus = NetworkStatus.error;
315+
}
316+
306317
if (
307318
__DEV__ &&
308319
!diff.complete &&

src/core/__tests__/ObservableQuery.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ describe("ObservableQuery", () => {
10031003
data: undefined,
10041004
errors: [error],
10051005
loading: false,
1006-
networkStatus: NetworkStatus.ready,
1006+
networkStatus: NetworkStatus.error,
10071007
// TODO: This is not present on the emitted result so this should match
10081008
partial: true,
10091009
});
@@ -2279,6 +2279,7 @@ describe("ObservableQuery", () => {
22792279
const result = await observable.result();
22802280
const currentResult = observable.getCurrentResult();
22812281

2282+
// TODO: This should include an `error` property, not just `errors`
22822283
expect(result).toEqualApolloQueryResult({
22832284
data: dataOne,
22842285
errors: [error],
@@ -2289,9 +2290,7 @@ describe("ObservableQuery", () => {
22892290
data: dataOne,
22902291
errors: [error],
22912292
loading: false,
2292-
// TODO: The networkStatus returned here is different than the one
2293-
// returned from `observable.result()`. These should match
2294-
networkStatus: NetworkStatus.ready,
2293+
networkStatus: NetworkStatus.error,
22952294
});
22962295
});
22972296

@@ -2497,6 +2496,11 @@ describe("ObservableQuery", () => {
24972496
loading: false,
24982497
networkStatus: NetworkStatus.ready,
24992498
});
2499+
expect(observable.getCurrentResult()).toEqualApolloQueryResult({
2500+
data: dataTwo,
2501+
loading: false,
2502+
networkStatus: NetworkStatus.ready,
2503+
});
25002504

25012505
await expect(stream).not.toEmitAnything();
25022506
});

src/react/hooks/__tests__/useLazyQuery.test.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,8 @@ describe("useLazyQuery Hook", () => {
15331533
variables: {},
15341534
});
15351535
}
1536+
1537+
await expect(takeSnapshot).not.toRerender();
15361538
});
15371539

15381540
it("the promise should not cause an unhandled rejection", async () => {

src/react/hooks/__tests__/useQuery.test.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -3971,7 +3971,7 @@ describe("useQuery Hook", () => {
39713971
errors: [{ message: "error" }],
39723972
called: true,
39733973
loading: false,
3974-
networkStatus: NetworkStatus.ready,
3974+
networkStatus: NetworkStatus.error,
39753975
previousData: undefined,
39763976
variables: {},
39773977
});
@@ -4039,7 +4039,7 @@ describe("useQuery Hook", () => {
40394039
errors: [{ message: 'Could not fetch "hello"' }],
40404040
called: true,
40414041
loading: false,
4042-
networkStatus: NetworkStatus.ready,
4042+
networkStatus: NetworkStatus.error,
40434043
previousData: undefined,
40444044
variables: {},
40454045
});
@@ -4103,7 +4103,7 @@ describe("useQuery Hook", () => {
41034103
errors: [{ message: 'Could not fetch "hello"' }],
41044104
called: true,
41054105
loading: false,
4106-
networkStatus: NetworkStatus.ready,
4106+
networkStatus: NetworkStatus.error,
41074107
previousData: undefined,
41084108
variables: {},
41094109
});
@@ -12025,7 +12025,7 @@ describe("useQuery Hook", () => {
1202512025
],
1202612026
called: true,
1202712027
loading: false,
12028-
networkStatus: NetworkStatus.ready,
12028+
networkStatus: NetworkStatus.error,
1202912029
previousData: {
1203012030
hero: {
1203112031
heroFriends: [
@@ -13506,7 +13506,7 @@ describe("useQuery Hook", () => {
1350613506
errors: [{ message: "Couldn't get name" }],
1350713507
called: true,
1350813508
loading: false,
13509-
networkStatus: NetworkStatus.ready,
13509+
networkStatus: NetworkStatus.error,
1351013510
previousData: undefined,
1351113511
variables: {},
1351213512
});

0 commit comments

Comments
 (0)