Converting update query to an upsert query

I have a type which has an ID. When I get the updates for that type, I don’t know if it is already present in the DB or not. I just want to add it if not present, and update it if present.

But, currently, I’ll have to try add first, then fail, then run update with a filter for that ID. That’s unnecessary work.

If you had an dgraph ID wouldn’t you know then that it already existed? I might be missing something here though…

It’s not a Dgraph ID. It is an external ID, coming from an external system, which is being internally mapped to a Dgraph ID.

Ah, that makes more sense then. Sort of then needing a SQL syntax of INSERT OR UPDATE. The trick here is matching the external ID to a unique key in Dgraph if not using the ID type (my incoming data has a guid formatted id). But Dgraph has no way to force uniqueness on any type other than ID.

I can see how an upsert like this would be useful. Currently, updates check that the node should already exist whereas add only adds it if it doesn’t exist. We could possible provide an optional argument in the updateType mutation to achieve this behavior which would also create the node if it doesn’t exist.

@JatinDevDG could you please note this down in the Community requests for this week so that we could discuss this along with other user issues?

1 Like


@mrjn Can you please post the schema and the steps that you tried? That will clear it a bit more and help us to look into it in more details.