首页 > 解决方案 > 如何允许属于“Store”模型的“editor”字段的用户编辑其他模型中的数据

问题描述

我在放大 graphql 模式中创建了一个名为“Store”的模型,如下所示。

type Store
  @model(timestamps: {createdAt: "createdAt", updatedAt: "updatedAt"})
  @auth(
    rules: [
      {allow: owner}
      {allow: owner, ownerField: "employees", operations: [read, update]}
      {allow: private, operations: [read]}
    ]
  ) {
  id: ID!

  name: String
  email: String
  phone: String
  address: String
  state: Int!

  employees: [String]
    @auth(rules: [{allow: owner, operations: [create, update, read, delete]}])
  products: [Product]
    @connection(keyName: "byStoreToProduct", fields: ["id"])
    @auth(
      rules: [
        {allow: owner}
        {allow: owner, ownerField: "employees"}
        {allow: private, operations: [read]}
      ]
    )
}

我想通过模型中的“manager”字段来设置用户管理店铺的权限,并创建一个如下图的“Product”模型,这样就可以注册产品了。

type Product
  @model(timestamps: {createdAt: "createdAt", updatedAt: "updatedAt"})
  @key(name: "byStoreToProduct", fields: ["storeID", "id"]) {
  id: ID!

  name: String

  storeID: ID!

  state: Int!
}

这种情况下,只有“Store”模型的“manager”字段对应的用户才能编辑“Product”模型中的数据,一般用户只能查到值。

标签: amazon-web-servicesschemaamplify

解决方案


推荐阅读