Multiple operations per request

The GraphQL standard generally assumes there will be one server request for each client operation you want to perform (such as a query or mutation). This is conceptually simple but has the potential to be inefficent.

Some client libraries such as apollo-link-batch-http have added the ability to batch operations in a single HTTP request to save network round-trips and potentially increase performance. There are some tradeoffs that should be considered before batching requests.

Juniper’s server integration crates support multiple operations in a single HTTP request using JSON arrays. This makes them compatible with client libraries that support batch operations without any special configuration.

Server integration crates maintained by others are not required to support batch requests. Batch requests aren’t part of the official GraphQL specification.

Assuming an integration supports batch requests, for the following GraphQL query:

{
  hero {
    name
  }
}

The json data to POST to the server for an individual request would be:

{
  "query": "{hero{name}}"
}

And the response would be of the form:

{
  "data": {
    "hero": {
      "name": "R2-D2"
    }
  }
}

If you wanted to run the same query twice in a single HTTP request, the batched json data to POST to the server would be:

[
  {
    "query": "{hero{name}}"
  },
  {
    "query": "{hero{name}}"
  }
]

And the response would be of the form:

[
  {
    "data": {
      "hero": {
        "name": "R2-D2"
      }
    }
  },
  {
    "data": {
      "hero": {
        "name": "R2-D2"
      }
    }
  }
]