【发布时间】:2020-11-26 05:36:49
【问题描述】:
在我的应用中,用户可以与其他用户开始对话,并且在创建对话后,他们可以互相发送消息。但是,在尝试创建两个用户之间的对话后,我收到一条错误消息
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
这是我第一次使用 GraphQL,它可能会显示出来。我想给模型Conversation 的members 创建他们的convo 的能力。任何人都可以引导我朝着正确的方向前进吗?
以下是我的 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 代码
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()
}
}
}
【问题讨论】:
标签: swift graphql aws-amplify