Skip to content

Commit

Permalink
Fix networkStatus update issue in useSuspenseQuery with `cache-an…
Browse files Browse the repository at this point in the history
…d-network` policy (#11489)
  • Loading branch information
gronxb committed Jan 17, 2024
1 parent da1e82e commit abfd02a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-roses-sit.md
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Fix `networkStatus` with `useSuspenseQuery` not properly updating to ready state when using a `cache-and-network` fetch policy that returns data equal to what is already in the cache.
4 changes: 2 additions & 2 deletions .size-limits.json
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 37922,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 31974
"dist/apollo-client.min.cjs": 37930,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 31972
}
5 changes: 4 additions & 1 deletion src/react/cache/QueryReference.ts
Expand Up @@ -238,7 +238,10 @@ export class InternalQueryReference<TData = unknown> {
// This occurs when switching to a result that is fully cached when this
// class is instantiated. ObservableQuery will run reobserve when
// subscribing, which delivers a result from the cache.
if (result.data === this.result.data) {
if (
result.data === this.result.data &&
result.networkStatus === this.result.networkStatus
) {
return;
}

Expand Down
47 changes: 47 additions & 0 deletions src/react/hooks/__tests__/useSuspenseQuery.test.tsx
Expand Up @@ -9932,6 +9932,53 @@ describe("useSuspenseQuery", () => {
});
});

it("updates networkStatus when a network request returns the same cached data with 'cache-and-network' fetchPolicy", async () => {
const { query } = useSimpleQueryCase();

const link = new ApolloLink(() => {
return new Observable((observer) => {
setTimeout(() => {
observer.next({ data: { greeting: "Hello" } });
observer.complete();
}, 10);
});
});

const client = new ApolloClient({
link,
cache: new InMemoryCache(),
});

// preloaded cache
await client.writeQuery({ query, data: { greeting: "Hello" } });

const { result } = renderSuspenseHook(
() =>
useSuspenseQuery(query, {
fetchPolicy: "cache-and-network",
}),
{ client }
);

await waitFor(() => {
// We should see the cached greeting while the network request is in flight
// and the network status should be set to `loading`.
expect(result.current).toMatchObject({
data: { greeting: "Hello" },
networkStatus: NetworkStatus.loading,
});
});

await waitFor(() => {
// We should see the updated greeting once the network request finishes
// and the network status should be set to `ready`.
expect(result.current).toMatchObject({
data: { greeting: "Hello" },
networkStatus: NetworkStatus.ready,
});
});
});

describe.skip("type tests", () => {
it("returns unknown when TData cannot be inferred", () => {
const query = gql`
Expand Down

0 comments on commit abfd02a

Please sign in to comment.