Content Delivery APIs: GraphQL vs. REST Patterns and Tradeoffs
Evaluate query precision, cacheability, persisted queries, payload sizing, and N+1 mitigation across GraphQL and REST delivery layers.
Query Precision vs. Resource Predictability
REST content delivery APIs expose predictable resource endpoints—such as /entries/{id} or filtered collection queries—that map naturally onto HTTP semantics and CDN caches. However, deeply nested pages often require multiple round-trips or return oversized payloads full of unused fields.
GraphQL lets each client request exact fields across connected types in a single round-trip. Mobile apps can pull compact card summaries while desktop storefronts fetch expanded specs from the exact same endpoint without custom backend-for-frontend endpoints.
- REST shines for flat collections, webhook payloads, and standard HTTP caching
- GraphQL reduces over-fetching across mobile and multi-reference pages
- Typed GraphQL schemas generate TypeScript interfaces automatically for front ends
- Both protocols benefit from strict pagination and query complexity limits
HTTP Caching and Persisted Queries
Traditional GraphQL sends POST requests with dynamic JSON bodies, which bypass standard CDN GET caches by default. Without deliberate architecture, every GraphQL read hits origin compute and incurs higher tail latency.
Automatic Persisted Queries (APQ) or hash-registered GET endpoints solve this gap. Clients send a short SHA-256 query hash and variables as URL query parameters, allowing CDN edge nodes to cache GraphQL responses identically to REST resources.
- Use HTTP GET with persisted query hashes to unlock edge CDN caching for GraphQL
- Reject arbitrary unapproved queries in production to protect origin databases
- Version REST projections or GraphQL fields additively rather than breaking clients
Preventing N+1 Queries and Complexity Abuse
Deeply nested GraphQL queries can accidentally trigger thousands of downstream database lookups—known as the N+1 problem—or allow maliciously recursive payloads. A five-level nested query over 50 items can overwhelm unguarded resolvers.
Production API gateways enforce query depth caps, calculated complexity budgets, and DataLoader request batching. On REST APIs, limit ?include= expansion depth and cap maximum page sizes to prevent oversized serialization spikes.
Choosing the Right Protocol for Each Workload
Engineering teams rarely need an ideological all-or-nothing choice. Many high-performing platforms use GraphQL for composite UI assembly and build-time data fetching, while relying on REST for asset uploads, bulk sync exports, and webhook management.
Evaluate team familiarity, edge caching requirements, and tooling ecosystems before standardizing. Clean contract tests and OpenAPI or GraphQL schema registries matter more to long-term velocity than protocol dogma.
Headless Content Delivery API Comparison
| Criterion | REST Content API | Dynamic GraphQL (POST) | Persisted GraphQL (GET) |
|---|---|---|---|
| Edge CDN Cacheability | Native HTTP GET caching | Poor; bypasses most CDNs | High; standard GET cache keys |
| Over-Fetching Risk | Moderate; fixed shapes | Very low; exact field selection | Very low; pre-registered fields |
| Round-Trips (Nested) | Multiple unless ?include used | Single composite query | Single composite query |
| Abuse Protection | Simple rate + page limits | Requires depth/cost analyzer | Strict allowlist by query hash |
| Client Type Safety | OpenAPI codegen | Native GraphQL codegen | Native GraphQL codegen |
Persisted GraphQL over HTTP GET combines GraphQL field precision with REST-grade CDN caching.
Worked Illustration: Optimizing a Product Catalog Query
Suppose a storefront product detail page fetches product copy, three linked specs, author reviews, and localized labels.
- Measure the baseline REST implementation requiring four sequential calls totaling 140 KB of unused editorial metadata.
- Consolidate the fetch into one GraphQL query selecting only title, summary, image URL, spec rows, and localized strings (18 KB payload).
- Register the query in the persisted-query allowlist and invoke it via HTTP GET /graphql?hash=...&locale=en-US.
- Configure Cache-Control: public, s-maxage=300, stale-while-revalidate=86400 with surrogate tags for each referenced entry ID.
Illustration Outcome: Payload weight drops significantly, client round-trips fall from four to one, and repeat requests serve directly from the CDN edge.
Content API Engineering Checklist
- Use persisted GraphQL queries over HTTP GET for cacheable public content reads.
- Enforce query depth, node count, and pagination caps across all public endpoints.
- Separate Draft/Preview API tokens from published Production Delivery API tokens.
- Implement DataLoader batching or indexed joins to eliminate N+1 resolver queries.
- Generate TypeScript types from OpenAPI or GraphQL SDL during CI builds.
- Emit W3C Trace Context headers to diagnose slow resolvers end to end.
Frequently Asked Questions
Is GraphQL always faster than REST for a headless CMS?
Not automatically. GraphQL reduces network round-trips and payload size, but uncached dynamic POST queries can be slower than edge-cached REST GET responses unless persisted GET queries are enabled.
Should preview environments use the same API endpoint as production?
Keep draft preview traffic isolated on a dedicated non-cached endpoint or token scope so unpublished edits never leak into public CDN caches.
How do you handle binary media assets in GraphQL vs. REST?
Store media metadata in the CMS API and serve actual images or videos from a dedicated transformation CDN URL supporting width, format, and quality parameters.
What is the best pagination pattern for large content archives?
Cursor-based pagination outperforms high-offset pagination on large datasets because it avoids expensive database row scans and duplicate items during live publishes.
Primary Standards & Specifications
- GraphQL Foundation — GraphQL Over HTTP Specification
- OpenAPI Initiative — OpenAPI Specification v3.1.0
- IETF RFC 9111 — HTTP Caching Semantics