forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
apolloClient.ts
174 lines (145 loc) · 4.65 KB
/
apolloClient.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { useEffect, useState } from 'react'
import { GetServerSidePropsResult, GetStaticPropsResult } from 'next'
import {
ApolloCache,
ApolloClient,
HttpLink,
InMemoryCache,
NormalizedCacheObject,
} from '@apollo/client'
import merge from 'deepmerge'
import isDeepEqual from 'fast-deep-equal/react'
import { useEffectOnce, usePrevious } from 'react-use'
import { CachePersistor, LocalStorageWrapper } from 'apollo3-cache-persist'
export const PERSISTOR_CACHE_KEY =
'with-apollo-and-cache-persist-typescript__apollo-persisted-cache'
const APOLLO_STATE_PROP_NAME = '__APOLLO_STATE__'
const isServer = (): boolean => typeof window === 'undefined'
let apolloClient: ApolloClient<NormalizedCacheObject>
function createCache() {
return new InMemoryCache({
typePolicies: {
Info: {
keyFields: ['name'],
},
},
})
}
function createApolloClient(cache?: ApolloCache<NormalizedCacheObject>) {
return new ApolloClient({
ssrMode: isServer(),
link: new HttpLink({
uri: 'https://api.spacex.land/graphql', // Your API URL here
credentials: 'same-origin',
}),
cache: cache || createCache(),
})
}
function mergeCache(
cache1: NormalizedCacheObject,
cache2: NormalizedCacheObject
) {
return merge(cache1, cache2, {
// Combine arrays using object equality (like in sets)
arrayMerge: (destinationArray, sourceArray) => [
...sourceArray,
...destinationArray.filter((d) =>
sourceArray.every((s) => !isDeepEqual(d, s))
),
],
})
}
export function initializeApollo(
cache?: ApolloCache<NormalizedCacheObject>
): ApolloClient<NormalizedCacheObject> {
const _apolloClient = apolloClient ?? createApolloClient(cache)
// For SSG and SSR always create a new Apollo Client
if (isServer()) return _apolloClient
// Create the Apollo Client once in the client
if (!apolloClient) apolloClient = _apolloClient
return _apolloClient
}
export function addApolloState<
P extends
| GetServerSidePropsResult<Record<string, unknown>>
| GetStaticPropsResult<Record<string, unknown>>
>(
client: ApolloClient<NormalizedCacheObject>,
pageProps: P,
existingCache?: NormalizedCacheObject
): P {
if (pageProps && 'props' in pageProps) {
const props = pageProps.props
if (existingCache) {
props[APOLLO_STATE_PROP_NAME] = mergeCache(
client.cache.extract(),
existingCache
)
} else {
props[APOLLO_STATE_PROP_NAME] = client.cache.extract()
}
}
return pageProps
}
function mergeAndRestoreCache(
client: ApolloClient<NormalizedCacheObject>,
state: NormalizedCacheObject | undefined
) {
if (!state) return
// Get existing cache, loaded during client side data fetching
const existingCache = client.extract()
// Merge the existing cache into data passed from getStaticProps/getServerSideProps
const data = mergeCache(state, existingCache)
// Restore the cache with the merged data
client.cache.restore(data)
}
export function useApollo(pageProps: Record<string, unknown>): {
client: ApolloClient<NormalizedCacheObject> | undefined
cachePersistor: CachePersistor<NormalizedCacheObject> | undefined
} {
const state = pageProps[APOLLO_STATE_PROP_NAME] as
| NormalizedCacheObject
| undefined
const previousState = usePrevious(state)
const [client, setClient] = useState<ApolloClient<NormalizedCacheObject>>()
const [cachePersistor, setCachePersistor] =
useState<CachePersistor<NormalizedCacheObject>>()
useEffectOnce(() => {
async function init() {
const cache = createCache()
const cachePersistor = new CachePersistor({
cache,
storage: new LocalStorageWrapper(window.localStorage),
debug: process.env.NODE_ENV === 'development',
key: PERSISTOR_CACHE_KEY,
})
// Restore client side persisted data before letting the application to
// run any queries
await cachePersistor.restore()
const client = initializeApollo(cache)
mergeAndRestoreCache(client, state)
// Trigger persist to persist data from SSR
cachePersistor.persist()
setCachePersistor(cachePersistor)
setClient(client)
}
init()
})
useEffect(() => {
// If your page has Next.js data fetching methods that use Apollo Client, the initial state
// gets hydrated here during page transitions
if (
client &&
state &&
previousState &&
!isDeepEqual(state, previousState)
) {
mergeAndRestoreCache(client, state)
if (cachePersistor) {
// Trigger persist to persist data from SSR
cachePersistor.persist()
}
}
}, [state, previousState, client, cachePersistor])
return { client, cachePersistor }
}