在AppSync @auth规则中,如何将ownerField设置为数组中的属性?

问题描述 投票:1回答:1

想象一下,我具有以下具有User类型的AppSync GraphQL模式,以及具有Post字段设置为editors s数组的User类型:

type User
  @model 
  @auth(rules: [
    { allow: owner }
  ])
{
  id: ID!
  owner: String!
  username: String!
}

type Post
  @model
  @auth(rules: [
    { allow: owner },
    # Can I do this?
    # { allow: owner, ownerField: "editors.owner", operations: [update] }
  ])
{
  id: ID!
  owner: String!
  title: String!
  content: String
  editors: [User]
}

如何创建@auth规则以授予对User数组中的editors的更新权限?

graphql aws-appsync
1个回答
0
投票

[如果您使用的是Amazon Cognito用户池,则应在Post中将编辑器类型设置为String数组,并将值设置为要访问的用户的Cognito ID。对此进行了解释in the amplify cli documentation

要使编辑器的类型为User,我建议您创建另一个名称不同的参数(例如editorUsers),然后按照here的说明将其连接到User模型。>

您的架构应如下所示:

type User
@model
@key(name: "byUser", fields: ["username"]) 
@auth(rules: [
 { allow: owner }
])
{
  id: ID!
  owner: String!
  username: String!
}

type Post
@model
@auth(rules: [
  { allow: owner },
  { allow: owner, ownerField: "editors", operations: [update] }
])
{
  id: ID!
  owner: String!
  title: String!
  content: String
  editors: [String]
  editorsUsers: [User] @connection(keyName: "byUser", fields: ["id"])
}
    
© www.soinside.com 2019 - 2024. All rights reserved.