Crates.io | simple-syrup |
lib.rs | simple-syrup |
version | 0.6.5 |
source | src |
created_at | 2022-02-05 20:13:06.396378 |
updated_at | 2022-05-07 21:33:50.350576 |
description | the fastest way to a graphql endpoint running in rust |
homepage | |
repository | https://github.com/danbruder/simple-syrup/ |
max_upload_size | |
id | 527551 |
size | 6,924 |
The fastest way to get a GraphQL server up and running in Rust
Add to Cargo.toml
:
[dependencies]
simple-syrup = "0.6.2"
use simple_syrup::*;
#[tokio::main]
async fn main() {
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription);
SimpleGraphql::new(schema).run().await
}
struct QueryRoot;
#[Object]
impl QueryRoot {
async fn zero(&self) -> u32 {
0
}
}
Running at http://0.0.0.0:3030
Routes:
/graphql
/playground
With sqlx and a sqlite database:
use simple_syrup::*;
#[tokio::main]
async fn main() {
let db = SimpleSqlite::new("foo.db");
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription);
SimpleGraphql::new(schema).with_sqlite(db).run().await
}
struct QueryRoot;
#[Object]
impl QueryRoot {
async fn two(&self, ctx: &Context<'_>) -> Result<i64> {
let pool = ctx.data::<SqlitePool>()?;
let result: (i64,) = sqlx::query_as("SELECT 1 + 1").fetch_one(&*pool).await?;
Ok(result.0)
}
}
Serving an SPA and assets:
use simple_syrup::*;
#[tokio::main]
async fn main() {
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription);
SimpleGraphql::new(schema)
.with_spa("assets/public", "assets/public/index.html")
.run()
.await
}
struct QueryRoot;
#[Object]
impl QueryRoot {
async fn zero(&self) -> u32 {
0
}
}