-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutilites.js
41 lines (35 loc) · 1.33 KB
/
utilites.js
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
import { useMemo } from 'react'
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client'
import { concatPagination } from '@apollo/client/utilities'
let apolloClient
const createApolloClient = () => {
return new ApolloClient({
ssrMode: typeof window === 'undefined',
link: new HttpLink({
uri: 'https://api-eu-central-1.graphcms.com/v2/ckemn1nux138n01z5h4ap5pjl/master', // Server URL (must be absolute)
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
}),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
allPosts: concatPagination(),
},
},
},
}),
})
}
export const initializeApollo = (initialState = null) => {
const _apolloClient = apolloClient ?? createApolloClient()
if (initialState) {
const existingCache = _apolloClient.extract()
_apolloClient.cache.restore({ ...existingCache, ...initialState })
}
if (typeof window === 'undefined') return _apolloClient
if (!apolloClient) apolloClient = _apolloClient
return _apolloClient
}
export const useApollo = (initialState) => {
return useMemo(() => initializeApollo(initialState), [initialState])
}