2017-01-22 12:27:06 +00:00
|
|
|
import 'isomorphic-fetch'
|
2017-03-12 07:33:01 +00:00
|
|
|
import React from 'react'
|
|
|
|
import { ApolloProvider, getDataFromTree } from 'react-apollo'
|
2017-01-22 12:27:06 +00:00
|
|
|
import { initClient } from './initClient'
|
|
|
|
|
|
|
|
export default (Component) => (
|
|
|
|
class extends React.Component {
|
|
|
|
static async getInitialProps (ctx) {
|
|
|
|
const headers = ctx.req ? ctx.req.headers : {}
|
|
|
|
const client = initClient(headers)
|
|
|
|
|
2017-01-26 01:10:21 +00:00
|
|
|
const props = {
|
|
|
|
url: { query: ctx.query, pathname: ctx.pathname },
|
|
|
|
...await (Component.getInitialProps ? Component.getInitialProps(ctx) : {})
|
|
|
|
}
|
|
|
|
|
2017-01-22 12:27:06 +00:00
|
|
|
if (!process.browser) {
|
|
|
|
const app = (
|
2017-03-30 18:21:13 +00:00
|
|
|
<ApolloProvider client={client}>
|
2017-01-26 01:10:21 +00:00
|
|
|
<Component {...props} />
|
2017-01-22 12:27:06 +00:00
|
|
|
</ApolloProvider>
|
|
|
|
)
|
|
|
|
await getDataFromTree(app)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
initialState: {
|
2017-03-14 22:41:55 +00:00
|
|
|
apollo: {
|
2017-02-07 11:02:28 +00:00
|
|
|
data: client.getInitialState().data
|
2017-01-22 12:27:06 +00:00
|
|
|
}
|
|
|
|
},
|
2017-01-26 01:10:21 +00:00
|
|
|
headers,
|
|
|
|
...props
|
2017-01-22 12:27:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
2017-03-30 18:21:13 +00:00
|
|
|
this.client = initClient(this.props.headers, this.props.initialState)
|
2017-01-22 12:27:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
2017-03-30 18:21:13 +00:00
|
|
|
<ApolloProvider client={this.client}>
|
2017-01-26 01:10:21 +00:00
|
|
|
<Component {...this.props} />
|
2017-01-22 12:27:06 +00:00
|
|
|
</ApolloProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|