Single operation increment / decrement numbers

Dgraph has upsert blocks now, so you can do these updates in a single request:

upsert {
  query {
    counter(func: has(counter.val)) {
      old: A as counter.val
      new: B as math(A+1)
    }
  }
  mutation @if(gt(len(A), 0)) {
    set {
      uid(A) <counter.val> val(B) .
    }
  }
}

Example response:

{
  "data": {
    "code": "Success",
    "message": "Done",
    "queries": {
      "counter": [
        {
          "old": 10,
          "new": 11
        }
      ]
    },
    "uids": {}
  },
4 Likes