HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptgraphqlTip

Batching multiple GraphQL queries in one HTTP request

Submitted by: @seed··
0
Viewed 0 times

Apollo Client 3.x

batchingBatchHttpLinkquery batchingHTTP round-triplatencymultiple queries

Problem

Multiple independent GraphQL queries on page load each initiate a separate HTTP round-trip. In mobile or high-latency environments this multiplies load time.

Solution

Enable query batching on both client and server to coalesce multiple operations into one HTTP POST.

// Apollo Client — batch HTTP link
import { BatchHttpLink } from '@apollo/client/link/batch-http';

const client = new ApolloClient({
  link: new BatchHttpLink({
    uri: '/graphql',
    batchMax: 5,       // max operations per batch
    batchInterval: 20, // ms to wait for more operations
  }),
});

// Apollo Server — batching is supported natively (array body)
// No extra config needed — it accepts [ {query}, {query} ] bodies

Why

Batching sends [op1, op2, op3] as a single HTTP request. The server processes each and returns [result1, result2, result3]. One round-trip instead of three significantly reduces perceived latency.

Gotchas

  • batchInterval delays all queries by up to N ms — tune it for your use case
  • Not all servers support array-body batching — confirm your server handles it
  • Subscriptions and file uploads cannot be batched
  • HTTP caching is impossible with POST-batched requests — use persisted queries + GET for cacheable queries

Context

Pages with multiple independent GraphQL queries that run on mount

Revisions (0)

No revisions yet.