BFF data aggregation
What you need to know
A Backend for Frontend (BFF) is an API layer shaped around a particular interface. It combines necessary data from services, converts it to a smaller contract and keeps service-specific details away from the consumer.
By the end, you can: compose downstream results; limit returned fields.
Project two mock service results
type CatalogRow = { id: string; title: string; supplierCost: number };type StockRow = { id: string; available: number };export function card(book: CatalogRow, stock: StockRow) { if (book.id !== stock.id) throw new Error('Mismatched resource'); return { id: book.id, title: book.title, available: stock.available };}Expected behavior (not framework-tested): Joining matching rows yields only the three public fields; a mismatched stock row fails instead of attaching unrelated data.
How it works
River Books needs a card containing a title and an availability count. A future price field would need its own validated public pricing source; supplier cost is never a substitute. If the catalog and stock live in independent systems, a BFF can perform their reads concurrently and join results by a stable ID. A DTO (data transfer object) explicitly selects what crosses the boundary. Do not expose whole upstream records containing supplier costs, internal notes or user data. Define how missing stock is handled; zero stock and unknown stock are different states. Validate both upstream records before joining: an ID must match the requested resource and availability must be a nonnegative safe integer. A mismatched or malformed mandatory record is an upstream failure, not an empty success.
For independent operations, Promise.all waits for both successes and rejects if either fails. This does not automatically cancel the other promise. Choose a mandatory-versus-optional policy; when stock is required for a purchase decision, failure should not silently look like zero. A Server Component in the same Next.js application should generally call the data layer directly instead of making an HTTP request to its own BFF. Route Handlers are useful for browser clients, integrations and explicit HTTP contracts.
Compose independent reads
// Partial helper: dependencies are injected for tests.export async function readCard<T, S>( getBook: () => Promise<T>, getStock: () => Promise<S>, combine: (book: T, stock: S) => unknown) { const [book, stock] = await Promise.all([getBook(), getStock()]); return combine(book, stock);}Expected behavior (not framework-tested): Both reads start before the await; if either rejects, the mandatory aggregate rejects. Cancellation and deadlines need separate handling.
Common mistake
Using { ...book, ...stock } as an API response exposes every upstream field, including future fields added by another team. Project public fields explicitly, and validate upstream data as carefully as incoming requests.
Practice and verification
Design a card DTO with title and availability but no supplier cost. Predict output for matching IDs, mismatched IDs, missing stock and upstream failure. Explain which operations can run together and how an optional recommendation panel would differ from required stock.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.
Further reading: Bff · Data Security