'AWS AppSync Amplify Relationship with Transformer v2 - Can't Retrieve related type data

So I am currently playing with AWS AppSync and Amplify, and all is good except, with the new Transformer V2, I am struggling to get things to work.

Here's my schema:

type Post
  @model
  @auth(
    rules: [
      { allow: owner, ownerField: "username" }
      { allow: public, operations: [read] }
    ]
  ) {
  id: ID!
  title: String!
  content: String!
  username: String
    @index(name: "postsByUsername", queryField: "postsByUsername")
  coverImage: String
  comments: [Comment] @hasMany
 
}


type Comment
  @model
  @auth(
    rules: [
      { allow: owner, ownerField: "createdBy" }
      { allow: public, operations: [read] }
    ]
  ) {
  id: ID!
  message: String
  
}

I can create a post and a comment. However, whenever I query a list of posts, I never get comments.

Here's an example of a query:

query MyQuery {
  listPosts {
    items {
      comments {
        items {
          id
          message
          createdAt
        }
      }
      content
      title
      username
    }
  }
}

and here's the corresponding result I get:

{
  "data": {
    "listPosts": {
      "items": [
        {
          "comments": {
            "items": []
          },
          "content": "Testing stuff",
          "title": "Today is the day",
          "username": "bawp"
        },
        {
          "comments": {
            "items": []
          },
          "content": "### hello\n````\n function call()\n```",
          "title": "next item of the day",
          "username": "paulod"
        },
        {
          "comments": {
            "items": []
          },
          "content": "Hello Word",
          "title": "santo dia",
          "username": "paulod"
        }
      ]
    }
  }
}

Notice how the

     "comments": {
            "items": []
          }, 

It is always empty!

Even though, when I query the Comments table, I get at least one comment added.

query MyQuery {
  listComments {
    items {
      id
      message
      createdBy
    }
  }
}

Result:

{
  "data": {
    "listComments": {
      "items": [
        {
          "id": "ddddf58b-df1c-498c-97b4-6d61361b4b9e",
          "message": "Thjis is may coment here\n\n```\nCode could go here also!\n```",
          "createdBy": "paulod"
        }
      ]
    }
  }
}

I am not sure what I am missing here. Remember I am using new directives (Transformer v2) as opposed to the old relationship directives like

@connection

Any help is greatly appreciated.

Thank you very much!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source