AppSync batch call for nested properties

半腔热情 提交于 2021-01-29 02:48:27

问题


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

回答1:


AppSync supports this type of batching behavior 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 not just add the field Person.children that calls your batch HTTP endpoint with the list of 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]
}

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




回答2:


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.



来源:https://stackoverflow.com/questions/57504905/appsync-batch-call-for-nested-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!