patterntypescriptgraphqlModerate
@defer and @stream directives — send partial query results incrementally
Viewed 0 times
Apollo Server 4.5+, Apollo Client 3.7+
@defer@streamincremental deliverypartial resultsstreamingmultipartperformance
Error Messages
Problem
A query that includes both fast fields and a slow expensive field blocks the entire response until all resolvers complete. Users see nothing until the slowest resolver finishes.
Solution
Use
@defer on slow fragments and @stream on lists to receive partial results as they resolve.query GetDashboard {
user {
id
name
# Fast — delivered immediately
recentOrders(limit: 5) { id total }
# Slow analytics — deferred
... on User @defer {
analyticsData {
totalRevenue
conversionRate
}
}
}
# Stream large list as items resolve
allProducts @stream(initialCount: 3) {
id
name
}
}// Apollo Client — handle incremental results
const { data, loading } = useQuery(DASHBOARD_QUERY);
// data builds up incrementally as deferred fragments arrive
// React re-renders automatically as new data arrivesWhy
With
@defer, the server sends the initial result immediately, then pushes deferred fragments as multipart HTTP chunks. Users see fast data instantly and slow data fills in without a loading spinner for the whole page.Gotchas
- @defer and @stream require HTTP multipart streaming — not all proxies, CDNs, or serverless platforms support streaming responses
- Apollo Server 4.5+ supports @defer natively; older versions need the experimental flag
- Apollo Client 3.7+ handles incremental delivery — older clients receive the full response after all fragments resolve
- @stream is less widely supported than @defer — check your server and client versions
Context
Queries with a mix of fast and slow fields where the slow fields should not block initial render
Revisions (0)
No revisions yet.