@@ -13,9 +13,15 @@ import { WebSocketLink } from "apollo-link-ws";
13
13
import { getMainDefinition } from "apollo-utilities" ;
14
14
import { SubscriptionClient } from "subscriptions-transport-ws" ;
15
15
16
+ /* Local */
17
+ import { Store } from "@/data/store" ;
18
+
16
19
// ----------------------------------------------------------------------------
17
20
18
- export function createClient ( ) : ApolloClient < NormalizedCacheObject > {
21
+ export function createClient (
22
+ // @ts -ignore - useful to pass in the store for `Authorization` headers, etc
23
+ store : Store ,
24
+ ) : ApolloClient < NormalizedCacheObject > {
19
25
// Create the cache first, which we'll share across Apollo tooling.
20
26
// This is an in-memory cache. Since we'll be calling `createClient` on
21
27
// universally, the cache will survive until the HTTP request is
@@ -28,13 +34,13 @@ export function createClient(): ApolloClient<NormalizedCacheObject> {
28
34
// set to an external playground at https://graphqlhub.com/graphql
29
35
const httpLink = new HttpLink ( {
30
36
credentials : "same-origin" ,
31
- uri : GRAPHQL
37
+ uri : GRAPHQL ,
32
38
} ) ;
33
39
34
40
// If we're in the browser, we'd have received initial state from the
35
41
// server. Restore it, so the client app can continue with the same data.
36
42
if ( ! SERVER ) {
37
- cache . restore ( ( window as any ) . __APOLLO_STATE__ ) ;
43
+ cache . restore ( ( window as any ) . __APOLLO__ ) ;
38
44
}
39
45
40
46
// Return a new Apollo Client back, with the cache we've just created,
@@ -52,8 +58,8 @@ export function createClient(): ApolloClient<NormalizedCacheObject> {
52
58
if ( graphQLErrors ) {
53
59
graphQLErrors . map ( ( { message, locations, path } ) =>
54
60
console . log (
55
- `[GraphQL error]: Message: ${ message } , Location: ${ locations } , Path: ${ path } `
56
- )
61
+ `[GraphQL error]: Message: ${ message } , Location: ${ locations } , Path: ${ path } ` ,
62
+ ) ,
57
63
) ;
58
64
}
59
65
if ( networkError ) {
@@ -75,15 +81,15 @@ export function createClient(): ApolloClient<NormalizedCacheObject> {
75
81
new WebSocketLink (
76
82
// Replace http(s) with `ws` for connecting via WebSockts
77
83
new SubscriptionClient ( GRAPHQL . replace ( / ^ h t t p s ? / , "ws" ) , {
78
- reconnect : true // <-- automatically redirect as needed
79
- } )
84
+ reconnect : true , // <-- automatically redirect as needed
85
+ } ) ,
80
86
) ,
81
87
// ... fall-back to HTTP for everything else
82
- httpLink
88
+ httpLink ,
83
89
)
84
- : httpLink // <-- just use HTTP on the server
90
+ : httpLink , // <-- just use HTTP on the server
85
91
] ) ,
86
92
// On the server, enable SSR mode
87
- ssrMode : SERVER
93
+ ssrMode : SERVER ,
88
94
} ) ;
89
95
}
0 commit comments