Я пытаюсь получить пример Next.js под названием «С Apollo Auth»: https://github.com/zeit/next.js/tree/master/examples/with-apollo-auth
Я все настроил, но получаю ошибку:
TypeError: Cannot destructure property `req` of 'undefined' or 'null'.
const { AppTree, ctx: { req, res }} = ctx <----- Error line
Этоошибка появляется на следующей странице, которая представляет собой стандартный код, предоставленный Next.js:
import initApollo from './initApollo'
function parseCookies (req, options = {}) {
return cookie.parse(req ? req.headers.cookie || '' : document.cookie, options)
}
export default App => {
return class WithData extends React.Component {
// It is needed for better devtools experience. Check how react devtools shows it: "MyApp WithData"
static displayName = `WithData(${App.displayName})`
// Since apolloState is required but it is missed before this method returns the new props,
// so it is needed to provide defaults
static defaultProps = {
apolloState: {}
}
static propTypes = {
apolloState: PropTypes.object.isRequired
}
static async getInitialProps (ctx) {
const { AppTree, ctx: { req, res }} = ctx
const apollo = initApollo(
{},
{
getToken: () => parseCookies(req).token
}
)
ctx.ctx.apolloClient = apollo
let appProps = {}
if (App.getInitialProps) {
appProps = await App.getInitialProps(ctx)
}
if (res && res.finished) {
// When redirecting, the response is finished.
// No point in continuing to render
return {}
}
if (typeof window === 'undefined') {
// Run all graphql queries in the component tree
// and extract the resulting data
try {
// Run all GraphQL queries
await getDataFromTree(<AppTree {...appProps} apolloClient={apollo} />)
} catch (error) {
// Prevent Apollo Client GraphQL errors from crashing SSR.
// Handle them in components via the data.error prop:
// https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-query-data-error
console.error('Error while running `getDataFromTree`', error)
}
// getDataFromTree does not call componentWillUnmount
// head side effect therefore need to be cleared manually
Head.rewind()
}
// Extract query data from the Apollo's store
const apolloState = apollo.cache.extract()
return {
...appProps,
apolloState
}
}
constructor (props) {
super(props)
// `getDataFromTree` renders the component first, the client is passed off as a property.
// After that rendering is done using Next's normal rendering pipeline
// @ts-ignore
this.apolloClient = initApollo(props.apolloState, {
getToken: () => {
// @ts-ignore
return parseCookies().token
}
})
}
render () {
// @ts-ignore
return <App apolloClient={this.apolloClient} {...this.props} />
}
}
}
Я не уверен, почему ctx не определен.