使用 aws-amplify 更新 GraphQL 对象时出现授权错误
我在使用AWS Amplify生成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
/>两者之间两者之间的关系。
在我签名到API并尝试通过JS API创建用户对象后,我会收到以下错误
'未被授权在类型ModelMessageConnection上访问消息'
await AuthAPI.graphql(
graphqlOperation(createUser, {
input: {
id,
username,
email,
name,
},
})
);
I'm experiencing issues when using aws amplify to generate a graphql API
My model has mainly two objects (User and messages):
User object:
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
}
Message object:
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
}
There's a hasMany
/belongsTo
relationship between the two and auth rules on both.
After I signin to the API and try to create a user object through the JS API, i'm getting the following error
'Not Authorized to access messages on type ModelMessageConnection'
await AuthAPI.graphql(
graphqlOperation(createUser, {
input: {
id,
username,
email,
name,
},
})
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上是由于缺少
read
操作的消息
规则所致。将对象模型更改为下面的代码修复This is actually due to the
message
rule that was missing theread
action. Changing the object model to the code below fixed it