使用aws放大生成graphql API 时遇到问题
我的模型主要有两个对象(用户和消息(:
用户对象:
type User
@model(subscriptions: null)
@auth(rules: [{ allow: owner, ownerField: "id" }]) {
id: ID!
email: String!
username: String!
avatar: S3Object
name: String
conversations: [Conversation] @manyToMany(relationName: "UserConversations")
messages: [Message!]! @hasMany(indexName: "byAuthor", fields: ["id"])
createdAt: String
updatedAt: String
}
消息对象:
type Message
@model(subscriptions: null)
@auth(
rules: [
{
allow: owner
ownerField: "authorId"
operations: [create, update, delete]
}
]
) {
id: ID!
author: User @belongsTo(fields: ["authorId"])
authorId: ID! @index(name: "byAuthor", sortKeyFields: ["content"])
content: String!
conversation: Conversation! @belongsTo(fields: ["messageConversationId"])
messageConversationId: ID!
@index(name: "byConversation", sortKeyFields: ["createdAt"])
createdAt: String
updatedAt: String
}
两者之间存在hasMany
/belongsTo
关系,两者上都有身份验证规则。
在我登录API并尝试通过JS API创建用户对象之后,我得到了以下错误
'Not Authorized to access messages on type ModelMessageConnection'
await AuthAPI.graphql(
graphqlOperation(createUser, {
input: {
id,
username,
email,
name,
},
})
);
这实际上是由于message
规则缺少read
操作。将对象模型更改为下面的代码修复了
type Message
@model(subscriptions: null)
@auth(
rules: [
{
allow: owner
ownerField: "authorId"
operations: [create, update, delete, read]
}
]
) {
id: ID!
author: User @belongsTo(fields: ["authorId"])
authorId: ID! @index(name: "byAuthor", sortKeyFields: ["content"])
content: String!
conversation: Conversation! @belongsTo(fields: ["messageConversationId"])
messageConversationId: ID!
@index(name: "byConversation", sortKeyFields: ["createdAt"])
createdAt: String
updatedAt: String
}