simple-syrup

Crates.iosimple-syrup
lib.rssimple-syrup
version0.6.5
sourcesrc
created_at2022-02-05 20:13:06.396378
updated_at2022-05-07 21:33:50.350576
descriptionthe fastest way to a graphql endpoint running in rust
homepage
repositoryhttps://github.com/danbruder/simple-syrup/
max_upload_size
id527551
size6,924
Dan Bruder (danbruder)

documentation

https://docs.rs/simple-syrup

README

Simple Syrup

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
    }
}
Commit count: 25

cargo fmt