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

标签 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
                    }
                }
            }
        }
    }
}

最佳答案

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

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

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

  1. 一个函数将查询聊天组数据库
  2. 第二个函数将使用聊天组的结果来获取消息
  3. 将所有结果合并到一个包含群组信息和消息的 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/

相关文章:

reactjs - AWS AppSync + React-Apollo Query/useQuery 引发异常 this.currentObservable.query.getCurrentResult 不是函数

swift - 使用 swift 4.0 编译的模块无法导入到 swift 3.1 中 — Carthage

ios - swift 2 错误 : Editor placeholder in sourcefile

javascript - Apollo 订阅 PubSub 未触发监听器

graphql - 使用 apollo graphql 客户端记录请求时间的正确方法是什么?

java - GraphQL Spring-boot 查询过滤

amazon-web-services - Appsync Resolver UpdateItem 忽略空参数?

swift - 使用 Xcode 在 Swift 中将默认选项设置为 OSX 而不是 IOS 的 Pod Init?

json - 尝试从结构中获取 "variable"但当我尝试时它说结构没有成员 "variable"

amazon-web-services - 如何在 AppSync 解析器中运行多个 SQL 语句?