GraphQL
GraphQL is a query language for APIs and a runtime for answering those queries. Instead of a server exposing dozens of fixed endpoints (/users/42, /users/42/posts, /posts/9/comments), it exposes one endpoint and a typed schema describing all the data clients can ask for. Each client sends a query naming exactly the fields it wants, and the server returns JSON in exactly that shape. Facebook built it for its mobile apps and open-sourced it in 2015.
The properties that matter:
- Client-specified responses: The client decides which fields come back. A phone's feed screen and a desktop's feed screen can ask for different fields from the same API without any backend change.
- One round trip for related data: A single query can walk relationships (a user, their last 10 posts, each post's top 3 comments and the commenters' avatars) that would take several REST calls.
- Strongly typed schema: Every field has a type, and queries are validated against the schema before they run. The schema doubles as documentation and drives code generation for clients.
- Resolver-based execution: Each field is backed by a resolver function, so one GraphQL API can stitch together data from databases, REST services and gRPC services behind it.
GraphQL shows up in interviews in two ways. In API design questions, you'll be asked to choose between REST, GraphQL and gRPC and justify it. And in product designs with rich clients (a social feed, a marketplace, a dashboard), proposing a GraphQL layer in front of microservices invites follow-ups about the N+1 problem, caching, expensive queries and how many teams can share one schema. We'll cover all of those.
Schema, Queries and Mutations
A GraphQL API is defined by its schema, written in the Schema Definition Language (SDL):
type User {
id: ID!
name: String!
avatarUrl(size: Int = 64): String
posts(first: Int = 10, after: String): PostConnection!
}
type Post {
id: ID!
text: String!
author: User!
likeCount: Int!
comments(first: Int = 3): [Comment!]!
}
type Comment {
id: ID!
text: String!
author: User!
}
type Query {
me: User
user(id: ID!): User
post(id: ID!): Post
}
type Mutation {
createPost(input: CreatePostInput!): Post!
likePost(postId: ID!): Post!
}
type Subscription {
commentAdded(postId: ID!): Comment!
}
! means non-null. Fields can take arguments, like avatarUrl(size:). There are three root operation types: queries for reads, mutations for writes and subscriptions for real-time events.
A client asks for exactly what a screen needs:
query FeedScreen {
me {
name
posts(first: 2) {
edges {
node {
text
likeCount
comments(first: 3) { text author { name avatarUrl(size: 32) } }
}
}
}
}
}
and gets back JSON that mirrors the query:
{ "data": { "me": { "name": "Ana", "posts": { "edges": [
{ "node": { "text": "Hello", "likeCount": 12, "comments": [
{ "text": "Hi!", "author": { "name": "Bo", "avatarUrl": "https://cdn.example.com/bo-32.jpg" } }
] } }
] } } } }
With REST, the same screen might take four calls (user, posts, comments for each post, then the commenters), or one bespoke /feed-screen endpoint that the backend team has to maintain for each client. That's the problem GraphQL was designed to fix: over-fetching (getting fields you don't need) and under-fetching (needing several round trips), which hurt most on mobile networks where each round trip can cost hundreds of milliseconds.