Swift 放大图形QL错误未授权访问创建
在“我的应用”中,用户可以与其他用户开始对话,并且在创建对话后,他们可以相互发送消息。然而,在尝试在两个用户之间创建对话后,我得到一个错误,即Swift 放大图形QL错误未授权访问创建,swift,graphql,aws-amplify,Swift,Graphql,Aws Amplify,在“我的应用”中,用户可以与其他用户开始对话,并且在创建对话后,他们可以相互发送消息。然而,在尝试在两个用户之间创建对话后,我得到一个错误,即 Failed to create graphql GraphQLResponseError<Conversation>: GraphQL service returned a successful response containing errors: [Amplify.GraphQLError(message: "Not Aut
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
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()
}
}
}
func createconva(){
let conversation=conversation(消息:List.init(),关联:List.init(),名称:“conva”,成员:[currentUserSub,receiveUserSub])
_=放大.API.mutate(请求:.create(对话)){中的事件
切换事件{
案例。成功(让结果):
切换结果{
成功案例(让车队):
//DispatchQueue.main.async{
打印(“已成功创建车队:\(车队)”)
//self.messageButton.ishiden=true
// }
案例失败(let graphQLError):
打印(“未能创建graphql\(graphQLError)”)
//self.checkIfOffline()
}
案例.失败(错误):
打印(“创建todo失败”,apiError)
//self.checkIfOffline()
}
}
}
在模型类型上使用auth指令时,即@auth(规则:[{allow:owner,operations:[create,delete,update]})
要求经过身份验证的用户调用API
用户是否在Amplify.API.mutate/query/subscribe
之前使用Amplify.Auth.sign
登录
调试
由于您使用的是auth指令,设置API也会设置auth类别。您可以通过amplify console auth
打开已设置的auth资源并选择Cognito User Pool。您可以通过控制台创建用户,并且将在需要新密码的状态下创建该用户
然后,您可以运行放大控制台api
,选择GraphQL,打开AppSync控制台,然后导航到Queries选项卡以测试api。测试api时,查询控制台应指示api的主要授权模式,并要求您登录。从Cognito Use获取web app客户端Idr Pool console,包含您创建的上一个用户的用户名和密码。尝试登录后,它将提示您输入新密码。通过查询控制台对用户进行身份验证后,您可以测试API调用
应用程序内
如果您能够通过将
Amplify.API.signUp/confirmSignUp/signIn
流合并到您的应用程序中来通过非授权错误,但仍然看到API调用的其他问题,请在Github repo中提供更多细节:在修补了r有一段时间,我删除了类型对话
的身份验证规则ownerField
,错误得到了解决。
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()
}
}
}