Can't run multiple mutations in same transaction

Moved from GitHub dgraph-js/113

Posted by benwerner01:

I’m trying to run multiple mutations in the same transaction. To my understanding something like this should work:

runMutations = async (mutations: DGraphJS.Mutation[]): Promise<DGraphJS.Response> => {
    const txn = this.client.newTxn();
    try {
      const req = new DGraphJS.Request();
      req.setCommitNow(true);
      req.setMutationsList(mutations);

      return txn.doRequest(req);
    } finally {
      await txn.discard();
    }
  }

However this throws the following error:

StartTs cannot be zero while committing a transaction

What’s weird to me is all of the mutations work when using this function. Is this error a bug, or am I doing something wrong?

benwerner01 commented :

Not sure about the above behaviour, but have found a solution for my problem:

runMutations = async (mutations: DGraphJS.Mutation[]): Promise<DGraphJS.Response[]> => {
    const txn = this.client.newTxn();
    try {
      const responses: DGraphJS.Response[] = [];
      for (let i = 0; i < mutations.length; i++) responses.push(await txn.mutate(mutations[i]));
      txn.commit();
      return responses;
    } finally {
      await txn.discard();
    }
  }

Calling txn.mutate for each mutation (sequentially not in parallel) works like a charm :slight_smile: