-
Notifications
You must be signed in to change notification settings - Fork 3
/
subscription-exchange.ts
36 lines (33 loc) · 1.29 KB
/
subscription-exchange.ts
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
import { subscriptionExchange } from "https://npm.tfl.dev/@urql/core@^3.0.0";
import { createClient as createWSClient } from "https://npm.tfl.dev/graphql-ws@5";
import config from "https://tfl.dev/@truffle/config@^1.0.0/index.ts";
import isSsr from "https://tfl.dev/@truffle/utils@~0.0.22/ssr/is-ssr.ts";
const wsClient = isSsr ? null : createWSClient({
// FIXME: .replace is hacky
url: `${config.API_URL.replace("http", "ws")}/graphql`,
// TODO: pass in websocket lib on node for ssr
// need to figure out smart way to import only for node - ideally normal import, not dynamic
// basically want to retry until we're connected
retryAttempts: 99999,
retryWait: async (retries) => {
// 0.5s, 1s, 1.5, 2s, ..., 5s (repeated)
const delayMs = Math.min((retries + 1) * 400, 5000);
await new Promise((resolve) => setTimeout(resolve, delayMs));
},
// always retry. otherwise it doesn't seem to retry if server is down
shouldRetry: (/* errOrCloseEvent */) => true,
});
export function getSubscriptionExchange() {
return subscriptionExchange({
forwardSubscription(operation) {
return {
subscribe: (sink) => {
const dispose = wsClient!.subscribe(operation, sink);
return {
unsubscribe: dispose,
};
},
};
},
});
}