Use Meros to handle GraphQL @defer and @stream directives for incremental data delivery
Meros was designed to be the transport layer for GraphQL’s @defer and @stream directives, enabling incremental data delivery for improved perceived performance.
GraphQL’s @defer and @stream directives allow servers to send query results incrementally using multipart responses. This reduces time-to-first-byte and improves user experience by showing critical data immediately while loading less important data progressively.
Meros handles the multipart transport protocol so you can focus on your GraphQL implementation.
import { meros } from 'meros';const parts = await meros(response);for await (const part of parts) { if (part.json) { // Each part contains a GraphQL response const { data, path, errors } = part.body; // Update your store/cache with the new data updateCache(path, data); }}
GraphQL multipart responses follow a specific format with data, path, and optional errors fields.
3
Optimize with multiple mode
Use multiple: true to process all available parts in a chunk synchronously:
const chunks = await meros(response, { multiple: true });for await (const parts of chunks) { // Process multiple parts at once for (const part of parts) { const { data, path } = part.body; updateCache(path, data); } // Commit all updates to your store in one batch commitUpdates();}
This prevents multiple re-renders by batching updates instead of processing parts one at a time.