简体   繁体   English

如何使用 Apollo Client 和 GraphQL HoC 并行运行多个突变?

[英]How to run multiple mutations in parallel using Apollo Client and GraphQL HoC?

I'm currently using the GraphQL HoC to pass a mutation through props.我目前正在使用 GraphQL HoC 通过道具传递突变。 However, I'd like to run a batch of mutations simultaneously, while also having error handling and knowing which mutations failed - with the ability to run the failed ones again.然而,我想同时运行一批突变,同时也有错误处理并知道哪些突变失败了——能够再次运行失败的突变。 I don't know how many mutations I'll run, it'll depend on the number of IDs i get which will be passed down as an Array in props.我不知道我会运行多少突变,这取决于我获得的 ID 数量,这些 ID 将作为道具中的数组传递下去。

What would be the best way to achieve this?实现这一目标的最佳方法是什么?

My initial thought was to somehow use the Map method on the array and run the mutation on each one.我最初的想法是以某种方式在数组上使用 Map 方法并对每个数组运行突变。 I dont know how I'll be able to track which ones failed using this method, I also don't know how to run them in parallel我不知道如何使用此方法跟踪哪些失败,我也不知道如何并行运行它们

The mutation will look something like this:突变看起来像这样:

updateUserAccount({userId, reason})

I will need to run anywhere between 5-10 of these in parallel我需要并行运行其中 5-10 个之间的任何地方

I will pass the mutation through props using the graphql HoC so I'll have access to the mutation in my component.我将使用 graphql HoC 通过道具传递突变,这样我就可以访问我的组件中的突变。 I'd like to run the failed ones 2 more times.我想再运行失败的 2 次。

Use Promise.all() to call the mutations.使用Promise.all()调用突变。 Also you need to create some mapper function to control the attempts when some request fails:您还需要创建一些映射器 function 来控制某些请求失败时的尝试:

Create and object for every request:为每个请求创建和 object:

const ids = ["1", "2", "3"];
const meta = ids.map(id => ({
  id,
  fn: () => updateUserAccount({id, reason}), //--> mutation function
  attemps: 0, //--> <= 3
  status: null, //--> ["OK", "ERROR"]
  data: null //-> response data, equal null if fails
}));

Create the mapper function:创建映射器 function:

Here you can control the function attempts.在这里你可以控制 function 次尝试。 The request will always resolve, this way you don't need to worry about rejections.该请求将始终得到解决,这样您就不必担心被拒绝。 If the request fails after 3 attempts you will resolve the object with data equal null and status equal error.如果请求在 3 次尝试后失败,您将解决 object 数据等于 null 和状态等于错误。

const mapper = item => {
  return new Promise(async resolve => {
    const call = async (attempts = 0) => {
      try {
        const data = await item.fn();
        resolve({ ...item, status: "OK", attempts, data });
      } catch (err) {
        ++attempts;
        if (attempts < 3) {
          call(attempts);
        } else {
          resolve({ ...item, status: "ERROR", attempts, data: null });
        }
      }
    };
    call();
  });
};

Execute requests:执行请求:

Run all function. You won't get any rejection, the mapper function is responsible for that. 运行所有 function。你不会得到任何拒绝,映射器 function 对此负责。
const fails = response.filter(item => item.status === "ERROR");

If you need to know which function failed just check the response object:如果您需要知道哪个 function 失败了,只需检查响应 object:

 const fails = response.filter(item => item.status === "ERROR");

I'm currently using the GraphQL HoC to pass a mutation through props.我目前正在使用 GraphQL HoC 通过道具传递突变。 However, I'd like to run a batch of mutations simultaneously, while also having error handling and knowing which mutations failed - with the ability to run the failed ones again.但是,我想同时运行一批突变,同时还要进行错误处理并知道哪些突变失败了——能够再次运行失败的突变。 I don't know how many mutations I'll run, it'll depend on the number of IDs i get which will be passed down as an Array in props.我不知道我会运行多少个突变,这取决于我得到的 ID 的数量,这些 ID 将作为 props 中的数组传递。

What would be the best way to achieve this?实现这一目标的最佳方法是什么?

My initial thought was to somehow use the Map method on the array and run the mutation on each one.我最初的想法是在阵列上以某种方式使用 Map 方法并在每个阵列上运行突变。 I dont know how I'll be able to track which ones failed using this method, I also don't know how to run them in parallel我不知道如何使用这种方法跟踪哪些失败,我也不知道如何并行运行它们

The mutation will look something like this:突变看起来像这样:

updateUserAccount({userId, reason})

I will need to run anywhere between 5-10 of these in parallel我需要并行运行其中的 5-10 个

I will pass the mutation through props using the graphql HoC so I'll have access to the mutation in my component.我将使用 graphql HoC 通过道具传递突变,这样我就可以访问我的组件中的突变。 I'd like to run the failed ones 2 more times.我想再运行两次失败的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM