Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 3f66939

Browse files
committed
Apollo + store
- renames `__APOLLO_STATE__` -> `__APOLLO__`, for Apollo GraphQL rehydration on the client - passes per-request `store` to `createClient()`, for use-cases where store data is needed inside GraphQL requests (JWTs, etc)
1 parent a6abe69 commit 3f66939

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

src/entry/client.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ import { rehydrate, autosave } from "@/lib/store";
3737

3838
// ----------------------------------------------------------------------------
3939

40-
// Create Apollo client
41-
const client = createClient();
42-
4340
// Create new MobX state
4441
const store = ((window as any).store = new Store());
4542

43+
// Create Apollo client
44+
const client = createClient(store);
45+
4646
// Create a browser history
4747
const history = createBrowserHistory();
4848

src/entry/server.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ export interface IRouterContext {
6363
export default function(output: Output) {
6464
// Create Koa middleware to handle React requests
6565
return async (ctx: Context) => {
66-
// Create a new Apollo client
67-
const client = createClient();
68-
69-
// Create new MobX state
66+
// Create new MobX store
7067
const store = new Store();
7168

69+
// Create a new Apollo client
70+
const client = createClient(store);
71+
7272
// Create a fresh 'context' for React Router
7373
const routerContext: IRouterContext = {};
7474

@@ -123,8 +123,8 @@ export default function(output: Output) {
123123
html={html}
124124
scripts={output.client.scripts()}
125125
window={{
126-
__APOLLO_STATE__: client.extract(), // <-- GraphQL store
127-
__STORE__: toJS(store), // <-- MobX state
126+
__APOLLO__: client.extract(), // <-- GraphQL store
127+
__STORE__: toJS(store), // <-- MobX store
128128
}}
129129
/>,
130130
);

src/lib/apollo.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ import { WebSocketLink } from "apollo-link-ws";
1313
import { getMainDefinition } from "apollo-utilities";
1414
import { 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(/^https?/, "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

Comments
 (0)