Expand description
§GraphQL Code Generator for Rust ORMs
Transform GraphQL schemas into production-ready Rust code for Diesel and Sea-ORM. Automatically generates entities, migrations, and schema definitions from your GraphQL API’s introspection.
§Key Features
- Dual ORM Support: Generate code for both Diesel and Sea-ORM
- Database Agnostic: Support for SQLite, PostgreSQL, and MySQL
- Type Safety: Compile-time guarantees with full GraphQL type mapping
- Migration Ready: Automatic database migration generation
- Introspection Powered: Works with any GraphQL API that supports introspection
- Flexible Configuration: TOML and YAML config support (with feature flag)
§Quick Start
§1. Install the CLI
cargo install graphql-codegen-rust
§2. Initialize your project
graphql-codegen-rust init --url https://api.example.com/graphql
§3. Generate code
graphql-codegen-rust generate
§Library Usage
For programmatic use in your Rust applications:
use graphql_codegen_rust::{CodeGenerator, Config};
use graphql_codegen_rust::cli::{OrmType, DatabaseType};
// Create configuration programmatically
let config = Config {
url: "https://api.example.com/graphql".to_string(),
orm: OrmType::Diesel,
db: DatabaseType::Postgres,
output_dir: "./generated".into(),
headers: std::collections::HashMap::new(),
type_mappings: std::collections::HashMap::new(),
scalar_mappings: std::collections::HashMap::new(),
table_naming: Default::default(),
generate_migrations: true,
generate_entities: true,
};
// Generate code
let generator = CodeGenerator::new(&config.orm);
generator.generate_from_config(&config).await?;
§Configuration
§TOML Configuration (graphql-codegen-rust.toml
)
url = "https://api.example.com/graphql"
orm = "Diesel"
db = "Postgres"
output_dir = "./generated"
[headers]
Authorization = "Bearer your-token-here"
[type_mappings]
# Custom type mappings if needed
§YAML Configuration (codegen.yml
) - Requires yaml-codegen-config
feature
schema:
url: https://api.example.com/graphql
headers:
Authorization: Bearer your-token-here
rust_codegen:
orm: Diesel
db: Postgres
output_dir: ./generated
§Generated Code Structure
generated/
├── src/
│ ├── schema.rs # Diesel schema definitions
│ ├── entities/
│ │ ├── user.rs # Entity structs and implementations
│ │ └── post.rs
│ └── mod.rs # Sea-ORM module definitions
└── migrations/
└── 0001_create_users_table/
├── up.sql
└── down.sql
§Error Handling
The library uses anyhow
for error handling, providing
detailed error messages with context. Common error scenarios:
- Network errors: GraphQL endpoint unreachable or authentication failures
- Schema errors: Invalid GraphQL schema or introspection disabled
- Configuration errors: Missing or invalid configuration files
- Generation errors: Unsupported GraphQL types or ORM constraints
§Feature Flags
yaml-codegen-config
: Enable YAML configuration file support- Default features include TOML support and both Diesel and Sea-ORM generators
§Requirements
- Rust 1.86+
- A GraphQL API that supports introspection
- Appropriate database dependencies based on your ORM choice
Re-exports§
pub use config::Config;
pub use generator::create_generator;
Modules§
Structs§
- Code
Generator - High-level interface for generating Rust ORM code from GraphQL schemas.
Functions§
- generate_
all_ code - generate_
from_ config_ file - Generates ORM code directly from a configuration file path.