Introduction
Strawberry is a modern Python GraphQL library built on type annotations and dataclasses. Instead of writing schema definitions in a separate DSL, you define your GraphQL types directly in Python using decorators and standard typing, getting full IDE autocompletion and type checking for free.
What Strawberry Does
- Defines GraphQL schemas using Python type annotations and dataclasses
- Generates schema SDL automatically from decorated Python classes
- Integrates with Django, FastAPI, Flask, and ASGI frameworks
- Supports subscriptions, federation, and file uploads
- Provides a built-in GraphiQL IDE for development
Architecture Overview
Strawberry uses Python decorators to transform regular classes into GraphQL types at import time. A schema compiler inspects type annotations to build the GraphQL type system, resolvers, and validation rules. The execution layer delegates to graphql-core for query parsing and validation while adding async support and DataLoader integration.
Self-Hosting & Configuration
- Install via pip:
pip install strawberry-graphql[fastapi] - Add framework integration (Django, FastAPI, Flask, Starlette)
- Configure via schema instantiation options (extensions, directives)
- Enable debug mode with built-in GraphiQL at /graphql
- Deploy as a standard ASGI/WSGI application
Key Features
- Code-first schema definition with full IDE support
- Native async/await support for resolvers
- Built-in DataLoader pattern for N+1 query prevention
- Apollo Federation support for distributed graphs
- Pydantic integration for input validation
Comparison with Similar Tools
- Graphene — Older Python GraphQL library; more verbose class-based API
- Ariadne — Schema-first approach using SDL strings; Strawberry is code-first
- tartiflette — Async-first with SDL; less community adoption
- Apollo Server (JS) — Node.js equivalent; Strawberry brings similar DX to Python
FAQ
Q: Can I use Strawberry with Django? A: Yes. Install strawberry-graphql-django for model integration, permission handling, and pagination support.
Q: Does Strawberry support file uploads? A: Yes. It implements the GraphQL multipart request specification for file upload mutations.
Q: How does Strawberry handle authentication? A: Via context injection. Your framework middleware sets auth info on the request context, which resolvers access through the info parameter.
Q: Is Strawberry compatible with Apollo Federation? A: Yes. Decorate types with @strawberry.federation.type and configure entity resolvers to participate in a federated graph.