@@ -13,9 +13,15 @@ import { WebSocketLink } from "apollo-link-ws";
1313import { getMainDefinition } from "apollo-utilities" ;
1414import { SubscriptionClient } from "subscriptions-transport-ws" ;
1515
16+ /* Local */
17+ import { Store } from "@/data/store" ;
18+
1619// ----------------------------------------------------------------------------
1720
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 > {
1925 // Create the cache first, which we'll share across Apollo tooling.
2026 // This is an in-memory cache. Since we'll be calling `createClient` on
2127 // universally, the cache will survive until the HTTP request is
@@ -28,13 +34,13 @@ export function createClient(): ApolloClient<NormalizedCacheObject> {
2834 // set to an external playground at https://graphqlhub.com/graphql
2935 const httpLink = new HttpLink ( {
3036 credentials : "same-origin" ,
31- uri : GRAPHQL
37+ uri : GRAPHQL ,
3238 } ) ;
3339
3440 // If we're in the browser, we'd have received initial state from the
3541 // server. Restore it, so the client app can continue with the same data.
3642 if ( ! SERVER ) {
37- cache . restore ( ( window as any ) . __APOLLO_STATE__ ) ;
43+ cache . restore ( ( window as any ) . __APOLLO__ ) ;
3844 }
3945
4046 // Return a new Apollo Client back, with the cache we've just created,
@@ -52,8 +58,8 @@ export function createClient(): ApolloClient<NormalizedCacheObject> {
5258 if ( graphQLErrors ) {
5359 graphQLErrors . map ( ( { message, locations, path } ) =>
5460 console . log (
55- `[GraphQL error]: Message: ${ message } , Location: ${ locations } , Path: ${ path } `
56- )
61+ `[GraphQL error]: Message: ${ message } , Location: ${ locations } , Path: ${ path } ` ,
62+ ) ,
5763 ) ;
5864 }
5965 if ( networkError ) {
@@ -75,15 +81,15 @@ export function createClient(): ApolloClient<NormalizedCacheObject> {
7581 new WebSocketLink (
7682 // Replace http(s) with `ws` for connecting via WebSockts
7783 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+ } ) ,
8086 ) ,
8187 // ... fall-back to HTTP for everything else
82- httpLink
88+ httpLink ,
8389 )
84- : httpLink // <-- just use HTTP on the server
90+ : httpLink , // <-- just use HTTP on the server
8591 ] ) ,
8692 // On the server, enable SSR mode
87- ssrMode : SERVER
93+ ssrMode : SERVER ,
8894 } ) ;
8995}
0 commit comments