-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
apolloClient.js
151 lines (137 loc) · 4.25 KB
/
apolloClient.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// import { ApolloClient } from 'apollo-client'
// import { InMemoryCache } from "apollo-cache-inmemory";
// import { HttpLink } from 'apollo-link-http'
import fetch from "isomorphic-unfetch";
// import { getClientInstance } from "./src/SsrEasyAccess.bs";
let ws = require("isomorphic-ws");
/*
type dataObject = {
.
"__typename": string,
"id": string,
};
let fetch = [%raw "require('isomorphic-unfetch')"];
// createInMemoryCache(~dataIdFromObject=(obj: dataObject) => obj##id, ());
let inMemoryCache = () =>
ApolloInMemoryCache.createInMemoryCache(
// ~dataIdFromObject=
// (obj: dataObject) => {
// obj##id ++ obj##__typename;
// },
~cacheRedirects=[%raw
"{
Query: {
global: (_, args, { getCacheKey }) => {
let result = getCacheKey({ __typename: 'Global', id: args.id })
// console.log(\"the result within\", result)
return result
},
wildcard: (_, args, { getCacheKey }) => {
return getCacheKey({ __typename: 'Wildcard', id: args.id })
},
},
// Subscription: {
// global: (_, args, { getCacheKey }) => {
// let result = getCacheKey({ __typename: 'Global', id: args.id })
// console.log(\"the result within\", result)
// return result
// },
// wildcard: (_, args, { getCacheKey }) => {
// return getCacheKey({ __typename: 'Wildcard', id: args.id })
// },
// },
}"
],
(),
);
let httpLink = networkId =>
ApolloLinks.createHttpLink(
~fetch,
~uri=
switch (networkId) {
| 5 => "https://api.thegraph.com/subgraphs/name/wild-cards/wildcards-goerli"
| _ => "https://api.thegraph.com/subgraphs/name/wild-cards/wildcards"
},
(),
);
let wsLink = networkId =>
ApolloLinks.webSocketLink({
uri:
switch (networkId) {
| 5 => "wss://api.thegraph.com/subgraphs/name/wild-cards/wildcards-goerli"
| _ => "wss://api.thegraph.com/subgraphs/name/wild-cards/wildcards"
},
options: {
reconnect: true,
connectionParams: None,
},
webSocketImpl: ws,
});
let webSocketHttpLink = networkId =>
ApolloLinks.split(
operation => {
let operationDefition =
ApolloUtilities.getMainDefinition(operation.query);
operationDefition.kind == "OperationDefinition"
&& operationDefition.operation == "subscription";
},
wsLink(networkId),
httpLink(networkId),
);
let instance = (~link, ~ssrMode, ~cache) =>
ReasonApollo.createApolloClient(~link, ~cache, ~ssrMode, ());
*/
// const fetch = require("node-fetch");
const ApolloClient = require("apollo-client").default;
// Setup the network "links"
const { WebSocketLink } = require("apollo-link-ws");
const { HttpLink } = require("apollo-link-http");
// const ws = require("ws");
const { split } = require("apollo-link");
const { getMainDefinition } = require("apollo-utilities");
const { InMemoryCache } = require("apollo-cache-inmemory");
// const dotenv = require("dotenv");
// dotenv.config();
const connection_string =
"api.thegraph.com/subgraphs/name/wild-cards/wildcards-goerli";
const httpLink = new HttpLink({
uri: "https://" + connection_string, // use https for secure endpoint
fetch: fetch,
});
// Create a WebSocket link:
const wsLink = new WebSocketLink({
uri: "wss://" + connection_string, // use wss for a secure endpoint
options: {
reconnect: true,
},
webSocketImpl: ws,
});
// using the ability to split links, you can send data to each link
// depending on what kind of operation is being sent
const link = split(
// split based on operation type
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === "OperationDefinition" && operation === "subscription";
},
wsLink,
httpLink
);
// // Instantiate client
// const client = new ApolloClient({
// link,
// cache: new InMemoryCache(),
// });
export default function createApolloClient(initialState, ctx) {
// The `ctx` (NextPageContext) will only be present on the server.
// use it to extract auth headers (ctx.req) or similar.
return new ApolloClient({
ssrMode: Boolean(ctx),
link,
cache: new InMemoryCache().restore(initialState),
});
// return getClientInstance(
// new InMemoryCache().restore(initialState),
// Boolean(ctx)
// );
}