GraphQL Proficient¶
🌐 Networking · Level 4
When you'd use this
Schema definition, resolvers, queries, mutations and Strawberry framework.
Expose a flexible query API where clients request exactly the fields they need — reducing over- and under-fetching.
GraphQL with Strawberry (Python-native)¶
Define a schema with Python types and let clients query exactly the fields they need.
import strawberry
from strawberry.fastapi import GraphQLRouter
from fastapi import FastAPI
@strawberry.type
class User:
id: int
name: str
email: str
age: int
@strawberry.type
class Query:
@strawberry.field
def user(self, id: int) -> User | None:
users = {1: User(1, "Alice", "a@b.com", 30), 2: User(2, "Bob", "b@c.com", 25)}
return users.get(id)
@strawberry.field
def users(self) -> list[User]:
return [User(1, "Alice", "a@b.com", 30), User(2, "Bob", "b@c.com", 25)]
@strawberry.type
class Mutation:
@strawberry.mutation
def create_user(self, name: str, email: str, age: int) -> User:
new_user = User(id=3, name=name, email=email, age=age)
return new_user
schema = strawberry.Schema(query=Query, mutation=Mutation)
app = FastAPI()
app.include_router(GraphQLRouter(schema), prefix="/graphql")
Querying¶
Shape responses from the client side — one request, precisely the data you want.
# Get specific fields (no over-fetching!)
query {
user(id: 1) {
name
email
}
}
# Response:
# {"data": {"user": {"name": "Alice", "email": "a@b.com"}}}
# Get multiple resources in one request
query {
user(id: 1) { name }
users { id name }
}
# Mutation
mutation {
createUser(name: "Charlie", email: "c@d.com", age: 35) {
id
name
}
}
REST vs GraphQL¶
When flexible client-driven queries beat fixed REST endpoints (and when they don't).
| Feature | REST | GraphQL |
|---|---|---|
| Endpoints | Multiple (/users, /posts) | Single (/graphql) |
| Data shape | Server decides | Client decides |
| Over-fetching | Common | Impossible |
| Under-fetching | Requires multiple calls | Single query |
| Versioning | URL or header | Evolve schema |
| Best for | Simple CRUD, public APIs | Complex UIs, mobile apps |
Practice Exercises¶
- Build a GraphQL API for a blog (posts, comments, authors) with Strawberry.
- Implement pagination with cursor-based connections.
- Add authentication — only logged-in users can mutate data.
- Implement DataLoader to solve the N+1 problem in resolvers.
- Compare a GraphQL API vs REST API for the same data — measure payload sizes.
💬 Discussion
Have a question about this topic? Found an error? Share your thoughts below.