2022-03-29 02:00:00+00:00

Building a frontend storefront or dashboard that communicates with multiple backend GraphQL services can quickly lead to network bottlenecks. If each page component triggers its own network request, mobile load speeds will suffer. Query Batching combines multiple requests into a single HTTP payload.

By leveraging Next.js Server-Side Rendering (SSR) along with Apollo Client, you can aggregate queries and serve pre-rendered pages instantly.


1. Configuring Apollo Client with Batching

In our Next.js frontend (web-ui-client), we enable query batching by replacing the standard Apollo Link with BatchHttpLink. This automatically debounces and groups multiple GraphQL queries into a single HTTP POST request:

// Apollo Client setup with query batching in TypeScript
import { ApolloClient, InMemoryCache } from "@apollo/client";
import { BatchHttpLink } from "@apollo/client/link/batch-http";

const batchLink = new BatchHttpLink({
  uri: "http://api.the enterprise platform.com/graphql",
  batchInterval: 10, // Group queries sent within 10ms
  batchMax: 5 // Maximum 5 queries per HTTP payload
});

export const client = new ApolloClient({
  link: batchLink,
  cache: new InMemoryCache()
});

2. Server-Side Rendering (SSR) Caching

For content-heavy pages, we call Apollo queries inside Next.js's getServerSideProps. The page is pre-rendered on the server node, injecting the populated Apollo cache as props. The client-side hydrator then loads the page immediately without triggering database sweeps.

Deduplicating SSR GraphQL Queries in Next.js Server Components

Combine request-level memoization with batched HTTP transport so deeply nested React Server Components resolve their data in a single round-trip.


About the Author — Hire Freelance Programmer Aldian Fazrihady

Aldian Fazrihady is a Top-Rated Senior Freelance Programmer, Freelance Full Stack Developer, and AI Application Engineer with 15+ years of production software engineering experience and Google Cloud Professional Cloud Architect & Data Engineer certifications. He specializes in building custom LLM & autonomous AI agents, RAG pipelines, Next.js/React PWAs, Python (Django/FastAPI), Go microservices, and serverless cloud infrastructure on Google Cloud Run and AWS.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD via Polar) | View Live AI Portfolio | Portfolio Directory | Send a Direct Message

Related Engineering Articles

Freelance Programmer Specializations & Category Hubs