-
Notifications
You must be signed in to change notification settings - Fork 118
/
App.tsx
148 lines (132 loc) · 3.57 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {useCallback, useEffect, useState} from 'react'
import {
ApolloClient,
ApolloProvider,
NormalizedCacheObject,
useQuery,
} from '@apollo/client';
import gql from 'graphql-tag';
import { InMemoryCache } from '@apollo/client/core';
import { CachePersistor, LocalStorageWrapper } from 'apollo3-cache-persist';
import styles from './App.module.css';
const episodesGQL = gql`
query episodes {
episodes {
results {
episode
id
name
air_date
}
}
}
`;
type EpisodesQuery = {
episodes: {
results: {
episode: string;
id: string;
name: string;
air_date: string;
}[];
}
};
const Launches = () => {
const { error, data, loading } = useQuery<EpisodesQuery>(episodesGQL, {
fetchPolicy: 'cache-and-network',
});
if (!data) {
// we don't have data yet
if (loading) {
// but we're loading some
return <h2>Loading initial data...</h2>;
}
if (error) {
// and we have an error
return <h2>Error loading data :(</h2>;
}
return <h2>Unknown error :(</h2>;
}
return (
<div>
{loading ? <h2>Loading fresh data...</h2> : null}
{data.episodes.results.map(episode => (
<div key={episode.id} className={styles.item}>
<span>{episode.episode}: {episode.name}</span>
<br />
<small>{episode.air_date}</small>
</div>
))}
</div>
);
};
const App = () => {
const [client, setClient] = useState<ApolloClient<NormalizedCacheObject>>();
const [persistor, setPersistor] = useState<
CachePersistor<NormalizedCacheObject>
>();
useEffect(() => {
async function init() {
const cache = new InMemoryCache();
let newPersistor = new CachePersistor({
cache,
storage: new LocalStorageWrapper(window.localStorage),
debug: true,
trigger: 'write',
});
await newPersistor.restore();
setPersistor(newPersistor);
setClient(
new ApolloClient({
uri: 'https://rickandmortyapi.com/graphql',
cache,
}),
);
}
init().catch(console.error);
}, []);
const clearCache = useCallback(() => {
if (!persistor) {
return;
}
persistor.purge();
}, [persistor]);
const reload = useCallback(() => {
window.location.reload();
}, []);
if (!client) {
return <h2>Initializing app...</h2>;
}
return (
<ApolloProvider client={client}>
<div className={styles.container}>
<div className={styles.content}>
<Launches />
</div>
<div className={styles.controls}>
<h3>Example controls</h3>
<p>Use the following buttons to control apollo3-cache-persist.</p>
<p>
Once you've loaded the initial data, you should see "Loading fresh
data" followed by the list of cached Launches every time you reload
the page.
</p>
<p>
Clear cache should remove everything from the localstorage, so that
when you reload the page, you should see "Loading initial data..."
for a moment.
</p>
<p>
Debugging output is enabled,{' '}
<strong>make sure to open Developer console</strong> to see what's
going on. You can see the cached data under{' '}
<i>Application → Local Storage</i>.
</p>
<button onClick={clearCache}>Clear cache</button>
<button onClick={reload}>Reload page</button>
</div>
</div>
</ApolloProvider>
);
};
export default App