17 releases (5 breaking)

0.6.5 May 7, 2022
0.6.4 May 7, 2022
0.6.2 Feb 12, 2022
0.5.2 Feb 5, 2022
0.1.1 Feb 5, 2022

#829 in HTTP server

MIT/Apache

7KB
127 lines

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
    }
}

Dependencies

~72MB
~1.5M SLoC