How to add the tag without duplication?

Hello, guys.

I’m creating a tiny blog system with Dgraph and Next.js. Saving the blog post, I post the following json object to PUT /api/posts/{uid} endpoint.

{
  "uid": "0x3a992",
  "title": "This is title",
  "user": "brainvader",
  "date": "2020-10-08T18:01:00Z",
  "posts": [
    {
      "uid": "0x3a98e",
       content: ""
    }
  ],
  "tags": [
    {
      "uid": "0x3a991",
      "tag_name": "dgraph"
    }
  ]
}

How to add only when there is no tags in the dgraph? What query do I have to use to prevent the duplicate tag nodes are added?

You can first do a query for the uid and then do the mutation only if tags are absent. You can take a look at conditional upserts for example.