mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
968475fb95
Update examples with-relay-modern and with-relay-modern-server-express to react-relay 2.0.0 - react-relay has started to use new Context API instead of Legacy Context API - add `parseInt` because graphql 14.0.0 introduced stricter scalar value coercion Closes #6157
54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
import React from 'react'
|
|
import initEnvironment from './createRelayEnvironment'
|
|
import { fetchQuery, ReactRelayContext } from 'react-relay'
|
|
|
|
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) {
|
|
const variables = composedInitialProps.relayVariables || {}
|
|
// TODO: Consider RelayQueryResponseCache
|
|
// https://github.com/facebook/relay/issues/1687#issuecomment-302931855
|
|
queryProps = await fetchQuery(environment, options.query, variables)
|
|
queryRecords = environment
|
|
.getStore()
|
|
.getSource()
|
|
.toJSON()
|
|
}
|
|
|
|
return {
|
|
...composedInitialProps,
|
|
...queryProps,
|
|
queryRecords
|
|
}
|
|
}
|
|
|
|
constructor (props) {
|
|
super(props)
|
|
this.environment = initEnvironment({
|
|
records: props.queryRecords
|
|
})
|
|
}
|
|
|
|
render () {
|
|
return (
|
|
<ReactRelayContext.Provider value={{ environment: this.environment, variables: {} }}>
|
|
<ComposedComponent {...this.props} />
|
|
</ReactRelayContext.Provider>
|
|
)
|
|
}
|
|
}
|
|
}
|