Last active
December 10, 2018 02:55
-
-
Save takanorip/4db791390672accb5eb0770a22635334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// react-router v4 を想定したサンプルですが | |
// その他のルーターでも動作します | |
import { ApolloProvider, getDataFromTree } from 'react-apollo'; | |
import { ApolloClient } from 'apollo-client'; | |
import { createHttpLink } from 'apollo-link-http'; | |
import Express from 'express'; | |
import { StaticRouter } from 'react-router'; | |
import { InMemoryCache } from "apollo-cache-inmemory"; | |
import Layout from './routes/Layout'; | |
const app = new Express(); | |
app.use((req, res) => { | |
const client = new ApolloClient({ | |
ssrMode: true, | |
link: createHttpLink({ | |
uri: 'http://localhost:3010', | |
credentials: 'same-origin', | |
headers: { | |
cookie: req.header('Cookie'), | |
}, | |
}), | |
cache: new InMemoryCache(), | |
}); | |
const context = {}; | |
const App = ( | |
<ApolloProvider client={client}> | |
<StaticRouter location={req.url} context={context}> | |
<Layout /> | |
</StaticRouter> | |
</ApolloProvider> | |
); | |
// 後述のレンダリング用コードを書く | |
}); | |
app.listen(basePort, () => console.log( // eslint-disable-line no-console | |
`app Server is now running on http://localhost:${basePort}` | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment