Frontend

GraphQL with React: From REST Migration to Production Patterns

9 min read By Suraj Kumar Gorai

After migrating two enterprise applications from REST to GraphQL, I've seen both the transformative benefits and the hidden costs. Here's a practitioner's guide to implementing GraphQL with React — including the patterns that took us months to discover.

Why We Migrated from REST

Our enterprise dashboard had 47 REST endpoints. The frontend was making 8-12 API calls per page to assemble the data it needed. Problems:

  • Over-fetching: The user list endpoint returned 30 fields per user when the sidebar only needed name and avatar
  • Under-fetching: Loading a project page required sequential calls: project → members → tasks → comments
  • API versioning pain: Every frontend feature change required coordinating with the backend team for new endpoints

GraphQL solved all three: the frontend queries exactly what it needs, in a single request, without backend changes.

Apollo Client Architecture

We chose Apollo Client for its mature ecosystem and caching capabilities. Key architecture decisions:

Normalized Cache

Apollo's normalized cache stores data by type and ID. Query a user in the sidebar and the same user's data is available in the main content area — no duplicate requests. When a mutation updates a user, every component displaying that user re-renders with fresh data automatically.

Query Colocation

Each component defines its own GraphQL fragment describing exactly what data it needs. Parent components compose these fragments into full queries. Benefits: components are self-documenting (you see exactly what data they use), and adding/removing a field is a one-component change.

Code Generation

GraphQL Code Generator creates TypeScript types from your schema and queries automatically. Every query, mutation, and fragment gets fully typed hooks. This eliminated an entire category of bugs — if the schema changes, TypeScript immediately shows every component that needs updating.

Patterns That Work

  • Pagination with fetchMore: Use cursor-based pagination for infinite scroll. Apollo's fetchMore with a merge function in field policies handles appending pages cleanly.
  • Optimistic UI: Define optimisticResponse in mutations so the UI updates instantly before the server confirms. Users never wait for network round-trips on actions like liking, favoriting, or status changes.
  • Error boundaries per query: Not every failed query should crash the entire page. Use query-level error handling to show fallback UI for failed sections while the rest of the page works normally.
  • Polling for real-time: For dashboards that need periodic freshness, Apollo's pollInterval is simpler and more reliable than WebSocket subscriptions for most use cases.

Common Pitfalls

  • N+1 on the backend: GraphQL makes it easy for the frontend to request deeply nested data that causes N+1 database queries on the server. Use DataLoader to batch and cache database calls.
  • Query complexity: A malicious or careless query can request the entire database graph. Implement query depth limiting and complexity analysis server-side.
  • Cache invalidation: Apollo's automatic cache updates work for single-entity mutations but not for list operations (add/remove items). Use refetchQueries or manual cache updates with cache.modify.
  • Bundle size: Apollo Client adds ~35KB gzipped. For simple apps, consider lighter alternatives like urql (~7KB) or even plain fetch with a type-generated client.

Results After Migration

  • 60% fewer network requests per page
  • 40% less data transferred (no over-fetching)
  • 3x faster feature development (frontend team no longer blocked on backend API changes)
  • Zero API versioning — the schema evolves with deprecations, not breaking version bumps

Considering GraphQL for your React application? Let's evaluate whether the benefits outweigh the migration cost for your specific case.