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'
|
|
|
|
|
2017-07-01 21:51:20 +00:00
|
|
|
// Gets the display name of a JSX component for dev tools
|
2017-12-27 18:57:57 +00:00
|
|
|
function getComponentDisplayName(Component) {
|
2017-07-01 21:51:20 +00:00
|
|
|
return Component.displayName || Component.name || 'Unknown'
|
|
|
|
}
|
|
|
|
|
2017-05-10 22:23:11 +00:00
|
|
|
export default ComposedComponent => {
|
|
|
|
return class WithData extends React.Component {
|
2017-12-27 18:57:57 +00:00
|
|
|
static displayName = `WithData(${getComponentDisplayName(
|
|
|
|
ComposedComponent
|
|
|
|
)})`
|
2017-05-10 22:23:11 +00:00
|
|
|
static propTypes = {
|
2018-01-31 09:40:32 +00:00
|
|
|
serverState: PropTypes.object.isRequired
|
2017-05-10 22:23:11 +00:00
|
|
|
}
|
2017-03-30 18:21:13 +00:00
|
|
|
|
2017-12-27 18:57:57 +00:00
|
|
|
static async getInitialProps(ctx) {
|
2018-01-31 09:40:32 +00:00
|
|
|
// Initial serverState with apollo (empty)
|
|
|
|
let serverState = {
|
2017-12-27 18:57:57 +00:00
|
|
|
apollo: {
|
|
|
|
data: {}
|
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
|
2017-07-01 21:56:12 +00:00
|
|
|
try {
|
|
|
|
// Run all GraphQL queries
|
|
|
|
await getDataFromTree(
|
2017-12-27 18:57:57 +00:00
|
|
|
<ApolloProvider client={apollo}>
|
|
|
|
<ComposedComponent {...composedInitialProps} />
|
|
|
|
</ApolloProvider>,
|
|
|
|
{
|
|
|
|
router: {
|
|
|
|
asPath: ctx.asPath,
|
|
|
|
pathname: ctx.pathname,
|
|
|
|
query: ctx.query
|
|
|
|
}
|
|
|
|
}
|
2017-07-01 21:56:12 +00:00
|
|
|
)
|
|
|
|
} 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-12-27 18:57:57 +00:00
|
|
|
// Extract query data from the Apollo store
|
2018-01-31 09:40:32 +00:00
|
|
|
serverState = {
|
2017-12-27 18:57:57 +00:00
|
|
|
apollo: {
|
|
|
|
data: apollo.cache.extract()
|
2017-03-30 18:21:13 +00:00
|
|
|
}
|
2017-05-10 22:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2018-01-31 09:40:32 +00:00
|
|
|
serverState,
|
2017-05-10 22:23:11 +00:00
|
|
|
...composedInitialProps
|
2017-03-30 18:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 18:57:57 +00:00
|
|
|
constructor(props) {
|
2017-03-30 18:21:13 +00:00
|
|
|
super(props)
|
2018-01-31 09:40:32 +00:00
|
|
|
this.apollo = initApollo(this.props.serverState.apollo.data)
|
2017-03-30 18:21:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-27 18:57:57 +00:00
|
|
|
render() {
|
2017-03-30 18:21:13 +00:00
|
|
|
return (
|
2017-12-27 18:57:57 +00:00
|
|
|
<ApolloProvider client={this.apollo}>
|
2017-05-10 22:23:11 +00:00
|
|
|
<ComposedComponent {...this.props} />
|
2017-03-30 18:21:13 +00:00
|
|
|
</ApolloProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-01-31 09:40:32 +00:00
|
|
|
}
|