【问题标题】:How to call multiple mutations at the same time?如何同时调用多个突变?
【发布时间】:2017-03-12 18:11:07
【问题描述】:

我有一个 id 数组,并且我创建了一个突变,允许我删除仅使用 1 个 id 的项目。
有没有办法使用Relay.Store.commitUpdate 或this.props.relay.commitUpdate 多次调用此突变?

【问题讨论】:

    标签: graphql relayjs


    【解决方案1】:

    我认为您可以将每个 Relay.Store.commitUpdate 包装在 Promise 中:

    commitMutationPromise = (Mutation, data) =>
      new Promise((resolve, reject) => {
        Relay.Store.commitUpdate(new Mutation(data), {
          onSuccess: (transaction) => {
            resolve(transaction);
          },
          onFailure: (transaction) => {
            reject(transaction);
          },
        });
      }
    

    并将您的突变作为一系列承诺提交并使用 Promise.all 捕获结果(但请记住它的快速失败行为)。 可能是这样的:

      handleDelete = (deleteIds) => {
        const deletePromisesArray = [];
        deleteIds.forEach(id => {
            deletePromisesArray.push(
              this.commitMutationPromise(DeleteMutation, { id })
            );
        });
        Promise.all(deletePromisesArray).then(values => {
              this.onSuccessDelete(result);
            }, error => {
              this.onFailDelete(error);
            });
      }
    

    【讨论】:

      猜你喜欢
      • 2020-03-13
      • 2018-03-22
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      相关资源
      最近更新 更多