-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApolloWrapper.tsx
74 lines (69 loc) · 2.03 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
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
import {
ApolloClient,
InMemoryCache,
ApolloProvider,
HttpLink,
from,
} from "@apollo/client"
import { onError } from "@apollo/client/link/error"
import { ReactNode } from "react"
import { hasMoreVar, limit } from "./state/endlessScrollState"
//Error handling from the Apollo docs: Advanced Error Handling
//https://www.apollographql.com/docs/react/data/error-handling/
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.forEach(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
)
)
if (networkError) {
console.log(`[Network error]: ${networkError}`)
}
})
const link = from([
errorLink,
new HttpLink({ uri: `http://it2810-41.idi.ntnu.no:4000/project3/graphql` }),
])
//The apollo client
const client = new ApolloClient({
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
gasStations: {
keyArgs: [
"city",
"maxPrice",
"nameSearch",
"sortBy",
"sortDirection",
],
merge(
existing: [],
incoming: [],
{ args: { skip = 0 } }: { args }
) {
// if number of items is less than limit, there are no more items to fetch
if (incoming.length < limit) {
hasMoreVar(false)
}
// Slicing is necessary because the existing data is
// immutable, and frozen in development.
const merged = existing ? existing.slice(0) : []
for (let i = 0; i < incoming.length; ++i) {
merged[skip + i] = incoming[i]
}
return merged
},
},
},
},
},
}),
link: link,
})
function ApolloWrapper({ children }: { children: ReactNode }) {
return <ApolloProvider client={client}>{children}</ApolloProvider>
}
export default ApolloWrapper