Advanced Backend
GraphQL
Schema design, resolvers, queries, and mutations.
Query Language for APIs
GraphQL lets clients request exactly the data they need, solving over-fetching and under-fetching problems common with REST.
Core Concepts
- Schema: Type definitions describing your data
- Queries: Read operations
- Mutations: Write operations
- Resolvers: Functions that fetch data for each field
- Subscriptions: Real-time updates via WebSocket
# Schema Definition
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
}
type Query {
user(id: ID!): User
users: [User!]!
}
# Query Example
query {
user(id: "1") {
name
posts {
title
}
}
}Benefits
- Single endpoint for all data needs
- Strongly typed schema
- Built-in documentation (introspection)
- Efficient data loading with DataLoader
Considerations
- N+1 query problem (solved by DataLoader)
- Caching is more complex than REST
- Query complexity and depth limiting