[英]Apollo React - `useMutation` in ApolloClient setup?
I have an interesting situation.我有一个有趣的情况。
I want to initiate refresh token request USING Apollo itself (aka to call a mutation)我想使用 Apollo 本身启动刷新令牌请求(也称为突变)
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({...}),
})
Basically I'm just trying to useMutation(REFRESH_ACCESS_TOKEN)
inside onError
while creating new ApolloClient instance.基本上我只是想在创建新的 ApolloClient 实例时在
onError
useMutation(REFRESH_ACCESS_TOKEN)
。
The problem: Invalid hook call. Hooks can only be called inside of the body of a function component.
问题:
Invalid hook call. Hooks can only be called inside of the body of a function component.
Invalid hook call. Hooks can only be called inside of the body of a function component.
Thanks.谢谢。
You can't use a hook outside a functional React component.你不能在功能性 React 组件之外使用钩子。 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 (ie 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.