What is GraphQL and Why It Exists
You'll learn what GraphQL is and how it solves common API data-fetching problems that REST struggles with.
What is GraphQL?
GraphQL is a query language for APIs developed by Facebook in 2012 (open-sourced in 2015). Unlike REST, where the server defines what data each endpoint returns, GraphQL lets the client specify exactly what data it needs in a single request.
Think of it like ordering at a restaurant: With REST, you order from a fixed menu where each dish comes predetermined (a "user" endpoint always returns name, email, address, preferences, etc.). With GraphQL, you tell the kitchen exactly what you want on your plate—just the name and email, nothing else.
The Problems GraphQL Solves
Over-fetching
REST endpoints often return more data than you need. If you only want a user's name but the /users/123 endpoint returns 20 fields, you've wasted bandwidth and processing time fetching unnecessary data.
Under-fetching
Conversely, you might need data from multiple resources. In REST, this requires multiple round-trips: fetch the user, then fetch their posts, then fetch comments on those posts. This creates network latency and complexity.
GraphQL solves both by letting you request exactly the fields you need from multiple related resources in one query.
How It Works (Conceptually)
The client sends a query describing the shape of data it wants. The server responds with JSON matching that exact structure—no more, no less. You get precisely what you asked for in a single request, regardless of how many underlying resources are involved.
Key Takeaway: GraphQL is a query language that lets clients request exactly the data they need, solving REST's over-fetching (too much data) and under-fetching (too many requests) problems with flexible, client-driven queries.