简体   繁体   中英

How to call multiple mutations at the same time?

I have an array of ids, and I created a mutation that allow me to delete an item using only 1 id.
Is there any way to call this mutation multiple times using Relay.Store.commitUpdate or this.props.relay.commitUpdate ?

I think you can wrap each Relay.Store.commitUpdate in Promise:

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

And commit your mutations as array of promises and catch result with Promise.all(but keep in mind its fail-fast behaviour). It could be something like this:

  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);
        });
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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