如何在graphQL中编写JOIN或从多种类型获取结果-AWS App sync iOS

时间:2019-01-02 14:38:04

标签: swift graphql aws-appsync aws-appsync-ios

我正在其中一个应用程序中将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
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

听起来您需要对一个或多个数据源的多个请求才能完成此graphQL查询。在这种情况下,您可以使用AppSync的管道解析器功能。

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

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

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

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

相关问题