1
1
import { ApolloClient } from 'apollo-client'
2
2
import { split , ApolloLink } from 'apollo-link'
3
3
import { HttpLink } from 'apollo-link-http'
4
- import { createUploadLink } from 'apollo-upload-client'
5
4
import { InMemoryCache } from 'apollo-cache-inmemory'
6
- import { SubscriptionClient , MessageTypes } from 'subscriptions-transport-ws'
5
+ import { SubscriptionClient } from 'subscriptions-transport-ws'
7
6
import { WebSocketLink } from 'apollo-link-ws'
8
7
import { getMainDefinition } from 'apollo-utilities'
9
- import { createPersistedQueryLink } from 'apollo-link-persisted-queries'
10
- import { setContext } from 'apollo-link-context'
11
8
import { withClientState } from 'apollo-link-state'
12
9
import defaults from './state/defaults'
13
10
import resolvers from './state/resolvers'
14
11
15
- function getAuth ( ) {
16
- // get the authentication token from local storage if it exists
17
- const token = localStorage . getItem ( 'apollo-token' )
18
- // return the headers to the context so httpLink can read them
19
- return token ? `Bearer ${ token } ` : ''
20
- }
21
-
22
- function restartWebsockets ( wsClient ) {
23
- // Copy current operations
24
- const operations = Object . assign ( { } , wsClient . operations )
25
-
26
- // Close connection
27
- wsClient . close ( true )
28
-
29
- // Open a new one
30
- wsClient . connect ( )
31
-
32
- // Push all current operations to the new connection
33
- Object . keys ( operations ) . forEach ( id => {
34
- wsClient . sendMessage (
35
- id ,
36
- MessageTypes . GQL_START ,
37
- operations [ id ] . options
38
- )
39
- } )
40
- }
41
-
42
12
// Create the apollo client
43
- export default function createApolloClient ( { ssr , base, endpoints, persisting } ) {
13
+ export default function createApolloClient ( { base, endpoints, persisting } ) {
44
14
let link
45
15
let wsClient
46
16
@@ -49,102 +19,36 @@ export default function createApolloClient ({ ssr, base, endpoints, persisting }
49
19
uri : base + endpoints . graphql
50
20
} )
51
21
52
- // HTTP Auth header injection
53
- const authLink = setContext ( ( _ , { headers } ) => ( {
54
- headers : {
55
- ...headers ,
56
- authorization : getAuth ( )
57
- }
58
- } ) )
59
-
60
- // Concat all the http link parts
61
- httpLink = authLink . concat ( httpLink )
62
- if ( persisting ) {
63
- httpLink = createPersistedQueryLink ( ) . concat ( httpLink )
64
- }
65
-
66
22
// Apollo cache
67
23
const cache = new InMemoryCache ( )
68
24
69
25
// Client-side state
70
26
const stateLink = withClientState ( { defaults, cache, resolvers } )
71
27
72
- if ( ! ssr ) {
73
- // If on the client, recover the injected state
74
- if ( typeof window !== 'undefined' ) {
75
- // eslint-disable-next-line no-underscore-dangle
76
- const state = window . __APOLLO_STATE__
77
- if ( state ) {
78
- // If you have multiple clients, use `state.<client_id>`
79
- cache . restore ( state . defaultClient )
80
- }
81
- }
82
-
83
- // Web socket
84
- wsClient = new SubscriptionClient ( base . replace ( / ^ h t t p s ? / i, 'ws' + ( process . env . NODE_ENV === 'production' ? 's' : '' ) ) +
85
- endpoints . subscription , {
86
- reconnect : true ,
87
- connectionParams : ( ) => ( {
88
- 'Authorization' : getAuth ( )
89
- } )
90
- } )
91
-
92
- // Create the subscription websocket link
93
- const wsLink = new WebSocketLink ( wsClient )
94
-
95
- // File upload
96
- const uploadLink = authLink . concat ( createUploadLink ( {
97
- uri : base + endpoints . graphql
98
- } ) )
28
+ // Web socket
29
+ wsClient = new SubscriptionClient ( base . replace ( / ^ h t t p s ? / i, 'ws' + ( process . env . NODE_ENV === 'production' ? 's' : '' ) ) +
30
+ endpoints . subscription , {
31
+ reconnect : true
32
+ } )
99
33
100
- // using the ability to split links, you can send data to each link
101
- // depending on what kind of operation is being sent
102
- httpLink = split (
103
- operation => operation . getContext ( ) . upload ,
104
- uploadLink ,
105
- httpLink
106
- )
34
+ // Create the subscription websocket link
35
+ const wsLink = new WebSocketLink ( wsClient )
107
36
108
- link = split (
109
- // split based on operation type
110
- ( { query } ) => {
111
- const { kind, operation } = getMainDefinition ( query )
112
- return kind === 'OperationDefinition' &&
113
- operation === 'subscription'
114
- } ,
115
- wsLink ,
116
- httpLink
117
- )
118
- } else {
119
- // On the server, we don't want WebSockets
120
- link = httpLink
121
- }
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
+ )
122
47
123
48
const apolloClient = new ApolloClient ( {
124
49
link : ApolloLink . from ( [ stateLink , link ] ) ,
125
- cache,
126
- // Additional options
127
- ...( ssr ? {
128
- // Set this on the server to optimize queries when SSR
129
- ssrMode : true
130
- } : {
131
- // This will temporary disable query force-fetching
132
- ssrForceFetchDelay : 100 ,
133
- // Apollo devtools
134
- connectToDevTools : process . env . NODE_ENV !== 'production'
135
- } )
50
+ cache
136
51
} )
137
52
138
- apolloClient . $onLogin = token => {
139
- localStorage . setItem ( 'apollo-token' , token )
140
- if ( wsClient ) restartWebsockets ( wsClient )
141
- }
142
-
143
- apolloClient . $onLogout = ( ) => {
144
- localStorage . removeItem ( 'apollo-token' )
145
- if ( wsClient ) restartWebsockets ( wsClient )
146
- apolloClient . resetStore ( )
147
- }
148
-
149
53
return apolloClient
150
54
}
0 commit comments