Instaclone Tutorial Walkthrough Help

What I want to do

Hi it’s me again, I am learning the ropes of Dgraph, and I am walking through this tutorial by @sakib but have been stuck on this step:

Adding comments on InstaClone posts

You have to know the IDs of all the posts since you’ll need that to connect a comment with the post via the commentOn field. You also need to know the usernames of existing users for the comment By field. The following query fetches IDs of existing posts along with usernames of their posters:

query queryAllPosts {
  queryPost {
    id
    postedBy {
      username
    }
  }
}

It gives the following response: (I managed to retrieve the posts correctly)

{
  "queryPost": [
    {
      "id": "0x4e5d9b3b",
      "postedBy": {
        "username": "karen"
      }
    }, ...

Now we’re ready to do an addComment mutation to add some comments: (But I am unable to do this step)

mutation AddComments($commentInput: [AddCommentInput!]!) {
  addComment(input: $commentInput) {
    comment {
      text
      commentBy {
        username
      }
      commentOn {
        description
        postedBy {
          username
        }
      }
    }
  }
}

For simplicity, let’s assume that existing users will comment on one another’s posts. The following is the data of the query variable:

{
  "commentInput": [
    {
      "text": "This book's a fantastic addition to your other collections",
      "commentBy": {
        "username": "kmulbery0"
      },
      "commentOn": {
        "id": "0x4e5d9b3b"
      }
    }, ...

This should result in a successful response:

{
  "addComment": {
    "comment": [
      {
        "text": "This book's a fantastic addition to your other collections",
        "commentBy": {
          "username": "kmulbery0"
        },
        "commentOn": {
          "description": "A new antique book I just collected!",
          "postedBy": {
            "username": "karen"
          }
        }
      }, ...

Instead, the response I get is:

{
  "errors": [
    {
      "message": "couldn't rewrite mutation addComment because failed to rewrite mutation payload because ID \"0x4e5d9b3b\" isn't a Post",
      "path": [
        "addComment"
      ]
    },
    {
      "message": "couldn't rewrite mutation addComment because failed to rewrite mutation payload because ID \"0x4e5d9b3e\" isn't a Post",
      "path": [
        "addComment"
      ]
    },...

What I did

I have dropped tables and input the mutations again, twice, but it produces the same error.

Dgraph metadata

Using Dgraph Cloud

Hi @Debiday. Glad to see you going through the article and learning about Dgraph and GraphQL!

The mutation is failing in your case because Dgraph generates those ids automatically, and they’re unique. They aren’t going to be the same as they were in my case.

That’s why we did a queryPost to find out what ids Dgraph generated for us. Then we can go ahead and use them.

Hope this helps. Feel free to ask any questions you may have.

2 Likes

Thanks @sakib! It worked perfectly after replacing the unique IDs. Totally missed that! Otherwise, everything has been a breeze to follow and understand, I am looking forward to the next post already! :grinning:

1 Like