How to do this mutation with DQL?

Hi, need help figuring out how to do a DQL mutation that is equivalent to the example below.
Given GraphQL types:

type Blog {
  title: String
  tagged: [Tag] @hasInverse(field: blogs)
}

type Tag {
  id: ID!
  tag_name: String
  blogs: [Blog]
}

By doing a mutation as follows:

mutation ($input: [AddBlogInput!]!) {
  addBlog(input: $input) {
    blog {
      title
      tagged {
        tag_name
      }
    }
  }
}

Mutation input:

{
	"input": [
		{
			"title": "DQL fundamentals",
			"tagged": [{
				"tag_name": "technical"
      },{
				"tag_name": "beginner"
      }]
		},
		{
			"title": "Advanced DQL",
			"tagged": [{
				"tag_name": "technical"
      },{
				"tag_name": "intermediate"
      }]
		}
	]
}

When I query by Blog type:

{
  blogs(func: type(Blog)) {
    uid
    Blog.title
    Blog.tagged {
      Tag.tag_name
    }
  }
}

Result of query by Blog type:

{
  "data": {
    "blogs": [
      {
        "uid": "0x704e2",
        "Blog.title": "DQL fundamentals",
        "Blog.tagged": [
          {
            "Tag.tag_name": "technical"
          },
          {
            "Tag.tag_name": "beginner"
          }
        ]
      },
      {
        "uid": "0x704e5",
        "Blog.title": "Advanced DQL",
        "Blog.tagged": [
          {
            "Tag.tag_name": "technical"
          },
          {
            "Tag.tag_name": "intermediate"
          }
        ]
      }
    ]
  }
}

Query by Tag type:

{
  tags(func: type(Tag)) {
    uid
    Tag.tag_name
    Tag.blogs {
      Blog.title
    }
  }
}

Result of query by Tag type:

{
  "data": {
    "tags": [
      {
        "uid": "0x704e1",
        "Tag.tag_name": "technical",
        "Tag.blogs": [
          {
            "Blog.title": "DQL fundamentals"
          }
        ]
      },
      {
        "uid": "0x704e3",
        "Tag.tag_name": "beginner",
        "Tag.blogs": [
          {
            "Blog.title": "DQL fundamentals"
          }
        ]
      },
      {
        "uid": "0x704e4",
        "Tag.tag_name": "technical",
        "Tag.blogs": [
          {
            "Blog.title": "Advanced DQL"
          }
        ]
      },
      {
        "uid": "0x704e6",
        "Tag.tag_name": "intermediate",
        "Tag.blogs": [
          {
            "Blog.title": "Advanced DQL"
          }
        ]
      }
    ]
  }
}

Question: How to write the mutation in DQL that will yield the same results as above?

Do you want the same results? Usually when using tags, you will want to use an @id or some other method to avoid tag duplication when inserting tags like this. If you notice above 0x704e1 and 0x704e4 are both a “technical” tag.

I think this should be clarified and understood first.

To make this in DQL is fairly simple using blank nodes. What part are you struggling with? What have you tried so far? Are you trying to run the mutation with JSON or RDF format?

Originally, yes I wanted the same results, but not anymore since you pointed out 0x704e1 and 0x704e4 are both a “technical” tag. I missed that, and I was expecting “technical” tag to be unique with only one uid.

I was struggling with understanding how the linkage happens.
In GraphQL, when I mutate a blog with tags, no matter I query a blog or tag, I can see the other.
Whereas in DQL, after I mutate a blog with tags, if I query the tags, I can’t see the blog. But now I understand this needs to be done manually like _:blog <Blog.tagged> _:tag . and _:tag <Tag.blogs> _:blog . .

For others who are learning dgraph like I am now, it helps to know how to get the uid.

1 Like