-
Notifications
You must be signed in to change notification settings - Fork 37
Batching Arrays and Queries
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 a delegation for each record in a list, we'd prefer to perform one delegation total on behalf of the entire list.
Query batching is a high-level strategy for combining all queries performed during an execution cycle into one GraphQL operation sent to a subschema. This will combine both array-batched and single-record queries performed across GraphQL types into one operation that executes all at once.
For example, given the following queries generated during an execution cycle:
query { products(ids: [1, 2, 5]) { name } }
query { seller(id: 7) { name } }
query { seller(id: 8) { name } }
query { buyer(id: 9) { name } }
All of these discrete queries get rewritten into a single operation sent to the subschema:
query {
products_0: products(ids: [1, 2, 5]) { name }
seller_0: seller(id: 7) { name }
seller_1: seller(id: 8) { name }
buyer_id: buyer(id: 9) { name }
}
This aggregate of queries has many advantages: it consolidates network requests sent to remote servers, and apps with synchronous execution have the opportunity to batch behaviors within the single executable operation. GraphQL Tools' batch-execute package automatically interprets batched field aliases and remaps resulting errors.