Axum GraphQL Implementation
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The Axum GraphQL implementation provides a flexible interface for querying and mutating data within the SeaORM Pro ecosystem. It integrates async-graphql with the axum web framework, utilizing seaography to bridge SeaORM entities with GraphQL schemas. This implementation supports both a web-based GraphQL Playground for interactive development and a robust backend handler for processing client requests.
The system is designed to handle complex data relationships while maintaining security through JWT-based authentication. It allows for fine-grained control over query execution, including limits on query depth and complexity to prevent resource exhaustion.
Sources: examples/axum_example/api/src/lib.rs:250-281, src/controllers/graphql.rs:36-54
Architecture and Components
The GraphQL implementation follows a modular structure where the schema generation, request handling, and playground serving are separated into distinct components.
Core Components Table
| Component | Description | Primary File(s) |
|---|---|---|
| Query Root | Responsible for dynamic schema construction and entity registration. | src/graphql/query_root.rs |
| GraphQL Handler | Axum route handler that executes GraphQL requests against the schema. | src/controllers/graphql.rs |
| Playground | Serves the interactive GraphQL IDE with local storage token support. | src/controllers/graphql.rs |
| Initializer | Sets up the GraphQL schema during application startup. | src/initializers/graphql.rs |
Sources: src/initializers/graphql.rs, src/controllers/graphql.rs:56-64, src/graphql/query_root.rs
Request Flow Diagram
The following diagram illustrates the flow of a GraphQL request through the Axum server.
Sources: src/controllers/graphql.rs:36-64
Schema Construction
The GraphQL schema is built dynamically using seaography::Builder. This process maps SeaORM entities directly to GraphQL objects, allowing for automated CRUD capabilities.
Registration and Limits
The schema construction involves registering specific entity modules and setting safety constraints. In the Axum example, the post entity is explicitly registered.
// examples/axum_example/api/src/graphql/query_root.rs:7-7
seaography::register_entity_modules!([post]);
// src/initializers/graphql.rs:7-10
const DEPTH: Option<usize> = Some(100);
const COMPLEXITY: Option<usize> = Some(1000);
Sources: examples/axum_example/api/src/graphql/query_root.rs:7, src/initializers/graphql.rs:7-10
Extension Handling
The system supports schema extensions, such as the Readonly extension, which can intercept queries to enforce read-only constraints on specific deployments (e.g., demo sites).
Sources: src/graphql/query_root.rs:53-75
Authentication and Security
Security is enforced at the controller level. Every POST request to the GraphQL endpoint undergoes JWT verification.
- JWT Extraction: The
auth::JWTextractor validates the token. - User Verification: The user is looked up in the database using the PID from the JWT claims.
- User Context: A
seaography::UserContextis injected into the GraphQL request data, allowing resolvers to access theuser_idof the requester.
Sources: src/controllers/graphql.rs:36-54, examples/axum_example/api/src/lib.rs:265-281
API Endpoints
The implementation exposes two primary routes within the graphql prefix.
| Method | Path | Function | Description |
|---|---|---|---|
GET | / | graphql_playground | Serves the HTML for the GraphQL Playground. |
POST | / | graphql_handler | Processes GraphQL queries and mutations. |
Sources: src/controllers/graphql.rs:56-64, examples/axum_example/api/src/lib.rs:250-281
Conclusion
The Axum GraphQL implementation in SeaORM Pro provides a highly automated yet configurable bridge between the database and the API layer. By leveraging seaography for schema generation and Axum for robust routing, it enables developers to rapidly deploy secure, high-performance GraphQL APIs. The inclusion of safety limits and custom extensions ensures that the implementation remains scalable and adaptable to various deployment requirements.