Loading pattern...

What is GraphQL?

GraphQL is a query language for APIs where clients specify exactly what data they need. REST: GET /users/1 returns everything (id, name, email, address, etc.). GraphQL: client says "give me id and name only." Reduces over-fetching (getting too much) and under-fetching (making multiple requests). One endpoint, flexible queries. Created by Facebook, used by GitHub, Shopify. More complex than REST, best for complex frontend needs.

When Should You Use This?

Use GraphQL when you have complex data relationships, mobile apps that need minimal data (save bandwidth), multiple clients with different needs (web wants everything, mobile wants less), or when you want to avoid versioning APIs. Don't use for simple CRUD—REST is simpler. Don't use if backend complexity isn't worth frontend flexibility. Start with REST, add GraphQL when pain points emerge.

Common Mistakes to Avoid

  • Using for simple APIs—GraphQL adds complexity, REST is fine for CRUD
  • No query limits—malicious queries can request entire database
  • N+1 query problem—naive implementation makes 100 DB queries instead of 1
  • Caching is harder—HTTP caching works with REST URLs, GraphQL needs custom caching
  • Over-engineering—GraphQL overkill for small teams/simple apps

Real-World Examples

  • GitHub API—GraphQL for flexible repo/issue queries
  • Shopify—GraphQL for storefront data, merchants query what they need
  • Netflix—Uses GraphQL internally for complex data aggregation
  • Airbnb—GraphQL for mobile apps to minimize data transfer

Category

System Design Patterns

Tags

graphqlapi-designquery-languagerest-alternativeapollo

Permalink