Increment / decrement update operation

Hey,

I need to atomicly increment a field. Firestore has a feature to simply send an “increment by n” update to the database, to avoid a non-atomic read-and-update operation on the client side.
Is this also possible in DGraph with graphql? I only found “remove” and “set” in the documentation :thinking:

If you search this forum, you will see a lot of old posts on this subject. Basically this feature does not exist, but it is probably not the best data model anyway for Dgraph.

If you have something like likes or votes, it is better to just count nodes than to try and do incrementing.

So you could have:

type Post {
  likes: [User]
  ...
}

This model won’t allow a user to vote more than once, for example. Then, just count those nodes to get number of likes.

J

Note: Now that Firestore has the count() directive, there is an argument to be made that you should not use incrementing there either in certain situations.

1 Like

It is possible to use a custom Lambda resolver to execute a conditional DQL mutation, which then acts as a read-and-update primitive on the server side. If required, authentication needs to be implemented manually for the custom resolver.

Also interesting idea, a bit more elaborate. But does it solve concurrency? I thought lambda function could also run in parallel. So a queueing system would also be required?

Lambda resolvers can run in parallel, but the conditional (query-and-update) DQL mutation would be acting as the read-and-update primitive. Retry mechanisms may be needed.