Batching
The GraphQL standard generally assumes that there will be one server request per each client operation to perform (such as a query or mutation). This is conceptually simple but potentially inefficient.
Some client libraries (such as apollo-link-batch-http
) have the ability to batch operations in a single HTTP request to save network round-trips and potentially increase performance. There are some tradeoffs, though, that should be considered before batching operations.
Juniper's server integration crates support batching multiple operations in a single HTTP request out-of-the-box via JSON arrays. This makes them compatible with client libraries that support batch operations without any special configuration.
NOTE: If you use a custom server integration, it's not a hard requirement to support batching, as it's not a part of the official GraphQL specification.
Assuming an integration supports operations batching, for the following GraphQL query:
{
hero {
name
}
}
The JSON data
to POST for an individual request would be:
{
"query": "{hero{name}}"
}
And the response would be in the form:
{
"data": {
"hero": {
"name": "R2-D2"
}
}
}
However, if we want to run the same query twice in a single HTTP request, the batched JSON data
to POST would be:
[
{
"query": "{hero{name}}"
},
{
"query": "{hero{name}}"
}
]
And then, the response would be in the following array form:
[
{
"data": {
"hero": {
"name": "R2-D2"
}
}
},
{
"data": {
"hero": {
"name": "R2-D2"
}
}
}
]