【问题标题】:operation.setContext errors with Apollo BoostApollo Boost 的 operation.setContext 错误
【发布时间】:2018-07-29 13:30:12
【问题描述】:

我一直在使用标头与 Apollo 客户端进行身份验证。以下工作正常:

const middlewareAuthLink = new ApolloLink((operation, forward) => {
    const token = localStorage.getItem('auth-token');
    const authorizationHeader = token ? `Bearer ${token}` : null;
    operation.setContext({
        headers: {
            authorization: authorizationHeader,
        },
    });
    return forward(operation);
});

我正在切换到 Apollo Boost:https://dev-blog.apollodata.com/zero-config-graphql-state-management-27b1f1b3c2c3

const client = new ApolloClient({
    uri: 'MY-GRAPHCOOL-API',
    fetchOptions: {
        credentials: 'include',
    },
    request: async operation => {
        operation.setContext({
            headers: {
                authorization: 'sadfjadsfsd',
            },
        });
    },
    clientState: {
        defaults: {
            CurrentUserIsLoggedIn: {
                __typename: 'CurrentUserIsLoggedIn',
                value: false,
            },
        },
        resolvers: {
            Mutation: {
                CurrentUserIsLoggedIn: (_, args, { cache }) => {
                    const data = {
                        CurrentUserIsLoggedIn: {
                            __typename: 'CurrentUserIsLoggedIn',
                            value: args.value,
                        },
                    };
                    cache.writeData({ data });
                },
            },
        },
    },
});

现在我收到一个错误,我的令牌没有被添加:

[Network error]: TypeError: operation.setContext is not a function

【问题讨论】:

  • 我收到同样的错误

标签: apollo-client


【解决方案1】:

GitHub 上有一个解决此问题的公开拉取请求:[apollo-boost] pass operation prop to config.request

在合并之前,您可以自己在node_modules 文件夹中的apollo-boost 包中应用更改,然后使用yarn build 运行其构建脚本。对我有用的快速修复。

【讨论】:

    猜你喜欢
    • 2021-01-15
    • 1970-01-01
    • 2019-02-20
    • 2019-12-27
    • 2017-06-23
    • 2018-12-27
    • 2018-08-31
    • 1970-01-01
    • 2020-03-01
    相关资源
    最近更新 更多