我在我的一个应用程序中使用 AWS AppSync 作为聊天应用程序。我们能够成功进行设置和基本查询。
在一种情况下,我需要编写一个自定义的 GraphQL 查询,以便我可以使用一种类型的另一种类型的引用来获取其他数据。例如,我可以拥有来自用户的 allMessageGroup
以及来自特定组的 allMessages
。
现在我想添加组中的最后一条消息及其发件人以及所有消息组的列表,就像应用程序主页一样。
但我无法理解如何进行 JOIN 或编写此类查询,这些查询会根据对话/消息/用户类型/表给出混合结果。
平台:iOS 语言:Swift
下面是我正在使用的架构和 API/查询的详细信息
架构
type Conversation {
conversation_cover_pic: String
conversation_type: String!
createdAt: String
id: ID!
messages(after: String, first: Int): MessageConnection
name: String!
privacy: String
}
type Message {
author: User
content: String!
conversationId: ID!
createdAt: String
id: ID!
recipient: User
sender: String
}
type MessageConnection {
messages: [Message]
nextToken: String
}
查询
query getUserConversationConnectionThroughUser($after: String, $first: Int)
{
me
{
id
__typename
conversations(first: $first, after: $after)
{
__typename
nextToken
userConversations
{
__typename
userId
conversationId
associated
{
__typename
userId
}
conversation
{
__typename
id
name
privacy
messages
{
__typename
id
conversationId
content
createdAt
sender
isSent
}
}
}
}
}
}
最佳答案
听起来您需要向一个或多个数据源发出多个请求才能完成此 graphQL 查询。在这种情况下,您可以使用 AppSync 的管道解析器功能。
使用管道解析器,您可以创建多个函数,每个函数都可以使用前一个函数的结果并查询数据库。这些函数按照您指定的顺序运行。
可以使用管道解析器执行的操作示例:
- 一个函数将查询聊天组数据库
- 第二个函数将使用聊天组的结果来获取消息
- 将所有结果合并到一个包含群组信息和消息的 graphQL 响应中
以下是管道解析器的文档: https://docs.aws.amazon.com/appsync/latest/devguide/pipeline-resolvers.html
关于swift - 如何在 graphQL 中编写 JOIN 或从多种类型获取结果 - AWS Appsync iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54008245/