Apollo React - ApolloClient 设置中的“useMutation"? [英] Apollo React - `useMutation` in ApolloClient setup?
问题描述
我有一个有趣的情况.
我想使用 Apollo 本身发起刷新令牌请求(也就是调用突变)
I want to initiate refresh token request USING Apollo itself (a.k.a to call a mutation)
知道如何实现这样的目标吗?
Any idea, how to achieve something like this?
export default new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError, operation, forward }) => {
// useMutation(REFRESH_ACCESS_TOKEN)
}),
]),
new HttpLink({...}),
})
基本上,我只是在创建新的 ApolloClient 实例时尝试在 onError
内部 useMutation(REFRESH_ACCESS_TOKEN)
.
Basically I'm just trying to useMutation(REFRESH_ACCESS_TOKEN)
inside onError
while creating new ApolloClient instance.
问题:无效的钩子调用.钩子只能在函数组件内部调用.
谢谢.
推荐答案
你不能在功能性 React 组件之外使用钩子.您可以直接使用客户端进行查询和更改——请记住,以这种方式运行的任何查询都不会被观察到(即,如果缓存发生变化,则不会更新).
You can't use a hook outside a functional React component. You can use the client directly to make queries and mutations -- just bear in mind that any queries ran this way will not be observable (i.e. won't be updated if the cache changes).
const client = new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError, operation, forward }) => {
client.mutate({ mutation, variables })
.then(({ data }) => {
console.log(data)
})
.catch((error) => {
console.log(error)
})
}),
new HttpLink({...}),
]),
})
export default client
这篇关于Apollo React - ApolloClient 设置中的“useMutation"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!