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,
},
})
);
This is actually due to the message
rule that was missing the read
action. Changing the object model to the code below fixed it
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
}