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屋!