想象一下,我具有以下具有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
的更新权限?
[如果您使用的是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"])
}