Skip to content

Commit 837779a

Browse files
authored
Fix issue where data would disappear after refreshing the page (#1454)
1 parent 043cc6c commit 837779a

File tree

4 files changed

+22
-67
lines changed

4 files changed

+22
-67
lines changed

.changeset/nine-walls-bathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"apollo-client-devtools": patch
3+
---
4+
5+
Fix an issue where data would sometimes disappear a few seconds after refreshing the page with no way to recover the data afterward.

src/application/App.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { isSnapshotRelease, parseSnapshotRelease } from "./utilities/github";
3636
import { Select } from "./components/Select";
3737
import { Divider } from "./components/Divider";
3838
import { useActorEvent } from "./hooks/useActorEvent";
39-
import { addClient, removeClient } from ".";
39+
import { removeClient } from ".";
4040

4141
const APP_QUERY: TypedDocumentNode<AppQuery, AppQueryVariables> = gql`
4242
query AppQuery {
@@ -74,22 +74,28 @@ export const App = () => {
7474
devtoolsMachine.provide({
7575
actions: {
7676
resetStore: () => {
77-
apolloClient.resetStore().catch(() => {});
77+
apolloClient.clearStore().catch(() => {});
7878
},
7979
},
8080
})
8181
);
82-
const { data, client: apolloClient } = useQuery(APP_QUERY, {
83-
errorPolicy: "all",
84-
});
82+
const {
83+
data,
84+
client: apolloClient,
85+
refetch,
86+
} = useQuery(APP_QUERY, { errorPolicy: "all" });
8587

8688
useActorEvent("connectToDevtools", () => {
8789
send({ type: "connect" });
8890
});
8991

90-
useActorEvent("registerClient", (message) => {
92+
useActorEvent("registerClient", () => {
9193
send({ type: "connect" });
92-
addClient(message.payload);
94+
// Unfortunately after we clear the store above, the query ends up "stuck"
95+
// holding onto the old list of clients even if we manually write a cache
96+
// update to properly resolve the list. Instead we refetch the list again to
97+
// force the query to reload to ensure its in sync with the latest values.
98+
refetch();
9399
});
94100

95101
useActorEvent("clientTerminated", (message) => {

src/application/index.tsx

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { useEffect } from "react";
22
import { createRoot } from "react-dom/client";
3-
import type { Reference, TypedDocumentNode } from "@apollo/client";
4-
import {
5-
ApolloClient,
6-
ApolloProvider,
7-
InMemoryCache,
8-
gql,
9-
} from "@apollo/client";
3+
import type { Reference } from "@apollo/client";
4+
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";
105
import { SchemaLink } from "@apollo/client/link/schema";
116

127
import { colorTheme, listenForThemeChange } from "./theme";
@@ -16,8 +11,6 @@ import * as Tooltip from "@radix-ui/react-tooltip";
1611

1712
import { getRpcClient } from "../extension/devtools/panelRpcClient";
1813
import { createSchemaWithRpcClient } from "./schema";
19-
import type { ApolloClientInfo } from "../types";
20-
import type { ClientFields } from "./types/gql";
2114

2215
const rpcClient = getRpcClient();
2316
const schema = createSchemaWithRpcClient(rpcClient);
@@ -51,9 +44,11 @@ const cache = new InMemoryCache({
5144
},
5245
},
5346
ClientQueries: {
47+
keyFields: false,
5448
merge: true,
5549
},
5650
ClientMutations: {
51+
keyFields: false,
5752
merge: true,
5853
},
5954
SerializedApolloError: {
@@ -67,49 +62,6 @@ const cache = new InMemoryCache({
6762

6863
export const client = new ApolloClient({ cache, link });
6964

70-
export const addClient = (clientData: ApolloClientInfo) => {
71-
client.cache.modify({
72-
id: "ROOT_QUERY",
73-
fields: {
74-
clients: (clients) => {
75-
const ref = client.writeFragment({
76-
fragment: gql`
77-
fragment ClientFields on Client {
78-
id
79-
version
80-
queries {
81-
total
82-
}
83-
mutations {
84-
total
85-
}
86-
}
87-
` as TypedDocumentNode<ClientFields>,
88-
id: client.cache.identify({
89-
__typename: "Client",
90-
id: clientData.id,
91-
}),
92-
data: {
93-
__typename: "Client",
94-
id: clientData.id,
95-
version: clientData.version,
96-
queries: {
97-
__typename: "ClientQueries",
98-
total: clientData.queryCount,
99-
},
100-
mutations: {
101-
__typename: "ClientMutations",
102-
total: clientData.mutationCount,
103-
},
104-
},
105-
});
106-
107-
return ref ? [...clients, ref] : clients;
108-
},
109-
},
110-
});
111-
};
112-
11365
export const removeClient = (clientId: string) => {
11466
client.cache.modify({
11567
id: "ROOT_QUERY",

src/application/types/gql.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,3 @@ export type SerializedErrorAlertDisclosureItem_error = {
280280
name: string;
281281
stack: string | null;
282282
};
283-
284-
export type ClientFields = {
285-
__typename: "Client";
286-
id: string;
287-
version: string;
288-
queries: { __typename: "ClientQueries"; total: number };
289-
mutations: { __typename: "ClientMutations"; total: number };
290-
};

0 commit comments

Comments
 (0)