-
Notifications
You must be signed in to change notification settings - Fork 117
/
index.tsx
executable file
·144 lines (129 loc) · 3.63 KB
/
index.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
import React, { useCallback, useEffect, useState } from 'react';
import { render } from 'react-dom';
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 './index.module.css';
const launchesGQL = gql`
query LaunchesQuery {
launches(limit: 10) {
id
mission_name
details
launch_date_utc
}
}
`;
type LaunchesQuery = {
launches: {
id: string;
mission_name: string;
details: string;
launch_date_utc: string;
}[];
};
const Launches = () => {
const { error, data, loading } = useQuery<LaunchesQuery>(launchesGQL, {
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.launches.map(launch => (
<div key={launch.id} className={styles.item}>
<span>{launch.mission_name}</span>
<br />
<small>{new Date(launch.launch_date_utc).toLocaleString()}</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://api.spacex.land/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>
);
};
render(<App />, document.getElementById('root'));