【问题标题】:AWS AppSync Amplify Relationship with Transformer v2 - Can't Retrieve related type dataAWS AppSync Amplify 与 Transformer v2 的关系 - 无法检索相关类型数据
【发布时间】:2022-10-16 20:19:51
【问题描述】:

所以我目前正在使用 AWS AppSync 和 Amplify,一切都很好,除了使用新的 Transformer V2,我很难让事情正常工作。

这是我的架构:

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
  
}

我可以创建帖子和评论。但是,每当我查询帖子列表时,我从来没有得到 cmets。

下面是一个查询示例:

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

这是我得到的相应结果:

{
  "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"
        }
      ]
    }
  }
}

注意如何

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

它总是空的!

尽管如此,当我查询 Comments 表时,我至少添加了一条评论。

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

结果:

{
  "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"
        }
      ]
    }
  }
}

我不确定我在这里缺少什么。请记住,我使用的是新指令(Transformer v2),而不是旧的关系指令,例如

@connection

任何帮助是极大的赞赏。

非常感谢!

【问题讨论】:

    标签: amazon-web-services relationship amplify


    【解决方案1】:

    好的,这是对可能遇到其中一些问题的任何人的问题的回应。

    首先,

    架构! 由于我在表之间添加了一些关系,因此必须正确添加指令。以下是我对架构所做的更改:

    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(indexName: "byPost", fields: ["id"]) #check out: https://docs.amplify.aws/cli/graphql/data-modeling/#has-many-relationship
    }
    
    #Comment
    #queries: null - removes any authorization rules for queries, allowing anyone to query for comments
    #ownerField:createdBy - sets the createdBy field as the currently signed in user
    type Comment
      @model
      @auth(
        rules: [
          { allow: owner, ownerField: "createdBy" }
          { allow: public, operations: [read] }
        ]
      ) {
      id: ID!
      message: String
      post: Post @belongsTo(fields: ["postID"])
      postID: ID @index(name: "byPost")
     
    }
    
    

    接下来,因为 appsync 在每次推送时都会自动生成 queries.js 文件,所以我不得不手动编辑这个文件(这并不是真正的最终解决方案)。特别是 listPosts 查询:

    export const listPosts = /* GraphQL */ `
      query ListPosts(
        $filter: ModelPostFilterInput
        $limit: Int
        $nextToken: String
      ) {
        listPosts(filter: $filter, limit: $limit, nextToken: $nextToken) {
          items {
            id
            title
            content
            username
            coverImage
            comments {
              items {
                id
                message
                postID
                createdAt
                updatedAt
                createdBy
              }
            }
            createdAt
            updatedAt
          }
          nextToken
        }
      }
    `;
    
    
    

    你看,当这个文件被自动生成时,它会有“nextToken”而不是我想要查询的字段。换句话说,comments 字段将具有 { nextToken }

    因此,查询将如下所示:

    export const listPosts = /* GraphQL */ `
      query ListPosts(
        $filter: ModelPostFilterInput
        $limit: Int
        $nextToken: String
      ) {
        listPosts(filter: $filter, limit: $limit, nextToken: $nextToken) {
          items {
            id
            title
            content
            username
            coverImage
            comments {
              nextToken # --> this is what was autogenerated, which was giving me issues!!!
            }
            createdAt
            updatedAt
          }
          nextToken
        }
      }
    `;
    
    
    

    一旦我将nextToke 更改为我在帖子前面向您展示的内容,一切都完美无缺。

    现在,我知道手动更改此文件似乎不对。但是,我还没有找到更好的方法来做到这一点。

    但就目前而言,这就是我能想到的。

    我希望这可以帮助遇到类似问题的人。

    谢谢!

    【讨论】:

      【解决方案2】:

      我已经像您在答案中一样应用了架构,但也使 postId 字段很重要。

      我认为这有助于获得嵌套的 cmets。

      type Post
        @model
        @auth(
          rules: [
            { allow: owner, ownerField: "username" }
            { allow: public, operations: [read] }
            { allow: private, operations: [read] }
          ]
        ) {
        id: ID!
        title: String!
        content: String!
        username: String
          @index(name: "postsByUsername", queryField: "postsByUsername")
        coverImage: String
        comments: [Comment] @hasMany(indexName: "byPost", fields: ["id"]) #check out: https://docs.amplify.aws/cli/graphql/data-modeling/#has-many-relationship
      }
      
      #Comment
      #queries: null - removes any authorization rules for queries, allowing anyone to query for comments
      #ownerField:createdBy - sets the createdBy field as the currently signed in user
      type Comment
        @model
        @auth(
          rules: [
            { allow: owner, ownerField: "createdBy" }
            { allow: public, operations: [read] }
            { allow: private, operations: [read] }
          ]
        ) {
        id: ID!
        message: String
        post: Post @belongsTo(fields: ["postID"])
        postID: ID! @index(name: "byPost")
      }
      
      

      【讨论】:

        猜你喜欢
        • 2019-09-06
        • 1970-01-01
        • 2018-09-06
        • 2018-10-27
        • 1970-01-01
        • 2020-03-05
        • 2020-02-03
        • 2020-12-28
        • 1970-01-01
        相关资源
        最近更新 更多