-
Notifications
You must be signed in to change notification settings - Fork 18
/
Apollo.res
61 lines (52 loc) · 1.45 KB
/
Apollo.res
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
let graphqlEndpoint = "localhost:4000/graphql"
let headers = {"high": "five"}
let httpLink = ApolloClient.Link.HttpLink.make(
~uri=_ => "http://" ++ graphqlEndpoint,
~headers=Obj.magic(headers),
(),
)
let _retryLink = ApolloClient.Link.RetryLink.make(
~attempts=RetryFunction(async (~count, ~operation as _, ~error as _) => count < 3),
~delay=DelayFunction((~count as _, ~operation as _, ~error as _) => 1_000),
(),
)
let wsLink = {
open ApolloClient.Link.WebSocketLink
make(
~uri="ws://" ++ graphqlEndpoint,
~options=ClientOptions.make(
~connectionParams=ConnectionParams(Obj.magic({"headers": headers})),
~reconnect=true,
(),
),
(),
)
}
let terminatingLink = ApolloClient.Link.split(~test=({query}) => {
let definition = ApolloClient.Utilities.getOperationDefinition(query)
switch definition {
| Some({kind, operation}) => kind === "OperationDefinition" && operation === "subscription"
| None => false
}
}, ~whenTrue=wsLink, ~whenFalse=httpLink)
let client = {
open ApolloClient
make(
~cache=Cache.InMemoryCache.make(),
~connectToDevTools=true,
~defaultOptions=DefaultOptions.make(
~mutate=DefaultMutateOptions.make(~awaitRefetchQueries=true, ()),
(),
),
~link=terminatingLink,
(),
)
}
client.onClearStore(~cb=async () => {
Js.log("store cleared")
})
client.onResetStore(~cb=async () => {
Js.log("store reset")
})
let _ = client.clearStore()
let _ = client.resetStore()