【问题标题】:AppSync batch call for nested properties嵌套属性的 AppSync 批处理调用
【发布时间】:2019-12-21 14:43:43
【问题描述】:

我的后端 REST API 采用 id 的列表并返回一个列表,例如,Person 对象的列表,其 id 被请求。每个Person 都有一个children 属性,它是Person.id 的列表。不错的家谱。

// GET /id/[1,2]
{
  "id": 1,
  "name": "Jacob",
  "children": [3, 4]
},
{
  "id": 2,
  "name": "Jack",
  "children": [5, 6]
}

使用 AppSync 来前置所述 API,我有一个 children 的解析器,它使用以下模板调用我的 API:

#set( $myMap = {
  "id" : $context.source.children
} )

{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload": {
    "id": $util.toJson($myMap)
  }
}

这很好用,AppSync 很好地用适当的Person“解包”children 数组。问题是,如果 PersonN 孩子,那么如果客户端请求孩子,则会对我的后端 API 进行 N 调用。

由于我的 API 可以一次获取所有 N 子级的所有子级 ID,如果 AppSync 能够批量处理这些调用,然后以某种方式解开响应并将每个 Person 放入正确的地方。

问题

有没有办法将嵌套属性的所有单独调用批处理到每个嵌套级别的单个调用中?

编辑

上面示例的我的 GraphQL 架构:

type Person {
  id: Int
  name: String
  children: [Person]
}

【问题讨论】:

    标签: rest graphql aws-appsync


    【解决方案1】:

    AppSync 在通过 AWS Lambda 代理时在一定程度上支持这种类型的批处理行为。请参阅https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html 并搜索“批处理”。

    话虽如此,根据您提供的内容,您能否仅从 Person.children 字段调用您的批处理 API?

    你指定你有一个对象:

    type Person {
      id: Int
      name: String
      childIds: [String]
      children: [Person]
    }
    

    您能否使用 $ctx.source 对象提供的子列表添加调用批处理 HTTP 端点的字段 Person.children

    type Person {
      id: Int
      name: String
      childIds: [String]
    
      # An HTTP resolver that performs a GET to /id/[1,2]
      # where [1,2] comes from $ctx.source.childIds
      children: [Person]
    }
    

    这将对每个人的 HTTP 端点执行一次批量调用。

    【讨论】:

      【解决方案2】:

      AppSync 在通过 AWS Lambda 代理时支持这种类型的批处理行为。请参阅https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html 并搜索“批处理”。

      话虽如此,根据您提供的内容,您能否仅从 Person.children 字段调用您的批处理 API?

      你指定你有一个对象:

      type Person {
        id: Int
        name: String
        childIds: [String]
        children: [Person]
      }
      

      您不能只添加字段Person.children 来调用您的批处理HTTP 端点和子列表吗?

      type Person {
        id: Int
        name: String
        childIds: [String]
      
        # An HTTP resolver that performs a GET to /id/[1,2]
        # where [1,2] comes from $ctx.source.childIds
        children: [Person]
      }
      

      这将对每个人的 HTTP 端点执行一次批量调用。

      【讨论】:

        猜你喜欢
        • 2019-02-03
        • 1970-01-01
        • 2022-08-16
        • 2014-09-17
        • 2011-05-19
        • 1970-01-01
        • 2016-04-22
        • 1970-01-01
        • 2023-01-25
        相关资源
        最近更新 更多