A complete GraphQL API built with Rust, Rocket, Juniper, and SeaORM.
rust-api/
├── Cargo.toml
├── database.sqlite
├── src/
│ ├── main.rs
│ ├── entity/
│ │ ├── mod.rs
│ │ └── hobbit.rs
│ └── graphql/
│ ├── mod.rs
│ └── schema/
│ ├── mod.rs
│ └── hobbit/
│ ├── mod.rs
│ ├── types.rs
│ └── operations.rs
Make sure you have Rust 1.70+ installed. Install SeaORM CLI:
cargo install sea-orm-cliIf you want to use SeaORM migrations:
sea-orm-cli migrate init
sea-orm-cli migrate generate "create_hobbit_table" -d ./migrationThen replace the migration file with the content from the tutorial, or manually create the table:
CREATETABLEIF NOT EXISTS hobbit (
id INTEGERPRIMARY KEY AUTOINCREMENT,
name TEXTNOT NULL,
surname TEXTNOT NULL
);You can either:
Option A: Manual SQL
sqlite3 database.sqliteThen run:
CREATETABLEIF NOT EXISTS hobbit (
id INTEGERPRIMARY KEY AUTOINCREMENT,
name TEXTNOT NULL,
surname TEXTNOT NULL
);Exit with .quit
Option B: Using SeaORM Migration After setting up the migration file, run:
sea-orm-cli migrate upcargo runThe server will start on http://localhost:8000
http://localhost:8000/- Homepage with linkshttp://localhost:8000/graphiql- GraphiQL interface for testinghttp://localhost:8000/playground- GraphQL Playgroundhttp://localhost:8000/graphql- GraphQL endpoint (POST)
query {
hobbits {
idnamesurname
}
}mutation {
createHobbit(input: {
name: "Frodo"surname: "Baggins"
}) {
idnamesurname
}
}mutation {
updateHobbit(
id: 1input: {
name: "Frodo"surname: "Took"
}
) {
idnamesurname
}
}mutation {
deleteHobbit(id: 1)
}- Rocket - Web framework
- Juniper - GraphQL implementation
- SeaORM - Async ORM
- SQLite - Database
- Tokio - Async runtime