-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathNetworkLayer.js
38 lines (32 loc) · 928 Bytes
/
NetworkLayer.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
/* eslint-disable no-console */
import Relay from 'react-relay/classic';
import { SubscriptionClient } from 'subscriptions-transport-ws';
export default class NetworkLayer extends Relay.DefaultNetworkLayer {
constructor(...args) {
super(...args);
this._subscriptionClient = new SubscriptionClient(
`ws://${global.location.host}/graphql`,
{ reconnect: true },
);
}
sendSubscription(request) {
const { unsubscribe } = this._subscriptionClient.request({
query: request.getQueryString(),
variables: request.getVariables(),
}).subscribe({
next: ({ errors, data }) => {
if (errors) {
request.onError(errors);
} else {
request.onNext(data);
}
},
error: request.onError,
complete: request.onCompleted,
});
return { dispose: unsubscribe };
}
disconnect() {
this._subscriptionClient.close();
}
}