GraphQL Fundamentals and Query Language
What You'll Learn
You'll learn how GraphQL's declarative query approach allows clients to request precisely the data they need, avoiding both over-fetching and under-fetching.
Core Idea
Unlike REST where endpoints return fixed data structures, GraphQL lets clients specify exactly what they want in each request. Instead of calling multiple endpoints or receiving more data than needed, you write a query that looks like the shape of the response you want.
The Type System
GraphQL starts with a strongly-typed schema that defines what data is available:
type User {
id: ID!
name: String!
email: String
posts: [Post!]!
}
type Post {
id: ID!
title: String!
content: String
author: User!
}
This schema becomes a contract between client and server, making data discovery and validation automatic.
Declarative Queries
Clients write queries that mirror the response structure they need:
query {
user(id: "123") {
name
email
posts {
title
}
}
}
This returns only the requested fields—no more, no less. Need different fields for mobile vs web? Write different queries against the same endpoint.
The Analogy
Think of REST like ordering from a set menu (you get the combo meal as-is), while GraphQL is like a build-your-own meal where you check exactly which items you want and the kitchen assembles only those.
Benefits in System Design
- Single endpoint instead of multiple versioned REST paths
- Reduced bandwidth by eliminating over-fetching
- Fewer round-trips by aggregating related data in one query
- Self-documenting through introspection of the type system
Key Takeaway: GraphQL's declarative query language and type system shift control to the client, enabling precise data requests that reduce bandwidth and round-trips while maintaining a single, evolvable API endpoint.