小编Kei*_*ith的帖子

React Apollo 错误:不变违规:在上下文中找不到“客户端”或作为选项传入

我正在使用 React、Apollo 和 Next.js 构建一个项目。我正在尝试将 react-apollo 更新到 3.1.3,现在在查看站点时出现以下错误。

不变违规:无法在上下文中找到“客户端”或作为选项传入。将根组件包装在 中,或通过选项传递 ApolloClient 实例。

如果我将 react-apollo 包降级到 2.5.8,它可以正常工作,所以我认为 2.5 和 3.x 之间发生了一些变化,但在 react-apollo 或 next-with-apollo 文档中找不到任何内容来表明那可能是什么。任何帮助将不胜感激。

withData.js

import withApollo from 'next-with-apollo';
import ApolloClient from 'apollo-boost';
import { endpoint } from '../config';

function createClient({ headers }) {
    return new ApolloClient({
        uri: endpoint,
        request: operation => {
            operation.setContext({
                fetchOptions: {
                    credentials: 'include'
                },
                headers
            });
        },
        // local data
        clientState: {
            resolvers: {
                Mutation: {}
            },
            defaults: {}
        }
    });
}

export default withApollo(createClient); …
Run Code Online (Sandbox Code Playgroud)

apollo reactjs graphql next.js

20
推荐指数
3
解决办法
3万
查看次数

标签 统计

apollo ×1

graphql ×1

next.js ×1

reactjs ×1