gpt4 book ai didi

apollo-client - Apollo Boost 的 operation.setContext 错误

转载 作者:行者123 更新时间:2023-12-04 08:47:22 26 4
gpt4 key购买 nike

我一直在使用 header 对 Apollo Client 进行身份验证。以下工作正常:

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 });
},
},
},
},
});

现在我收到一个错误,我的 token 没有被添加:
[Network error]: TypeError: operation.setContext is not a function

最佳答案

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

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

关于apollo-client - Apollo Boost 的 operation.setContext 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48864040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com