Skip to content

Commit e14205a

Browse files
authored
Emit initial loading state when notifyOnNetworkStatusChange is true (#12536)
1 parent e809b71 commit e14205a

26 files changed

+2847
-643
lines changed

Diff for: .changeset/forty-tomatoes-punch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
The returned `networkStatus` in `useLazyQuery` is now set to `setVariables` when calling the `useLazyQuery` `execute` function for the first time with variables.

Diff for: .changeset/fuzzy-tips-sit.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Ensure `ObservableQuery` stops polling if switching to a `standby` `fetchPolicy`. When switching back to a non-`standby` `fetchPolicy`, polling will resume.

Diff for: .changeset/khaki-keys-deliver.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": major
3+
---
4+
5+
An initial loading state is now emitted from `ObservableQuery` when subscribing if `notifyOnNetworkStatusChange` is set to `true`.

Diff for: .changeset/large-plants-know.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Ensure a loading state is emitted when calling the `execute` function after changing clients in `useLazyQuery`.

Diff for: .size-limits.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 42852,
3-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38311,
4-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 32787,
5-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27726
2+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 42984,
3+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38432,
4+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 32968,
5+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27861
66
}

Diff for: src/__tests__/ApolloClient.ts

+53-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { TypedDocumentNode } from "@graphql-typed-document-node/core";
22
import { expectTypeOf } from "expect-type";
33
import { Kind } from "graphql";
44
import { gql } from "graphql-tag";
5-
import { Observable, of } from "rxjs";
5+
import { delay, Observable, of } from "rxjs";
66

77
import type {
88
ApolloQueryResult,
@@ -1163,7 +1163,7 @@ describe("ApolloClient", () => {
11631163
},
11641164
};
11651165
const link = new ApolloLink(() => {
1166-
return of({ data });
1166+
return of({ data }).pipe(delay(20));
11671167
});
11681168
function newClient() {
11691169
return new ApolloClient({
@@ -1216,6 +1216,13 @@ describe("ApolloClient", () => {
12161216
const observable = client.watchQuery<Data>({ query });
12171217
const stream = new ObservableStream(observable);
12181218

1219+
await expect(stream).toEmitTypedValue({
1220+
data: undefined,
1221+
loading: true,
1222+
networkStatus: NetworkStatus.loading,
1223+
partial: true,
1224+
});
1225+
12191226
await expect(stream).toEmitTypedValue({
12201227
data,
12211228
loading: false,
@@ -1265,6 +1272,13 @@ describe("ApolloClient", () => {
12651272
const observable = client.watchQuery<Data>({ query });
12661273
const stream = new ObservableStream(observable);
12671274

1275+
await expect(stream).toEmitTypedValue({
1276+
data: undefined,
1277+
loading: true,
1278+
networkStatus: NetworkStatus.loading,
1279+
partial: true,
1280+
});
1281+
12681282
await expect(stream).toEmitTypedValue({
12691283
data,
12701284
loading: false,
@@ -1321,6 +1335,13 @@ describe("ApolloClient", () => {
13211335
const observable = client.watchQuery<Data>({ query });
13221336
const stream = new ObservableStream(observable);
13231337

1338+
await expect(stream).toEmitTypedValue({
1339+
data: undefined,
1340+
loading: true,
1341+
networkStatus: NetworkStatus.loading,
1342+
partial: true,
1343+
});
1344+
13241345
{
13251346
const result = await stream.takeNext();
13261347

@@ -1359,6 +1380,13 @@ describe("ApolloClient", () => {
13591380
const observable = client.watchQuery<Data>({ query });
13601381
const stream = new ObservableStream(observable);
13611382

1383+
await expect(stream).toEmitTypedValue({
1384+
data: undefined,
1385+
loading: true,
1386+
networkStatus: NetworkStatus.loading,
1387+
partial: true,
1388+
});
1389+
13621390
{
13631391
const result = await stream.takeNext();
13641392

@@ -2125,7 +2153,6 @@ describe("ApolloClient", () => {
21252153
const observable = client.watchQuery({
21262154
query,
21272155
fetchPolicy,
2128-
notifyOnNetworkStatusChange: true,
21292156
});
21302157
const stream = new ObservableStream(observable);
21312158
await expect(stream).toEmitTypedValue({
@@ -2158,12 +2185,16 @@ describe("ApolloClient", () => {
21582185
});
21592186
client.prioritizeCacheValues = true;
21602187

2161-
const observable = client.watchQuery({
2162-
query,
2163-
fetchPolicy,
2164-
notifyOnNetworkStatusChange: true,
2165-
});
2188+
const observable = client.watchQuery({ query, fetchPolicy });
21662189
const stream = new ObservableStream(observable);
2190+
2191+
await expect(stream).toEmitTypedValue({
2192+
data: undefined,
2193+
loading: true,
2194+
networkStatus: NetworkStatus.loading,
2195+
partial: true,
2196+
});
2197+
21672198
await expect(stream).toEmitTypedValue({
21682199
loading: false,
21692200
networkStatus: NetworkStatus.ready,
@@ -2197,8 +2228,7 @@ describe("ApolloClient", () => {
21972228
notifyOnNetworkStatusChange: true,
21982229
});
21992230
const stream = new ObservableStream(observable);
2200-
// not really part of this test case but I decided to leave it here to highlight this
2201-
// in the case of a network request, `no-cache` emits a `loading` state while `network-only` etc. do not?
2231+
22022232
await expect(stream).toEmitTypedValue({
22032233
loading: true,
22042234
networkStatus: NetworkStatus.loading,
@@ -2957,7 +2987,19 @@ describe("ApolloClient", () => {
29572987
const stream = new ObservableStream(observable);
29582988
link.simulateResult({ result: { data: { foo: { bar: 1 } } } }, true);
29592989

2960-
await stream.takeNext();
2990+
await expect(stream).toEmitTypedValue({
2991+
data: undefined,
2992+
loading: true,
2993+
networkStatus: NetworkStatus.loading,
2994+
partial: true,
2995+
});
2996+
2997+
await expect(stream).toEmitTypedValue({
2998+
data: { foo: { bar: 1 } },
2999+
loading: false,
3000+
networkStatus: NetworkStatus.ready,
3001+
partial: false,
3002+
});
29613003

29623004
const result = client.refetchQueries({
29633005
include: "all",

0 commit comments

Comments
 (0)