|
1 | 1 | import { ApolloClient } from 'apollo-client'
|
2 |
| -import { split, ApolloLink } from 'apollo-link' |
3 |
| -import { HttpLink } from 'apollo-link-http' |
| 2 | +import { ApolloLink } from 'apollo-link' |
4 | 3 | import { InMemoryCache } from 'apollo-cache-inmemory'
|
5 | 4 | import { SubscriptionClient } from 'subscriptions-transport-ws'
|
6 | 5 | import { WebSocketLink } from 'apollo-link-ws'
|
7 |
| -import { getMainDefinition } from 'apollo-utilities' |
8 | 6 | import { withClientState } from 'apollo-link-state'
|
9 | 7 | import defaults from './state/defaults'
|
10 | 8 | import resolvers from './state/resolvers'
|
11 | 9 |
|
12 | 10 | // Create the apollo client
|
13 | 11 | export default function createApolloClient ({ base, endpoints, persisting }) {
|
14 |
| - let link |
15 |
| - let wsClient |
16 |
| - |
17 |
| - let httpLink = new HttpLink({ |
18 |
| - // You should use an absolute URL here |
19 |
| - uri: base + endpoints.graphql |
20 |
| - }) |
21 |
| - |
22 | 12 | // Apollo cache
|
23 | 13 | const cache = new InMemoryCache()
|
24 | 14 |
|
25 | 15 | // Client-side state
|
26 | 16 | const stateLink = withClientState({ defaults, cache, resolvers })
|
27 | 17 |
|
28 | 18 | // Web socket
|
29 |
| - wsClient = new SubscriptionClient(base.replace(/^https?/i, 'ws') + |
| 19 | + const wsClient = new SubscriptionClient(base.replace(/^https?/i, 'ws') + |
30 | 20 | endpoints.subscription, {
|
31 | 21 | reconnect: true
|
32 | 22 | })
|
33 | 23 |
|
34 | 24 | // Create the subscription websocket link
|
35 | 25 | const wsLink = new WebSocketLink(wsClient)
|
36 | 26 |
|
37 |
| - link = split( |
38 |
| - // split based on operation type |
39 |
| - ({ query }) => { |
40 |
| - const { kind, operation } = getMainDefinition(query) |
41 |
| - return kind === 'OperationDefinition' && |
42 |
| - operation === 'subscription' |
43 |
| - }, |
44 |
| - wsLink, |
45 |
| - httpLink |
46 |
| - ) |
47 |
| - |
48 | 27 | const apolloClient = new ApolloClient({
|
49 |
| - link: ApolloLink.from([stateLink, link]), |
| 28 | + link: ApolloLink.from([stateLink, wsLink]), |
50 | 29 | cache
|
51 | 30 | })
|
52 | 31 |
|
|
0 commit comments