2017-03-30 18:21:13 +00:00
|
|
|
import React from 'react'
|
2017-05-10 22:23:11 +00:00
|
|
|
import PropTypes from 'prop-types'
|
2017-03-30 18:21:13 +00:00
|
|
|
import { ApolloProvider, getDataFromTree } from 'react-apollo'
|
2017-06-28 17:07:21 +00:00
|
|
|
import Head from 'next/head'
|
2017-05-10 22:23:11 +00:00
|
|
|
import initApollo from './initApollo'
|
|
|
|
import initRedux from './initRedux'
|
|
|
|
|
2017-07-01 21:51:20 +00:00
|
|
|
// Gets the display name of a JSX component for dev tools
|
|
|
|
function getComponentDisplayName (Component) {
|
|
|
|
return Component.displayName || Component.name || 'Unknown'
|
|
|
|
}
|
|
|
|
|
2017-05-10 22:23:11 +00:00
|
|
|
export default ComposedComponent => {
|
|
|
|
return class WithData extends React.Component {
|
2017-07-01 21:51:20 +00:00
|
|
|
static displayName = `WithData(${getComponentDisplayName(ComposedComponent)})`
|
2017-05-10 22:23:11 +00:00
|
|
|
static propTypes = {
|
|
|
|
serverState: PropTypes.object.isRequired
|
|
|
|
}
|
2017-03-30 18:21:13 +00:00
|
|
|
|
|
|
|
static async getInitialProps (ctx) {
|
2017-05-10 22:23:11 +00:00
|
|
|
let serverState = {}
|
2017-03-30 18:21:13 +00:00
|
|
|
|
2017-05-10 22:23:11 +00:00
|
|
|
// Evaluate the composed component's getInitialProps()
|
|
|
|
let composedInitialProps = {}
|
|
|
|
if (ComposedComponent.getInitialProps) {
|
|
|
|
composedInitialProps = await ComposedComponent.getInitialProps(ctx)
|
2017-03-30 18:21:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-01 21:56:12 +00:00
|
|
|
// Run all GraphQL queries in the component tree
|
2017-05-10 22:23:11 +00:00
|
|
|
// and extract the resulting data
|
2017-03-30 18:21:13 +00:00
|
|
|
if (!process.browser) {
|
2017-05-10 22:23:11 +00:00
|
|
|
const apollo = initApollo()
|
|
|
|
const redux = initRedux(apollo)
|
2017-07-01 21:56:12 +00:00
|
|
|
// Provide the `url` prop data in case a GraphQL query uses it
|
2017-05-10 22:23:11 +00:00
|
|
|
const url = {query: ctx.query, pathname: ctx.pathname}
|
|
|
|
|
2017-07-01 21:56:12 +00:00
|
|
|
try {
|
|
|
|
// Run all GraphQL queries
|
|
|
|
await getDataFromTree(
|
|
|
|
// No need to use the Redux Provider
|
|
|
|
// because Apollo sets up the store for us
|
|
|
|
<ApolloProvider client={apollo} store={redux}>
|
|
|
|
<ComposedComponent url={url} {...composedInitialProps} />
|
|
|
|
</ApolloProvider>
|
|
|
|
)
|
|
|
|
} catch (error) {
|
|
|
|
// Prevent Apollo Client GraphQL errors from crashing SSR.
|
|
|
|
// Handle them in components via the data.error prop:
|
|
|
|
// http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
|
|
|
|
}
|
2017-06-28 17:07:21 +00:00
|
|
|
// getDataFromTree does not call componentWillUnmount
|
|
|
|
// head side effect therefore need to be cleared manually
|
|
|
|
Head.rewind()
|
2017-03-30 18:21:13 +00:00
|
|
|
|
2017-05-10 22:23:11 +00:00
|
|
|
// Extract query data from the store
|
|
|
|
const state = redux.getState()
|
2017-03-30 18:21:13 +00:00
|
|
|
|
2017-05-10 22:23:11 +00:00
|
|
|
// No need to include other initial Redux state because when it
|
|
|
|
// initialises on the client-side it'll create it again anyway
|
|
|
|
serverState = {
|
2017-07-01 21:56:12 +00:00
|
|
|
apollo: { // Only include the Apollo data state
|
2017-05-10 22:23:11 +00:00
|
|
|
data: state.apollo.data
|
2017-03-30 18:21:13 +00:00
|
|
|
}
|
2017-05-10 22:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
serverState,
|
|
|
|
...composedInitialProps
|
2017-03-30 18:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
2017-05-10 22:23:11 +00:00
|
|
|
this.apollo = initApollo()
|
|
|
|
this.redux = initRedux(this.apollo, this.props.serverState)
|
2017-03-30 18:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
2017-05-10 22:23:11 +00:00
|
|
|
// No need to use the Redux Provider
|
|
|
|
// because Apollo sets up the store for us
|
|
|
|
<ApolloProvider client={this.apollo} store={this.redux}>
|
|
|
|
<ComposedComponent {...this.props} />
|
2017-03-30 18:21:13 +00:00
|
|
|
</ApolloProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 22:23:11 +00:00
|
|
|
}
|