In my app users have the ability to start a conversation with other users and after the conversation is created they can send messages to one another. However after trying to create a conversation between two users I get an error saying
Failed to create graphql GraphQLResponseError<Conversation>: GraphQL service returned a successful response containing errors: [Amplify.GraphQLError(message: "Not Authorized to access createConversation on type Conversation", locations: Optional([Amplify.GraphQLError.Location(line: 2, column: 3)]), path: Optional([Amplify.JSONValue.string("createConversation")]), extensions: Optional(["data": Amplify.JSONValue.null, "errorType": Amplify.JSONValue.string("Unauthorized"), "errorInfo": Amplify.JSONValue.null]))]
Recovery suggestion: The list of `GraphQLError` contains service-specific messages
This is my first time using GraphQL and it probably shows. I would like to give members of model Conversation the ability to create their convo. Can anyone possibly steer me in the right direction?
The following is my GraphQL Schema
type User
@model
@auth(rules: [{ allow: owner, operations: [create, delete, update]}]) {
id: ID!
userSub: String!
fullName: String!
profileImageFileName: String!
conversations: [ConvoLink] @connection(name: "UserLinks")
messages: [ChatMessage] @connection(name: "UserMessages", keyField: "authorId")
createdAt: String
updatedAt: String
}
type Conversation
@model
@auth(rules: [{ allow: owner, ownerField: "members", operations: [create, delete, update] }]) {
id: ID!
messages: [ChatMessage] @connection(name: "ConvoMsgs", sortField: "createdAt")
associated: [ConvoLink] @connection(name: "AssociatedLinks")
name: String!
members: [String!]!
createdAt: String
updatedAt: String
}
type ChatMessage
@model
@auth(rules: [{ allow: owner, ownerField: "authorId" }]) {
id: ID!
author: User @connection(name: "UserMessages", keyField: "authorId")
authorId: String
content: String!
conversation: Conversation! @connection(name: "ConvoMsgs")
messageConversationId: ID!
createdAt: String
updatedAt: String
}
type ConvoLink
@model(
mutations: { create: "createConvoLink", update: "updateConvoLink" }
queries: null
subscriptions: null
) {
id: ID!
user: User! @connection(name: "UserLinks")
convoLinkUserId: ID
conversation: Conversation! @connection(name: "AssociatedLinks")
convoLinkConversationId: ID!
createdAt: String
updatedAt: String
}
Swift code
func createConvo(){
let conversation = Conversation( messages: List<ChatMessage>.init(), associated: List<ConvoLink>.init(),name: "convo", members: [currentUserSub, recieverUserSub])
_ = Amplify.API.mutate(request: .create(conversation)) { event in
switch event {
case .success(let result):
switch result {
case .success(let convo):
// DispatchQueue.main.async {
print("Successfully created the convo: \(convo)")
// self.messageButton.isHidden = true
// }
case .failure(let graphQLError):
print("Failed to create graphql \(graphQLError)")
// self.checkIfOffline()
}
case .failure(let apiError):
print("Failed to create a todo", apiError)
// self.checkIfOffline()
}
}
}