Swift 如何使用AWS&;上传与不同型号相关的信息;GraphQL?
我有一个我一直在开发的应用程序,允许用户上传帖子,其他用户可以在看到他们的帖子后发送消息。我使用AWS Amplify作为后端,并尝试将用户消息存储在DynamoDB表中。我能够成功地将用户信息存储在DynamoDB表中,在他们创建帐户并成功上传帖子之后。当我试图向另一个用户发送有关他们帖子的消息时,我的问题就开始了 按sendSwift 如何使用AWS&;上传与不同型号相关的信息;GraphQL?,swift,graphql,aws-amplify,Swift,Graphql,Aws Amplify,我有一个我一直在开发的应用程序,允许用户上传帖子,其他用户可以在看到他们的帖子后发送消息。我使用AWS Amplify作为后端,并尝试将用户消息存储在DynamoDB表中。我能够成功地将用户信息存储在DynamoDB表中,在他们创建帐户并成功上传帖子之后。当我试图向另一个用户发送有关他们帖子的消息时,我的问题就开始了 按send创建graphql GraphQLResponseError失败后,我出现此错误。graphql服务返回了一个成功的响应,其中包含错误:[放大.GraphQLError(
创建graphql GraphQLResponseError失败后,我出现此错误。graphql服务返回了一个成功的响应,其中包含错误:[放大.GraphQLError(消息:“变量输入包含未为输入对象类型“CreateChatMessageInput\”定义的字段名“chatMessageAuthorId\”,位置:nil,路径:nil,扩展名:nil)]恢复建议:“GraphQLError”列表包含特定于服务的消息
我的GraphQL架构如下所示:
type User
@model
@auth(rules: [{ allow: owner, operations: [create, delete, update]}]) {
id: ID!
userSub: String!
fullName: 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" }]) {
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
}
type Post @model
@auth(rules: [{ allow: owner, operations: [create, delete, update] }]) {
id: ID!
userSub: String!
filename: String!
description: String!
dateUploaded: String!
}
这是我发送信息给另一个用户的Swift代码
@objc func sendMessage(){
currentUserSub = AWSMobileClient.default().userSub!
guard let message = messageBox.text else{return}
let formatter = DateFormatter()
formatter.dateFormat = "h:mm a"
let hourString = formatter.string(from: Date())
let user = User(userSub: currentUserSub, fullName: userFullName)
let conversation = Conversation(messages: List<ChatMessage>.init(), associated: List<ConvoLink>.init(), name: "Chat between \(currentUserSub) & \(otherUserSub)", members: [currentUserSub,otherUserSub], createdAt: hourString, updatedAt: hourString)
let chatMessage = ChatMessage(author: user, authorId: currentUserSub, content: message, conversation: conversation, messageConversationId: "\(currentUserSub) & \(otherUserSub)", createdAt: hourString, updatedAt: hourString)
_ = Amplify.API.mutate(request: .create(chatMessage)) { 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.setTitle("Message Sent", for: .normal)
self.messageButton.isEnabled = false
}
case .failure(let graphQLError):
DispatchQueue.main.async {
print("Failed to create graphql \(graphQLError)")
self.messageButton.setTitle("An error has occured, try again.", for: .normal)
// self.checkIfOffline()
}
}
case .failure(let apiError):
print("Failed to create a message", apiError)
// self.checkIfOffline()
}
}
hideKeyboard()
print(message)
}
@objc func sendMessage(){
currentUserSub=AWSMobileClient.default().userSub!
guard let message=messageBox.text else{return}
let formatter=DateFormatter()
formatter.dateFormat=“h:mm a”
让hourString=formatter.string(from:Date())
let user=user(userSub:currentUserSub,fullName:userFullName)
let conversation=conversation(消息:List.init(),关联:List.init(),名称:“在\(currentUserSub)和\(otherUserSub)之间聊天”,成员:[currentUserSub,otherUserSub],createdAt:hourString,updatedAt:hourString)
让chatMessage=chatMessage(作者:用户,作者:currentUserSub,内容:消息,对话:对话,messageConversationId:“\(currentUserSub)和\(otherUserSub)”,创建日期:小时字符串,更新日期:小时字符串)
_=放大.API.mutate(请求:.create(chatMessage)){event in
切换事件{
案例。成功(让结果):
切换结果{
成功案例(让车队):
DispatchQueue.main.async{
打印(“已成功创建车队:\(车队)”)
self.messageButton.setTitle(“已发送消息”,用于:。正常)
self.messageButton.isEnabled=false
}
案例失败(let graphQLError):
DispatchQueue.main.async{
打印(“未能创建graphql\(graphQLError)”)
self.messageButton.setTitle(“发生错误,请重试。”,对于:。正常)
//self.checkIfOffline()
}
}
案例.失败(错误):
打印(“创建消息失败”,apiError)
//self.checkIfOffline()
}
}
隐藏板()
打印(信息)
}
谁能告诉我我做错了什么?我是GraphQL和AWS的新手。您能分享一下您使用的Amplify iOS和Amplify CLI的版本吗?对于CLI版本,了解您在创建和推送后端时使用的版本非常重要。@DanielRochetti My Amplify CLI版本为4.24.3,并且在我的podfile.lock文件中显示My Amplify iOS版本为(1.0.4)。您可以共享您使用的Amplify iOS和Amplify CLI的版本吗?对于CLI版本,了解您在创建和推送后端时使用的版本很重要。@DanielRochetti My Amplify CLI版本是4.24.3,而我的podfile.lock文件中说我的Amplify iOS版本是(1.0.4)