2018-11-25 14:14:36 +00:00
|
|
|
import React from 'react'
|
|
|
|
import { QueryRenderer, fetchQuery } from 'react-relay'
|
|
|
|
import NextApp, { Container } from 'next/app'
|
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
import { initEnvironment, createEnvironment } from '../lib/createEnvironment'
|
2018-11-25 14:14:36 +00:00
|
|
|
|
|
|
|
export default class App extends NextApp {
|
|
|
|
static getInitialProps = async ({ Component, router, ctx }) => {
|
2018-12-17 16:34:32 +00:00
|
|
|
const { variables } = Component.getInitialProps
|
|
|
|
? await Component.getInitialProps(ctx)
|
|
|
|
: {}
|
2018-11-25 14:14:36 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (initEnvironment) {
|
|
|
|
const { environment, relaySSR } = initEnvironment()
|
|
|
|
|
|
|
|
await fetchQuery(environment, Component.query, variables)
|
|
|
|
|
|
|
|
return {
|
|
|
|
variables,
|
|
|
|
relayData: await relaySSR.getCache()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
variables
|
|
|
|
}
|
2018-12-17 16:34:32 +00:00
|
|
|
}
|
2018-11-25 14:14:36 +00:00
|
|
|
|
|
|
|
render () {
|
|
|
|
const { Component, variables = {}, relayData } = this.props
|
|
|
|
const environment = createEnvironment(
|
|
|
|
relayData,
|
|
|
|
JSON.stringify({
|
|
|
|
queryID: Component.query ? Component.query().name : undefined,
|
|
|
|
variables
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<QueryRenderer
|
|
|
|
environment={environment}
|
|
|
|
query={Component.query}
|
|
|
|
variables={variables}
|
|
|
|
render={({ error, props }) => {
|
|
|
|
if (error) return <div>{error.message}</div>
|
|
|
|
else if (props) return <Component {...props} />
|
|
|
|
return <div>Loading</div>
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|