简体   繁体   English

Apollo React - ApolloClient 设置中的“useMutation”?

[英]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.

相关问题 ApolloClient不是构造函数react-apollo - ApolloClient is not a constructor react-apollo ApolloClient:永久违反1-正确设置apollo客户端 - ApolloClient: Invariant Violation 1 – setup apollo client properly 对使用 React Apollo 的 useMutation 钩子不工作的乐观响应 - optimisticResponse not working for useMutation hook using React Apollo graphQL 使用 React 钩子处理突变错误(useMutation + Apollo-client) - graphQL mutation error handling with React hooks(useMutation + Apollo-client) 在 onClick 中使用解构的 useMutation function 时,Apollo 反应 typescript 错误 - Apollo react typescript error when using destructured useMutation function in onClick Apollo 客户端 useMutation 数据总是返回 undefined - Apollo Client useMutation data always returns undefined Apollo Client useMutation 不调用 API - Apollo Client useMutation does not call the API 使用 Apollo 客户端 useMutation 后未触发 useEffect - useEffect not triggered after useMutation using Apollo Client 我想制作与 apollo/client 提供的 useFormik 或 useMutation 类似的反应挂钩,在其中我可以运行 onSuccess 或 onError 函数 - I want to make similar react hook same as useFormik or useMutation provided by apollo/client, in where I can run onSuccess or onError functions 无法处理React ApolloClient中的错误 - Cannot handle error in React ApolloClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM