-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathapollo-client.ts
60 lines (55 loc) · 1.99 KB
/
apollo-client.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
import {
ApolloClient,
createHttpLink,
InMemoryCache,
makeVar,
} from "@apollo/client"
import { setContext } from "@apollo/client/link/context"
import { relayStylePagination } from "@apollo/client/utilities"
const httpLink = createHttpLink({
uri: process.env.NEXT_PUBLIC_HYGRAPH_URL,
})
const authLink = setContext((_, { headers }) => {
const token = process.env.NEXT_PUBLIC_HYGRAPH_AUTH_TOKEN
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
},
}
})
export const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
worksConnection: relayStylePagination(),
guestBooksConnection: relayStylePagination(),
blogs: {
//first merge func gets called and then read func. fetchMore always sends a network request which then comes back to the cache and gets merged
//with the help of merge function. Then cache tries to read the query to send the required data back to the client. But if a read
// function is present then the moment cache will try to read the query, it will instantly call the read function.
read(existing, { args: { skip, first } }: any) {
return existing && existing.slice(skip, skip + first)
},
keyArgs: false,
merge(existing, incoming, { args: { skip } }: any) {
const merged = existing ? existing.slice(0) : []
for (let i = 0; i < incoming.length; ++i) {
merged[skip + i] = incoming[i]
}
return merged
},
},
},
},
},
}),
})
export const currentWorkTab = makeVar<string>("All")
export const currentMenu = makeVar<number>(1)
export const currentWork = makeVar<null | string>(null)
export const showMenu = makeVar<boolean>(false)
export default client