问题
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