Support Batch Upserts in Live Loader

arijitAD commented :

You can run queries like this if you want to perform upsert operation in batches.

upsert {
  query {
    q1(func: eq(username, "Arijit")) {
      u1 as uid
    }
    q2(func: eq(username, "Arijit1")) {
      u2 as uid
    }
   q3(func: eq(username, "Arijit2")) {
      u3 as uid
    }
    q4(func: eq(username, "Arijit3")) {
      u4 as uid
    }
  }

  mutation {
    set {
      uid(u1) <email> "arijit@dgraph.io" .
      uid(u1) <knows> uid(u2) .
      uid(u2) <email> "arijit1@dgraph.io" .
      uid(u2) <knows> uid(u3) .
      uid(u3) <email> "arijit2@dgraph.io" .
      uid(u3) <knows> uid(u4) .
      uid(u4) <email> "arijit3@dgraph.io" .
      uid(u4) <knows> uid(u1) .
    }
  }
}

You can use Dgraph client to perform this operartion.

You can also batch transactions by starting a new transaction, sending upsert requests and then calling commit.

txn := dgraphClient.NewTxn()

for ... { // Perform multiple upsert query.
  query = `
    query {
        user as var(func: eq(email, "wrong_email@dgraph.io"))
    }`
  mu := &api.Mutation{
    SetNquads: []byte(`uid(user) <email> "correct_email@dgraph.io" .`),
  }
  req := &api.Request{
    Query: query,
    Mutations: []*api.Mutation{mu},
    **CommitNow:false**,
  }
  // Update email only if matching uid found.
  if _, err := dg.NewTxn().Do(ctx, req); err != nil {
    log.Fatal(err)
  }
}

err := txn.Commit(ctx)
if err == y.ErrAborted {
  // Retry or handle error
}