如何使不同 DynamoDB 表中的数据关联?

How to make data in different DynamoDB Tables relational?

我目前正在关注 AWS Amplify docs,我正在使用默认的博客 GraphQL 架构来尝试创建关系 Dynamo DB tables。

博客模型 table 出现在 DynamoDB 中,我可以向它们上传信息 我只是不知道如何使它们相关。模型类型为 BlogPostComment 。例如,在将 post 上传到它的 DynamoDB table 之后,我如何才能 link 将上传的评论发送到同一个 post?在我的 Swift 代码中,我尝试这样做但无济于事。

我也不理解 List<Comment>.init() 的语法,它可能不应该存在,但没有给出错误。也许这就是我的问题的原因。

创建 post:

     let blog = Blog(name: "UserBlog", posts: List<Post>.init())
        let posts = Post(title: "Testing out AWS", blog:blog, comments: List<Comment>.init())
        _ = Amplify.API.mutate(request: .create(posts)) { event in
            switch event {
            case .success(let result):
                switch result {
                case .success(let post):
                        print("Successfully created the post: \(post)")
                case .failure(let graphQLError):
                    print("Failed to create graphql \(graphQLError)")
                }
            case .failure(let apiError):
                print("Failed to create a todo", apiError)
           
            }
        }

调试控制台的输出

Successfully created the post: Post(id: "83D71F16-6B0D-453A-A163-AABF484CE527", title: "Testing out AWS", blog: nil, comments: nil)

然后在使用此代码 post 创建评论后

  let blog = Blog(name: "UserBlog", posts: List<Post>.init())
        let posts = Post(title: "Testing out AWS", blog:blog, comments: List<Comment>.init())
        let comments = Comment(content: "It worked", post: posts)
        _ = Amplify.API.mutate(request: .create(comments)) { event in
            switch event {
            case .success(let result):
                switch result {
                case .success(let comment):
                        print("Successfully created the comment: \(comment)")
                case .failure(let graphQLError):
                    print("Failed to create graphql \(graphQLError)")
                }
            case .failure(let apiError):
                print("Failed to create a todo", apiError)
           
            }
        }

调试控制台的输出

Successfully created the comment: Comment(id: "85395F8B-C8C2-4ACB-8FC5-DAEFC2728C32", content: Optional("It worked"), post: nil)

最后在使用此代码从 table 中获取后

        let post = Post.keys
           let predicate = post.title == "Testing out AWS"
           _ = Amplify.API.query(request: .list(Post.self, where: predicate)) { event in
               switch event {
               case .success(let result):
                   switch result {
                   case .success(let post):
                       print("Successfully retrieved list of posts: \(post)")

                   case .failure(let error):
                       print("Got failed result with \(error.errorDescription)")
                   }
               case .failure(let error):
                   print("Got failed event with error \(error)")
               }
           }

调试控制台的输出

Successfully retrieved list of posts: [testAWS.Post(id: "83D71F16-6B0D-453A-A163-AABF484CE527", title: "Testing out AWS", blog: nil, comments: nil)]

如何 link 对 post 的评论,以便当我查询它时,评论将显示而不是 nil

我的架构:

type Blog @model {
  id: ID!
  name: String!
  posts: [Post] @connection(name: "BlogPosts")
}
type Post @model {
  id: ID!
  title: String!
  blog: Blog @connection(name: "BlogPosts")
  comments: [Comment] @connection(name: "PostComments")
}
type Comment @model {
  id: ID!
  content: String
  post: Post @connection(name: "PostComments")
}

如果您还没有保存您的每一个模型,例如,我看到您创建了一个博客,然后 post 与博客一起保存。您需要保存博客,然后保存 post,否则博客在 DynamoDB 中不存在。 Post 和 Comment 看起来是正确的,因为您保存的是 post,然后是评论(包含用于关联的 post)。

数据已保留,但未 return 发送给您的客户端。当前用于创建 GraphQLRequest .list(Post.self, where: predicate) 的构建器只会生成 return 和 Post 的选择集,但不会生成连接评论的可选列表。默认情况下,walk-depth 为 1。目前 Amplify for Android 的做法有所不同,并且在回购协议中有一个现有的 Issue open 来跟踪是否应该将其更新为默认情况下的 walk depth 2 . https://github.com/aws-amplify/amplify-ios/issues/681

这里的一个解决方法是使用包含 post 和评论的选择集创建您自己的自定义 GraphQL 请求。或者针对列表查询的自定义 GraphQL 请求,该请求具有您设置为针对特定 post.id.

过滤的过滤器参数

您可以在此处看到此示例创建了一个嵌套选择集,其中包含 post 和注释:https://github.com/aws-amplify/docs/blob/fbe1773bd21476954f379909b7a9a7abf3f02c2a/docs/lib/graphqlapi/fragments/ios/advanced-workflows.md#nested-data

extension GraphQLRequest {
    static func getPostWithComments(byId id: String) -> GraphQLRequest<JSONValue> {
        let document = """
        query getPost($id: ID!) {
          getPost(id: $id) {
            id
            title
            rating
            status
            comments {
              items {
                id
                postID
                content
              }
            }
          }
        }
        """
        return GraphQLRequest<JSONValue>(document: document,
                                         variables: ["id": id],
                                         responseType: JSONValue.self)
    }
}

请随时 post 您在 github 存储库 https://github.com/aws-amplify/amplify-ios/issues 中提出的问题,因为这将有助于我们促进对话