2018-02-24 23:17:04 +00:00
|
|
|
import React from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { ApolloProvider, getDataFromTree } from 'react-apollo'
|
|
|
|
import Head from 'next/head'
|
|
|
|
import initApollo from './initApollo'
|
|
|
|
|
|
|
|
// Gets the display name of a JSX component for dev tools
|
2018-08-20 06:31:24 +00:00
|
|
|
function getComponentDisplayName (Component) {
|
2018-02-24 23:17:04 +00:00
|
|
|
return Component.displayName || Component.name || 'Unknown'
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ComposedComponent => {
|
|
|
|
return class WithApollo extends React.Component {
|
|
|
|
static displayName = `WithApollo(${getComponentDisplayName(
|
|
|
|
ComposedComponent
|
|
|
|
)})`
|
|
|
|
static propTypes = {
|
|
|
|
serverState: PropTypes.object.isRequired
|
|
|
|
}
|
|
|
|
|
2018-08-20 06:31:24 +00:00
|
|
|
static async getInitialProps (ctx) {
|
2018-02-24 23:17:04 +00:00
|
|
|
// Initial serverState with apollo (empty)
|
|
|
|
let serverState = {
|
|
|
|
apollo: {
|
|
|
|
data: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate the composed component's getInitialProps()
|
|
|
|
let composedInitialProps = {}
|
|
|
|
if (ComposedComponent.getInitialProps) {
|
|
|
|
composedInitialProps = await ComposedComponent.getInitialProps(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run all GraphQL queries in the component tree
|
|
|
|
// and extract the resulting data
|
|
|
|
if (!process.browser) {
|
|
|
|
const apollo = initApollo()
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Run all GraphQL queries
|
|
|
|
await getDataFromTree(
|
|
|
|
<ApolloProvider client={apollo}>
|
|
|
|
<ComposedComponent {...composedInitialProps} />
|
|
|
|
</ApolloProvider>,
|
|
|
|
{
|
|
|
|
router: {
|
|
|
|
asPath: ctx.asPath,
|
|
|
|
pathname: ctx.pathname,
|
|
|
|
query: ctx.query
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} catch (error) {
|
|
|
|
// Prevent Apollo Client GraphQL errors from crashing SSR.
|
|
|
|
// Handle them in components via the data.error prop:
|
2018-08-09 21:00:08 +00:00
|
|
|
// https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-query-data-error
|
2018-02-24 23:17:04 +00:00
|
|
|
}
|
|
|
|
// getDataFromTree does not call componentWillUnmount
|
|
|
|
// head side effect therefore need to be cleared manually
|
|
|
|
Head.rewind()
|
|
|
|
|
|
|
|
// Extract query data from the store
|
|
|
|
const state = {}
|
|
|
|
|
|
|
|
// Extract query data from the Apollo store
|
|
|
|
serverState = Object.assign(
|
|
|
|
state,
|
|
|
|
{ apollo: { data: apollo.cache.extract() } }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
serverState,
|
|
|
|
...composedInitialProps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 06:31:24 +00:00
|
|
|
constructor (props) {
|
2018-02-24 23:17:04 +00:00
|
|
|
super(props)
|
|
|
|
this.apollo = initApollo(props.serverState.apollo.data)
|
|
|
|
}
|
|
|
|
|
2018-08-20 06:31:24 +00:00
|
|
|
render () {
|
2018-02-24 23:17:04 +00:00
|
|
|
return (
|
|
|
|
<ApolloProvider client={this.apollo}>
|
|
|
|
<ComposedComponent {...this.props} />
|
|
|
|
</ApolloProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|