graphqlrelayjs

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 ?


Solution

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