嵌套属性的AppSync批处理调用 [英] AppSync batch call for nested properties

查看:63
本文介绍了嵌套属性的AppSync批处理调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的后端REST API接受一个 id 的列表,并返回一个列表,例如,一个请求了 id Person 对象.每个 Person 都有一个 children 属性,该属性是 Person.id 的列表.一棵漂亮的家谱.

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

使用AppSync在前面提到的API之前,我有一个用于 children 的解析器,该解析器使用以下模板来调用我的API:

  #set($ myMap = {"id":$ context.source.children}){"version":"2017-02-28","operation":"Invoke",有效载荷":{"id":$ util.toJson($ myMap)}} 

这很好用,AppSync可以使用适当的 Person 很好地解包" children 数组.问题是,如果 Person N 个子代,则如果客户端请求这些子代,则会对我的后端API进行 N 个调用.

由于我的API可以一次获取所有 N 个子代的所有子代ID,所以如果AppSync有一种方法可以批量处理这些调用,然后以某种方式解开响应并放入每个子代,那就太好了人员在正确的位置.

问题

是否有一种方法可以将嵌套属性的所有单个调用分批到每个嵌套级别的单个调用中?

编辑

上面示例的GraphQL模式:

 类型Person {id:整数名称:字符串儿童:[人]} 

解决方案

在通过AWS Lambda进行代理时,AppSync在某种程度上支持这种批处理行为.参见 https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html 并搜索批量".

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

您指定了一个对象:

 类型Person {id:整数名称:字符串childIds:[String]儿童:[人]} 

您是否可以添加$ code.Person.children 字段,该字段使用$ ctx.source对象提供的子级列表来调用批处理HTTP终结点?

 类型Person {id:整数名称:字符串childIds:[String]#一个对/id/[1,2]执行GET的HTTP解析器#其中[1,2]来自$ ctx.source.childIds儿童:[人]} 

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

My backend REST API takes a list of id's and returns a list of, say, Person objects whose id was requested. Each Person has a children property, which is a list of Person.id. A nice family tree.

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

Using AppSync to front said API, I have a resolver for children that makes a call to my API using the following template:

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

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

This works fine, and AppSync nicely "unwraps" the children array with the appropriate Person. The issue is that if a Person has N children, then N calls are made to my backend API if the client requests the children.

Since my API could take all of the children IDs for all N children at once, it would be nice if AppSync had a way to batch those calls, and then somehow untangle the response and put each Person in the right place.

Question

Is there a way to batch all of individual calls for a nested property into a single call per nested level?

Edit

My GraphQL schema for the example above:

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

解决方案

AppSync supports this type of batching behavior to an extent when proxied through AWS Lambda. See https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html and search for "Batching".

With that being said, based on what you have provided, can you not just call your batch API from the Person.children field for example?

You specified that you have an object:

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

Can you add the field Person.children that calls your batch HTTP endpoint with the list of children provided by the $ctx.source object?

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]
}

This will perform a single batch call to your HTTP endpoint per person.

这篇关于嵌套属性的AppSync批处理调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆