AWS放大获取所有者数据不仅是Id

Posted

技术标签:

【中文标题】AWS放大获取所有者数据不仅是Id【英文标题】:AWS amplify Getting the owner data not only the Id 【发布时间】:2022-01-12 01:33:29 【问题描述】:

这样的事情可能吗?这应该如何为这个用例设计?我是否必须添加一个 lambda 函数,以便在创建帖子时将用户 (owner) 添加到帖子中?

谁能帮我完成那个..谢谢!

这是 Post 架构:

type Post
  @model
  @key(name: "byClub", fields: ["clubId"])
  @auth(
    rules: [
       allow: owner, operations: [create, update, delete, read] 
       allow: private, operations: [read] 
    ]
  ) 
  id: ID!
  content: String!
  upVotes: Int!
  downVotes: Int!
  image: String
  clubId: ID!
  comments: [Comment] @connection(keyName: "byPost", fields: ["id"])

当我获取帖子时,这就是我得到的:


    "id": "xxxxxxx",
    "content": "xxxxx!",
    "upVotes": 0,
    "downVotes": 0,
    "image": null,
    "clubId": "xxxxxx",
    "comments": 
        "nextToken": null
    ,
    "createdAt": "2021-12-05T10:46:59.797Z",
    "updatedAt": "2021-12-05T10:46:59.797Z",
    "owner": "moneebalalfi@gmail.com"

我想要这样的东西:


    "id": "xxxxx",
    "content": "xxxxxxx",
    "upVotes": 0,
    "downVotes": 0,
    "image": null,
    "clubId": "xxxxxxxx",
    "comments": 
        "nextToken": null
    ,
    "createdAt": "2021-12-05T10:46:59.797Z",
    "updatedAt": "2021-12-05T10:46:59.797Z",
    "owner": 
       name: "xxxxx",
       email: "xxxx@gmail.com",
       image: "xxxxxx",
       // and so on ...
    


【问题讨论】:

【参考方案1】:

owner字段仅用于检查请求的用户是否是数据的所有者。

所有者授权指定用户是否可以访问或操作对象。为此,每个对象都将获得一个 ownerField 字段(默认情况下,如果未指定 owner 将添加到对象中),该字段存储所有权信息并在解析器执行期间以各种方式进行验证。

from Amplify Docs

为了能够与用户建立连接,您需要创建另一个 GraphQL 类型。

type User
  @model 
  @auth(
    rules: [
       allow: owner, operations: [create, update, delete, read] 
       allow: private, operations: [read] 
    ]
  ) 
  id: ID!
  email: String!
  image: String


type Post
  @model
  @key(name: "byClub", fields: ["clubId"])
  @auth(
    rules: [
       allow: owner, operations: [create, update, delete, read] 
       allow: private, operations: [read] 
    ]
  ) 
  id: ID!
  content: String!
  upVotes: Int!
  downVotes: Int!
  image: String
  clubId: ID!
  owner: ID
  user: User @connection(fields: ["owner"])
  comments: [Comment] @connection(keyName: "byPost", fields: ["id"])

如果没有显示,您可能需要配置 amplify codegen 以增加最大深度。

$ amplify configure codegen 
$ amplify codegen

【讨论】:

以上是关于AWS放大获取所有者数据不仅是Id的主要内容,如果未能解决你的问题,请参考以下文章

AWS Athena 分区获取所有路径

AWS Amplify AppSync 订阅无法正常工作

如何在Python AWS boto API中获取EC2实例ID的容器实例列表

如何通过 id 获取最新的共享 AWS RDS 快照?

AWS Cognito - 如何从令牌对象获取用户组

如何使用 aws java sdk 在我的亚马逊账户下获取所有带有标签的实例