如何在 graphQL 中编写 JOIN 或从多种类型获取结果 - AWS 应用程序同步 iOS



我正在将 AWS AppSync 用于我的一个应用程序中的聊天应用程序。 我们能够成功地进行设置和基本查询。

在一种情况下,我需要编写一个自定义的 GraphQL 查询,以便我可以使用一种类型的引用来获得其他数据。例如,我可以从用户那里获得allMessageGroup,也可以从特定组中获得allMessages

现在,我想添加组中的最后一条消息及其发件人以及所有消息组的列表,就像应用程序主页一样。

但是我无法理解如何进行JOIN或编写此类查询,这些查询根据对话/消息/用户类型/表给出混合结果。

平台:苹果语言: 斯威夫特

有关详细信息,以下是我正在使用的架构和 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 的管道解析程序功能。

使用

管道解析程序,可以创建多个函数,每个函数都可以使用前一个函数的结果并查询数据库。这些函数按您指定的顺序运行。

可以使用管道解析器执行的操作示例:

  1. 一个函数将查询聊天组数据库
  2. 第二个函数将使用聊天组的结果来获取消息
  3. 将所有结果合并到一个包含组信息和消息的 graphQL 响应中

以下是管道解析程序的文档:https://docs.aws.amazon.com/appsync/latest/devguide/pipeline-resolvers.html

最新更新