AI summary by TwinMind
Six Years Later, I’m Over GraphQL
Theo - t3․gg
34m 40s
Personal GraphQL Stance Clarification
- Speaker clarifies doesn't hate GraphQL - thinks it's great solution but not right for everybody
- Believes if backend/frontend both TypeScript in same repo, should use tRPC or similar type-sharing solutions
- Ability to move fast no longer reserved to GraphQL
- GraphQL makes sense for different backend/frontend teams using different technologies
- Gave keynote at GraphQL Conf last year explaining when GraphQL is appropriate
Twitch Experience with GraphQL
- At Twitch: backend moving to Go, frontend to React/TypeScript
- GraphQL was "glue that held it all together"
- Enabled backend/frontend teams to work independently
- Before GraphQL: stuck on backend because nothing else could work if backend didn't
- GraphQL schema layer allowed teams to define contract and work separately
- Core distinction: team/company structure determines when GraphQL makes sense
Team Structure Decision Matrix (Tweet Reference)
- Same team, both TypeScript: use tRPC
- Backend not TypeScript, frontend is: GraphQL solid solution (or REST)
- Different teams, backend not TypeScript: GraphQL makes sense
- Different company/external APIs: use REST
- GraphQL for public APIs often "reinventing REST but worse"
- GitHub's GraphQL API cited as "miserable experience"
Article Analysis: "Why After 6 Years I'm Over GraphQL"
- Author previously championed GraphQL since 2018
- Initially loved GraphQL after dealing with "untyped JSON REST APIs"
- Type safety was major attraction
- Perspective changed when deploying to environments where security, performance, maintainability mattered more
Security Attack Surface Issues
Authorization Problems
- Most widely understood GraphQL risk
- Every field must be authorized against current user appropriately
- Context matters - same field may have different authorization requirements
- Example: Twitter API with user handle, email, blocked users list
- GraphQL potentially responsible for "Broken Access Control" climbing to OWASP top 10 #1 spot (was #5)
- Recent Facebook/Instagram breach: 200+ million records extracted via GraphQL API
Rate Limiting Challenges
- Cannot assume all requests equally hard on server
- No limit to query size
- Introspection types are cyclical - can craft queries returning megabytes of JSON
- Example: 128-byte query causing 10-second CPU usage, 500 response
Twitch Outage Case Study: Badges Field
- Moderator view page showing list of moderators with badges
- Badges field seemed innocent but queried payment service for subscription badges
- Page had minimal traffic (1 in 10,000 sessions) with queries every 1-2 minutes
- Still almost took down payments for all of Twitch
- Problem: badges team didn't cache values, made it frontend team's problem
- Demonstrates hidden complexity in seemingly simple fields
Rate Limiting Mitigation Complexity
- Common solution: estimate complexity for every field in schema
- Abandon queries exceeding maximum complexity value
- Use credit bucket system that resets at intervals
- Problem: calculations delicate to get right
- Schema cycles compound estimation errors exponentially
- Example: 5 tags assumption becomes 5^5 = 3,125 complexity
- 10 tags creates 100,000 complexity (20x greater than estimated)
- GraphQL Ruby default max depth: 13, example only depth 7
- REST equivalent: simple bucketed rate limiter (200 requests/minute)
tRPC Rate Limiting Demo
- Showed adding Upstash rate limiter to tRPC endpoint
- Simple implementation:
await rateLimit.limit(request.ip) - Easy configuration: once per 10 seconds, 20 times per 30 minutes
- Rate limiting applied before expensive code runs
- Each endpoint can have different rate limiting rules
Query vs Endpoint Philosophy Difference
- GraphQL: define queries on client, server parses and validates
- tRPC: define queries on server, client calls specific endpoints
- tRPC inversion: if need different data, create new server endpoint
- Easy to copy-paste existing endpoint and modify
- Each endpoint trivial to validate individually
Query Parsing Vulnerabilities
- Pen test found crafted invalid query strings can out-of-memory server
- Syntactically valid but schema-invalid queries
- Server parses and builds error responses with thousands of errors
- Memory amplification: 2,000x more memory than query string
- Not enough to limit payload size - valid queries can be larger than dangerous ones
- Need maximum error limits or custom solutions
- No REST equivalent attack of this severity
Performance Issues
HTTP Caching Incompatibility
- GraphQL uses POST requests (need body for query)
- Can't cache POST requests easily
- tRPC uses GET with URL encoding - easy to cache
- SuperJSON transformation handles dates automatically
N+1 Problem
- Field resolver hits external data source nested in list of n items
- Results in n database/API calls instead of 1
- Data loader pattern common solution
- Problem: clients can trigger N+1 by modifying queries without backend changes
- Defensive data loader abstraction needed everywhere "just in case"
- REST solution: hoist queries to controller level, fetch 25 posts including author/tags in one call
Authorization N+1 Problem
- Authorization framework integration creates new N+1 category
- Example: birthday field authorized if "friends with user"
- Must check user.friends for every friend's birthday
- Each authorization check fires additional database query
- Trickier than data N+1 because authorization runs in multiple contexts
- Biggest source of performance issues in author's experience
- Queries spending more time authorizing than fetching data
- REST solution: authenticate at endpoint level, implicit permission checking
Coupling and Testing Problems
- Business logic forced into transport layer
- Authorization rules peppered throughout GraphQL types
- Data fetching logic moved into GraphQL-specific data loaders
- Must test exclusively at integration layer (running GraphQL queries)
- Errors captured by framework - difficult debugging
- Breakpoints often not in application code
- Stack traces and JSON GraphQL error responses
Additional Complexity Issues
- GraphQL discourages breaking changes, provides no tools for them
- Versioning miserable for those controlling all clients
- HTTP response codes: always 200 even for errors
- Can't cache errors (but can't cache anyway due to POST)
- Fetching all data in one query often worse for response time in HTTP/2+ era
- Requires server parallelization vs separate parallel requests
Recommended Alternatives
When to Use What
- Control clients + <3 clients + statically typed language + >1 language: OpenAPI 3.0 compliant JSON REST API
- Tooling improved significantly since GraphQL emergence
- Many options for generating typed client code
- Framework-specific data fetching libraries available
- Experience close to best parts of GraphQL without Facebook-scale complexity
Implementation Approaches
- Generate OpenAPI spec from type file
- Spec-first: define schema ahead of time
- Language-specific solutions like tRPC for TypeScript-only environments
Author's Current Position
- Still not anti-GraphQL despite perception
- If can use tRPC, will use it
- If can't use tRPC, GraphQL is next choice
- GraphQL benefits not worth complexity unless really needed
- Acknowledges GraphQL fanboys now acknowledging flaws
- Maintains middle ground position
