forked from leebenson/reactql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.tsx
63 lines (45 loc) · 1.62 KB
/
client.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Client entry point
// ----------------------------------------------------------------------------
// IMPORTS
/* NPM */
// Create browser history, for navigation a la single page apps
import createBrowserHistory from "history/createBrowserHistory";
// React, our UI engine
import * as React from "react";
// HOC for enabling Apollo GraphQL `<Query>` and `<Mutation>`
import { ApolloProvider } from "react-apollo";
// Attach React to the browser DOM
import * as ReactDOM from "react-dom";
// Single page app routing
import { Router } from "react-router-dom";
// MobX provider
import { Provider } from "mobx-react";
/* Local */
// Our main component, and the starting point for server/browser loading
import Root from "@/components/root";
// Helper function that creates a new Apollo client per request
import { createClient } from "@/lib/apollo";
// MobX state
import { Store } from "@/data/store";
import { rehydrate, autosave } from "@/lib/store";
// ----------------------------------------------------------------------------
// Create new MobX state
const store = ((window as any).store = new Store());
// Create Apollo client
const client = createClient(store);
// Create a browser history
const history = createBrowserHistory();
// Render
const root = document.getElementById("root")!;
ReactDOM[root.innerHTML ? "hydrate" : "render"](
<Provider store={store}>
<ApolloProvider client={client}>
<Router history={history}>
<Root />
</Router>
</ApolloProvider>
</Provider>,
document.getElementById("root")
);
// Rehydrate MobX store and save changes
[rehydrate, autosave].forEach(fn => fn(store));