2017-08-14 16:41:12 +00:00
|
|
|
import React from 'react'
|
|
|
|
import initEnvironment from './createRelayEnvironment'
|
|
|
|
import { fetchQuery } from 'react-relay'
|
|
|
|
import RelayProvider from './RelayProvider'
|
|
|
|
|
|
|
|
export default (ComposedComponent, options = {}) => {
|
|
|
|
return class WithData extends React.Component {
|
|
|
|
static displayName = `WithData(${ComposedComponent.displayName})`
|
|
|
|
|
|
|
|
static async getInitialProps (ctx) {
|
|
|
|
// Evaluate the composed component's getInitialProps()
|
|
|
|
let composedInitialProps = {}
|
|
|
|
if (ComposedComponent.getInitialProps) {
|
|
|
|
composedInitialProps = await ComposedComponent.getInitialProps(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
let queryProps = {}
|
|
|
|
let queryRecords = {}
|
|
|
|
const environment = initEnvironment()
|
|
|
|
|
|
|
|
if (options.query) {
|
|
|
|
// Provide the `url` prop data in case a graphql query uses it
|
|
|
|
// const url = { query: ctx.query, pathname: ctx.pathname }
|
|
|
|
const variables = {}
|
|
|
|
// TODO: Consider RelayQueryResponseCache
|
|
|
|
// https://github.com/facebook/relay/issues/1687#issuecomment-302931855
|
|
|
|
queryProps = await fetchQuery(environment, options.query, variables)
|
2018-12-17 16:34:32 +00:00
|
|
|
queryRecords = environment
|
|
|
|
.getStore()
|
|
|
|
.getSource()
|
|
|
|
.toJSON()
|
2017-08-14 16:41:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...composedInitialProps,
|
|
|
|
...queryProps,
|
|
|
|
queryRecords
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
|
|
|
this.environment = initEnvironment({
|
|
|
|
records: props.queryRecords
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<RelayProvider environment={this.environment} variables={{}}>
|
|
|
|
<ComposedComponent {...this.props} />
|
|
|
|
</RelayProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|