Apollo React - ApolloClient 设置中的 `useMutation`?
Apollo React - `useMutation` in ApolloClient setup?
我有一个有趣的情况。
我想使用 Apollo 本身发起刷新令牌请求(a.k.a 调用突变)
任何想法,如何实现这样的目标?
export default new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError, operation, forward }) => {
// useMutation(REFRESH_ACCESS_TOKEN)
}),
]),
new HttpLink({...}),
})
基本上我只是在创建新的 ApolloClient 实例时尝试在 onError
中 useMutation(REFRESH_ACCESS_TOKEN)
。
问题:
Invalid hook call. Hooks can only be called inside of the body of a function component.
谢谢。
您不能在功能性 React 组件之外使用挂钩。您可以直接使用客户端进行查询和更改——请记住,任何查询 运行 这种方式都不会被观察到(即,如果缓存发生变化,则不会更新)。
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 本身发起刷新令牌请求(a.k.a 调用突变)
任何想法,如何实现这样的目标?
export default new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError, operation, forward }) => {
// useMutation(REFRESH_ACCESS_TOKEN)
}),
]),
new HttpLink({...}),
})
基本上我只是在创建新的 ApolloClient 实例时尝试在 onError
中 useMutation(REFRESH_ACCESS_TOKEN)
。
问题:
Invalid hook call. Hooks can only be called inside of the body of a function component.
谢谢。
您不能在功能性 React 组件之外使用挂钩。您可以直接使用客户端进行查询和更改——请记住,任何查询 运行 这种方式都不会被观察到(即,如果缓存发生变化,则不会更新)。
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