/
apollo.js
46 lines (40 loc) 路 1.06 KB
/
apollo.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
42
43
44
45
46
import { ApolloClient, InMemoryCache, gql } from '@apollo/client'
import find from 'lodash/find'
import link from './link'
export const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
reviews: {
merge(existing = [], incoming, { readField }) {
const notAlreadyInCache = (review) =>
!find(
existing,
(existingReview) =>
readField('id', existingReview) === readField('id', review)
)
const newReviews = incoming.filter(notAlreadyInCache)
return [...existing, ...newReviews]
},
keyArgs: ['orderBy'],
},
section: (_, { args: { id }, toReference }) =>
toReference({
__typename: 'Section',
id,
}),
},
},
Section: {
fields: {
scrollY: (scrollY) => scrollY || 0,
},
},
},
})
const typeDefs = gql`
extend type Section {
scrollY: Int
}
`
export const apollo = new ApolloClient({ link, cache, typeDefs })