【问题标题】:AWS amplify Getting the owner data not only the IdAWS放大获取所有者数据不仅是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 ...
    }

}

【问题讨论】:

    标签: reactjs amazon-web-services next.js amazon-cognito aws-amplify


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 2018-05-08
      • 2021-04-18
      • 1970-01-01
      相关资源
      最近更新 更多