-
Notifications
You must be signed in to change notification settings - Fork 3
/
environment.ts
44 lines (37 loc) · 1.37 KB
/
environment.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
37
38
39
40
41
42
43
44
import { Environment, Network, RecordSource, Store } from 'relay-runtime';
import { getAccessToken } from '../helpers/auth';
import RelayNetworkLogger from 'relay-runtime/lib/RelayNetworkLogger'
import { SubscriptionClient } from 'subscriptions-transport-ws'
//@TODO fix this
const GRAPHQL_URL = process.env.GRAPHQL_URL || 'http://localhost:8080/graphql'
const SUBSCRIPTION_URL = process.env.SUBSCRIPTION_URL || 'wss://house-automation-api.herokuapp.com/subscriptions'
const setupSubscription = (config, variables, cacheConfig, observer) => {
const query = config.text
const subscriptionClient = new SubscriptionClient(SUBSCRIPTION_URL, {reconnect: true})
subscriptionClient.subscribe({query, variables}, (error, result) => {
observer.onNext({data: result})
})
}
const fetchQuery = async (operation, variables) => {
const response = await fetch(GRAPHQL_URL, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: getAccessToken(),
},
body: JSON.stringify({
query: operation.text,
variables,
}),
});
return await response.json();
};
const network = Network.create(RelayNetworkLogger.wrapFetch(fetchQuery, () => ''), setupSubscription);
const source = new RecordSource();
const store = new Store(source);
const env = new Environment({
network,
store,
});
export default env;