A Rust library for creating GraphQL APIs with PostgreSQL, inspired by PostGraphile.
- async-graphql: High-performance GraphQL server library
- tokio-postgres: Async PostgreSQL client
- Tokio: Async runtime for Rust
- Rust (latest stable version)
- PostgreSQL database
- Clone this repository or use it as a library
- Copy
.env.example
to.env
and configure your database connection:cp .env.example .env
- Update the
DATABASE_URL
in.env
with your PostgreSQL connection details
# Build the project
cargo build
# Run the example
cargo run
# Run tests
cargo test
Add this to your Cargo.toml
:
[dependencies]
postgraphile-rust = "0.1.0"
Example usage:
use postgraphile_rust::{create_db_client, create_schema};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create database client
let db_client = create_db_client("postgresql://localhost/mydb").await?;
// Create GraphQL schema
let schema = create_schema().data(db_client);
// Execute a query
let query = r#"query { hello }"#;
let request = async_graphql::Request::new(query);
let response = schema.execute(request).await;
println!("{}", serde_json::to_string_pretty(&response)?);
Ok(())
}
src/lib.rs
- Main library code with GraphQL schema and database helperssrc/main.rs
- Example binary demonstrating usageCargo.toml
- Project dependencies and metadata
- tokio-postgres (0.7) - PostgreSQL async client
- async-graphql (7.0) - GraphQL server implementation
- tokio (1.0) - Async runtime with full features
- serde (1.0) - Serialization framework
- serde_json (1.0) - JSON serialization
- uuid (1.0) - UUID generation and parsing
This project is set up as both a library and binary. You can:
- Use it as a library in other projects
- Extend the example in
src/main.rs
- Add more GraphQL resolvers in
src/lib.rs
- Add database models and queries
- Add more GraphQL resolvers
- Implement database models
- Add authentication and authorization
- Set up a web server (e.g., with warp or axum)
- Add database migrations
- Implement subscriptions