-
Notifications
You must be signed in to change notification settings - Fork 37
Batching Arrays and Queries
Greg MacWilliam edited this page Nov 26, 2020
·
18 revisions
It's quite common for stitching to fetch supporting data for each record in an array. Given [1, 2, 5]
as an array of record IDs, we could query a subschema for each of those records individually:
# product(id: Int!): Product
query { product(id: 1) { name } }
query { product(id: 2) { name } }
query { product(id: 5) { name } }
However, this is pretty inefficient (yes, this is absolutely an N+1 query). Each query must be delegated, resolved, and call the database individually. This would be significantly more efficient to resolve all of these records at once using an array service:
# products(ids: [Int!]!): [Product]!
query { products(ids: [1, 2, 5]) { name } }
That, in a nutshell, is array batching. Rather than performing single delegations on each of a list of records, we'd prefer to perform one delegation on behalf of the entire list that resolves everything at once.
tktk