1
+ "use strict" ;
2
+
3
+ var _interopRequireDefault = require ( "@babel/runtime/helpers/interopRequireDefault" ) ;
4
+
5
+ Object . defineProperty ( exports , "__esModule" , {
6
+ value : true
7
+ } ) ;
8
+ exports . createApolloClient = createApolloClient ;
9
+ exports . restartWebsockets = restartWebsockets ;
10
+
11
+ var _typeof2 = _interopRequireDefault ( require ( "@babel/runtime/helpers/typeof" ) ) ;
12
+
13
+ var _regenerator = _interopRequireDefault ( require ( "@babel/runtime/regenerator" ) ) ;
14
+
15
+ var _asyncToGenerator2 = _interopRequireDefault ( require ( "@babel/runtime/helpers/asyncToGenerator" ) ) ;
16
+
17
+ var _defineProperty2 = _interopRequireDefault ( require ( "@babel/runtime/helpers/defineProperty" ) ) ;
18
+
19
+ var _apolloClient = require ( "apollo-client" ) ;
20
+
21
+ var _apolloLink = require ( "apollo-link" ) ;
22
+
23
+ var _apolloUploadClient = require ( "apollo-upload-client" ) ;
24
+
25
+ var _apolloCacheInmemory = require ( "apollo-cache-inmemory" ) ;
26
+
27
+ var _subscriptionsTransportWs = require ( "subscriptions-transport-ws" ) ;
28
+
29
+ var _messageTypes = _interopRequireDefault ( require ( "subscriptions-transport-ws/dist/message-types" ) ) ;
30
+
31
+ var _apolloLinkWs = require ( "apollo-link-ws" ) ;
32
+
33
+ var _apolloUtilities = require ( "apollo-utilities" ) ;
34
+
35
+ var _apolloLinkPersistedQueries = require ( "apollo-link-persisted-queries" ) ;
36
+
37
+ var _apolloLinkContext = require ( "apollo-link-context" ) ;
38
+
39
+ var _apolloLinkState = require ( "apollo-link-state" ) ;
40
+
41
+ function ownKeys ( object , enumerableOnly ) { var keys = Object . keys ( object ) ; if ( Object . getOwnPropertySymbols ) { var symbols = Object . getOwnPropertySymbols ( object ) ; if ( enumerableOnly ) symbols = symbols . filter ( function ( sym ) { return Object . getOwnPropertyDescriptor ( object , sym ) . enumerable ; } ) ; keys . push . apply ( keys , symbols ) ; } return keys ; }
42
+
43
+ function _objectSpread ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] != null ? arguments [ i ] : { } ; if ( i % 2 ) { ownKeys ( Object ( source ) , true ) . forEach ( function ( key ) { ( 0 , _defineProperty2 [ "default" ] ) ( target , key , source [ key ] ) ; } ) ; } else if ( Object . getOwnPropertyDescriptors ) { Object . defineProperties ( target , Object . getOwnPropertyDescriptors ( source ) ) ; } else { ownKeys ( Object ( source ) ) . forEach ( function ( key ) { Object . defineProperty ( target , key , Object . getOwnPropertyDescriptor ( source , key ) ) ; } ) ; } } return target ; }
44
+
45
+ // Create the apollo client
46
+ function createApolloClient ( _ref ) {
47
+ var _ref$clientId = _ref . clientId ,
48
+ clientId = _ref$clientId === void 0 ? 'defaultClient' : _ref$clientId ,
49
+ httpEndpoint = _ref . httpEndpoint ,
50
+ _ref$wsEndpoint = _ref . wsEndpoint ,
51
+ wsEndpoint = _ref$wsEndpoint === void 0 ? null : _ref$wsEndpoint ,
52
+ _ref$tokenName = _ref . tokenName ,
53
+ tokenName = _ref$tokenName === void 0 ? 'apollo-token' : _ref$tokenName ,
54
+ _ref$persisting = _ref . persisting ,
55
+ persisting = _ref$persisting === void 0 ? false : _ref$persisting ,
56
+ _ref$ssr = _ref . ssr ,
57
+ ssr = _ref$ssr === void 0 ? false : _ref$ssr ,
58
+ _ref$websocketsOnly = _ref . websocketsOnly ,
59
+ websocketsOnly = _ref$websocketsOnly === void 0 ? false : _ref$websocketsOnly ,
60
+ _ref$link = _ref . link ,
61
+ link = _ref$link === void 0 ? null : _ref$link ,
62
+ _ref$preAuthLinks = _ref . preAuthLinks ,
63
+ preAuthLinks = _ref$preAuthLinks === void 0 ? [ ] : _ref$preAuthLinks ,
64
+ _ref$defaultHttpLink = _ref . defaultHttpLink ,
65
+ defaultHttpLink = _ref$defaultHttpLink === void 0 ? true : _ref$defaultHttpLink ,
66
+ _ref$httpLinkOptions = _ref . httpLinkOptions ,
67
+ httpLinkOptions = _ref$httpLinkOptions === void 0 ? { } : _ref$httpLinkOptions ,
68
+ _ref$cache = _ref . cache ,
69
+ cache = _ref$cache === void 0 ? null : _ref$cache ,
70
+ _ref$inMemoryCacheOpt = _ref . inMemoryCacheOptions ,
71
+ inMemoryCacheOptions = _ref$inMemoryCacheOpt === void 0 ? { } : _ref$inMemoryCacheOpt ,
72
+ _ref$apollo = _ref . apollo ,
73
+ apollo = _ref$apollo === void 0 ? { } : _ref$apollo ,
74
+ _ref$clientState = _ref . clientState ,
75
+ clientState = _ref$clientState === void 0 ? null : _ref$clientState ,
76
+ _ref$getAuth = _ref . getAuth ,
77
+ getAuth = _ref$getAuth === void 0 ? defaultGetAuth : _ref$getAuth ,
78
+ _ref$typeDefs = _ref . typeDefs ,
79
+ typeDefs = _ref$typeDefs === void 0 ? undefined : _ref$typeDefs ,
80
+ _ref$resolvers = _ref . resolvers ,
81
+ resolvers = _ref$resolvers === void 0 ? undefined : _ref$resolvers ,
82
+ _ref$onCacheInit = _ref . onCacheInit ,
83
+ onCacheInit = _ref$onCacheInit === void 0 ? undefined : _ref$onCacheInit ;
84
+ var wsClient , authLink , stateLink ;
85
+ var disableHttp = websocketsOnly && ! ssr && wsEndpoint ; // Apollo cache
86
+
87
+ if ( ! cache ) {
88
+ cache = new _apolloCacheInmemory . InMemoryCache ( inMemoryCacheOptions ) ;
89
+ }
90
+
91
+ if ( ! disableHttp ) {
92
+ var httpLink = ( 0 , _apolloUploadClient . createUploadLink ) ( _objectSpread ( {
93
+ uri : httpEndpoint
94
+ } , httpLinkOptions ) ) ;
95
+
96
+ if ( ! link ) {
97
+ link = httpLink ;
98
+ } else if ( defaultHttpLink ) {
99
+ link = ( 0 , _apolloLink . from ) ( [ link , httpLink ] ) ;
100
+ } // HTTP Auth header injection
101
+
102
+
103
+ authLink = ( 0 , _apolloLinkContext . setContext ) ( /*#__PURE__*/ function ( ) {
104
+ var _ref3 = ( 0 , _asyncToGenerator2 [ "default" ] ) ( /*#__PURE__*/ _regenerator [ "default" ] . mark ( function _callee ( _ , _ref2 ) {
105
+ var headers , Authorization , authorizationHeader ;
106
+ return _regenerator [ "default" ] . wrap ( function _callee$ ( _context ) {
107
+ while ( 1 ) {
108
+ switch ( _context . prev = _context . next ) {
109
+ case 0 :
110
+ headers = _ref2 . headers ;
111
+ _context . next = 3 ;
112
+ return getAuth ( tokenName ) ;
113
+
114
+ case 3 :
115
+ Authorization = _context . sent ;
116
+ authorizationHeader = Authorization ? {
117
+ Authorization : Authorization
118
+ } : { } ;
119
+ return _context . abrupt ( "return" , {
120
+ headers : _objectSpread ( _objectSpread ( { } , headers ) , authorizationHeader )
121
+ } ) ;
122
+
123
+ case 6 :
124
+ case "end" :
125
+ return _context . stop ( ) ;
126
+ }
127
+ }
128
+ } , _callee ) ;
129
+ } ) ) ;
130
+
131
+ return function ( _x , _x2 ) {
132
+ return _ref3 . apply ( this , arguments ) ;
133
+ } ;
134
+ } ( ) ) ; // Concat all the http link parts
135
+
136
+ link = authLink . concat ( link ) ;
137
+
138
+ if ( preAuthLinks . length ) {
139
+ link = ( 0 , _apolloLink . from ) ( preAuthLinks ) . concat ( authLink ) ;
140
+ }
141
+ } // On the server, we don't want WebSockets and Upload links
142
+
143
+
144
+ if ( ! ssr ) {
145
+ // If on the client, recover the injected state
146
+ if ( typeof window !== 'undefined' ) {
147
+ // eslint-disable-next-line no-underscore-dangle
148
+ var state = window . __APOLLO_STATE__ ;
149
+
150
+ if ( state && state [ clientId ] ) {
151
+ // Restore state
152
+ cache . restore ( state [ clientId ] ) ;
153
+ }
154
+ }
155
+
156
+ if ( ! disableHttp ) {
157
+ var persistingOpts = { } ;
158
+
159
+ if ( ( 0 , _typeof2 [ "default" ] ) ( persisting ) === 'object' && persisting != null ) {
160
+ persistingOpts = persisting ;
161
+ persisting = true ;
162
+ }
163
+
164
+ if ( persisting === true ) {
165
+ link = ( 0 , _apolloLinkPersistedQueries . createPersistedQueryLink ) ( persistingOpts ) . concat ( link ) ;
166
+ }
167
+ } // Web socket
168
+
169
+
170
+ if ( wsEndpoint ) {
171
+ wsClient = new _subscriptionsTransportWs . SubscriptionClient ( wsEndpoint , {
172
+ reconnect : true ,
173
+ connectionParams : function connectionParams ( ) {
174
+ var Authorization = getAuth ( tokenName ) ;
175
+ return Authorization ? {
176
+ Authorization : Authorization ,
177
+ headers : {
178
+ Authorization : Authorization
179
+ }
180
+ } : { } ;
181
+ }
182
+ } ) ; // Create the subscription websocket link
183
+
184
+ var wsLink = new _apolloLinkWs . WebSocketLink ( wsClient ) ;
185
+
186
+ if ( disableHttp ) {
187
+ link = link ? link . concat ( wsLink ) : wsLink ;
188
+ } else {
189
+ link = ( 0 , _apolloLink . split ) ( // split based on operation type
190
+ function ( _ref4 ) {
191
+ var query = _ref4 . query ;
192
+
193
+ var _getMainDefinition = ( 0 , _apolloUtilities . getMainDefinition ) ( query ) ,
194
+ kind = _getMainDefinition . kind ,
195
+ operation = _getMainDefinition . operation ;
196
+
197
+ return kind === 'OperationDefinition' && operation === 'subscription' ;
198
+ } , wsLink , link ) ;
199
+ }
200
+ }
201
+ }
202
+
203
+ if ( clientState ) {
204
+ console . warn ( 'clientState is deprecated, see https://vue-cli-plugin-apollo.netlify.com/guide/client-state.html' ) ;
205
+ stateLink = ( 0 , _apolloLinkState . withClientState ) ( _objectSpread ( {
206
+ cache : cache
207
+ } , clientState ) ) ;
208
+ link = ( 0 , _apolloLink . from ) ( [ stateLink , link ] ) ;
209
+ }
210
+
211
+ var apolloClient = new _apolloClient . ApolloClient ( _objectSpread ( _objectSpread ( {
212
+ link : link ,
213
+ cache : cache
214
+ } , ssr ? {
215
+ // Set this on the server to optimize queries when SSR
216
+ ssrMode : true
217
+ } : {
218
+ // This will temporary disable query force-fetching
219
+ ssrForceFetchDelay : 100 ,
220
+ // Apollo devtools
221
+ connectToDevTools : process . env . NODE_ENV !== 'production'
222
+ } ) , { } , {
223
+ typeDefs : typeDefs ,
224
+ resolvers : resolvers
225
+ } , apollo ) ) ; // Re-write the client state defaults on cache reset
226
+
227
+ if ( stateLink ) {
228
+ apolloClient . onResetStore ( stateLink . writeDefaults ) ;
229
+ }
230
+
231
+ if ( onCacheInit ) {
232
+ onCacheInit ( cache ) ;
233
+ apolloClient . onResetStore ( function ( ) {
234
+ return onCacheInit ( cache ) ;
235
+ } ) ;
236
+ }
237
+
238
+ return {
239
+ apolloClient : apolloClient ,
240
+ wsClient : wsClient ,
241
+ stateLink : stateLink
242
+ } ;
243
+ }
244
+
245
+ function restartWebsockets ( wsClient ) {
246
+ // Copy current operations
247
+ var operations = Object . assign ( { } , wsClient . operations ) ; // Close connection
248
+
249
+ wsClient . close ( true ) ; // Open a new one
250
+
251
+ wsClient . connect ( ) ; // Push all current operations to the new connection
252
+
253
+ Object . keys ( operations ) . forEach ( function ( id ) {
254
+ wsClient . sendMessage ( id , _messageTypes [ "default" ] . GQL_START , operations [ id ] . options ) ;
255
+ } ) ;
256
+ }
257
+
258
+ function defaultGetAuth ( tokenName ) {
259
+ if ( typeof window !== 'undefined' ) {
260
+ // get the authentication token from local storage if it exists
261
+ var token = window . localStorage . getItem ( tokenName ) ; // return the headers to the context so httpLink can read them
262
+
263
+ return token ? "Bearer " . concat ( token ) : '' ;
264
+ }
265
+ }
0 commit comments