-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapollo.js
47 lines (40 loc) · 1.11 KB
/
apollo.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
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { WebSocketLink } from 'apollo-link-ws';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import { InMemoryCache } from "apollo-cache-inmemory";
import { split } from 'apollo-link';
import { getMainDefinition } from 'apollo-utilities';
import { GRAPHQL_ENGINE_URL } from './constants';
const GRAPHQL_ENDPOINT = `${GRAPHQL_ENGINE_URL.split('//')[1]}/v1alpha1/graphql`;
const httpLink = new HttpLink({
uri: `http://${GRAPHQL_ENDPOINT}`,
headers: {
'x-hasura-access-key': '12345'
}
});
const wsLink = new WebSocketLink({
uri: `ws://${GRAPHQL_ENDPOINT}`,
options: {
reconnect: true,
connectionParams: {
headers: {
'x-hasura-access-key': '12345'
}
}
}
});
const link = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink,
);
const cache = new InMemoryCache();
const client = new ApolloClient({
link,
cache
});
export default client;