11import { ApolloClient } from 'apollo-client'
22import { split , ApolloLink } from 'apollo-link'
33import { HttpLink } from 'apollo-link-http'
4- import { createUploadLink } from 'apollo-upload-client'
54import { InMemoryCache } from 'apollo-cache-inmemory'
6- import { SubscriptionClient , MessageTypes } from 'subscriptions-transport-ws'
5+ import { SubscriptionClient } from 'subscriptions-transport-ws'
76import { WebSocketLink } from 'apollo-link-ws'
87import { getMainDefinition } from 'apollo-utilities'
9- import { createPersistedQueryLink } from 'apollo-link-persisted-queries'
10- import { setContext } from 'apollo-link-context'
118import { withClientState } from 'apollo-link-state'
129import defaults from './state/defaults'
1310import resolvers from './state/resolvers'
1411
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-
4212// Create the apollo client
43- export default function createApolloClient ( { ssr , base, endpoints, persisting } ) {
13+ export default function createApolloClient ( { base, endpoints, persisting } ) {
4414 let link
4515 let wsClient
4616
@@ -49,102 +19,36 @@ export default function createApolloClient ({ ssr, base, endpoints, persisting }
4919 uri : base + endpoints . graphql
5020 } )
5121
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-
6622 // Apollo cache
6723 const cache = new InMemoryCache ( )
6824
6925 // Client-side state
7026 const stateLink = withClientState ( { defaults, cache, resolvers } )
7127
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+ } )
9933
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 )
10736
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+ )
12247
12348 const apolloClient = new ApolloClient ( {
12449 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
13651 } )
13752
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-
14953 return apolloClient
15054}
0 commit comments