如何在 apollo 客户端中将突变链接在一起 [英] How to chain together Mutations in apollo client

查看:18
本文介绍了如何在 apollo 客户端中将突变链接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆信息存储在我的状态中,我需要使用突变将这些信息传递给我的 graphQL 服务器,但是我需要在调用下一个突变之前使用每个突变的结果,因为我需要:

I have a bunch of information stored in my state that I need to pass to my graphQL server using mutations, but I need to use the results of each mutation before I call the next one since I need to:

  • 在我的数据库中创建一个新对象
  • 使用为该对象生成的 id 创建另一个对象
  • 修改原对象存放第二个对象生成的id

我注意到 apollo Mutation 组件有一个 onCompleted 回调,但我不知道如何使用这个回调来触发另一个突变,或者这是否是解决我的问题的正确方法.我还研究了批处理我的突变以一次发送它们,但这似乎也不是解决方案.任何帮助将不胜感激

I noticed that apollo Mutation components have an onCompleted callback, but I don't know how to use this callback to fire off another mutation or whether it's the right way solve my problem. I also looked into batching my mutations to send them all at once but it doesn't seem like that is the solution either. Any help would be appreciated

推荐答案

正如你所提到的,解决它的独特方法是批量处理所有的突变,请查看 问题相关

As you've mentioned, the unique way to solve it is batching all the mutations, check out the issue related!

实际上,编写它们并没有那么糟糕,您可以这样做:

Actually, it's not that bad to compose them, you could do something like this:

const handleClick = async (mutation1Fn, mutation2Fn, mutation3Fn) => {
  const data1 = await mutation1Fn()
  const data2 = await mutation2Fn()
  const data3 = await mutation3Fn()
}

const Mutations = () => (
  <Composer
    components={[
      <Mutation mutation={mutation1} />,
      <Mutation mutation={mutation2} />,
      <Mutation mutation={mutation3} />
    ]}
  >
    {([mutation1Fn, mutation2Fn, mutation3Fn]) => (
      <button
        onClick={() => handleClick(mutation1Fn, mutation2Fn, mutation3Fn)}
      >
        exec!
      </button>
    )}
  </Composer>
)

如果您遇到什么困难,请告诉我!

Let me know if you're struggling with something!

这篇关于如何在 apollo 客户端中将突变链接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆