-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApolloWrapper.tsx
37 lines (35 loc) · 1.1 KB
/
ApolloWrapper.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
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client'
import { RestLink } from 'apollo-link-rest'
import { transformRestResponse, userReactiveVar } from 'gql/User'
import { PropsWithChildren, useMemo } from 'react'
export const ApolloWrapper = ({ children }: PropsWithChildren) => {
const client = useMemo(() => {
return new ApolloClient({
cache: new InMemoryCache({
typePolicies: {
User: {
fields: {
isAdmin: {
read: (existing, { readField }) => {
const userId = readField('id') as string
return userReactiveVar()[userId]?.isAdmin ?? false
}
}
}
}
}
}),
link: new RestLink({
uri: '/api',
responseTransformer: async (response) => {
const json = await response.json()
if ('users' in json) {
return transformRestResponse(json.users)
}
return json
}
})
})
}, [])
return <ApolloProvider client={client}>{children}</ApolloProvider>
}