我已经像这样设置了我的中继环境:
// createRelayEnvironment.ts
let relayEnvironment
export default function initEnvironment({ records }: EnvProps) {
// Create a network layer from the fetch function
const network = Network.create(fetchQuery)
const store = new Store(new RecordSource(records || {}))
// Make sure to create a new Relay environment for every server-side request so that data
// isn't shared between connections
if (!(process as any).browser) {
return new Environment({
network,
store
})
}
// reuse Relay environment on client-side
if (!relayEnvironment) {
relayEnvironment = new Environment({
network,
store
})
}
return relayEnvironment
}
并获取数据:
class WithData extends React.Component {
environment: Environment
constructor(props: any) {
super(props)
this.environment = initEnvironment({
records: props.queryRecords
})
}
static async getInitialProps(ctx) {
let composedInitialProps = {}
if (ComposedComponent.getInitialProps) {
composedInitialProps = await ComposedComponent.getInitialProps(ctx)
}
let queryProps = {}
let queryRecords = {}
const environment = initEnvironment({ records: {} })
if (options.query) {
const variables = options.mapProps ? options.mapProps() : {}
here
queryProps = await fetchQuery(environment, options.query, variables)
queryRecords = environment.getStore().getSource().toJSON()
}
return {
...ctx.query, // query info from next-router. E.g "slug"
...composedInitialProps,
...queryProps,
queryRecords
}
}
render() {
const relay = {
environment: this.environment
}
return (
<ReactRelayContext.Provider value={{ environment: this.environment }}>
<ComposedComponent {...(this.props as P)} {...{ relay }} />
</ReactRelayContext.Provider>
)
}
}
}
我收到以下错误:
Invariant Violation: RelayContext: Expected `context.relay` to be an object conforming to the `RelayContext` interface, got `[object Object]`.
NextJS 11 和 Relay v6.0.0 正在发生这种情况。它在 v5.0.0 上运行良好。这些是相当旧的版本,但我正在尝试升级。
通过查看源代码,这表明 Relay 环境不是正确的格式,但我不明白这是怎么回事,因为我只是按照文档的初始化说明进行操作
有什么简单的我做错了吗?