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

#809 in Database interfaces

Download history 6/week @ 2023-06-14 31/week @ 2023-06-21 44/week @ 2023-06-28 8/week @ 2023-07-05 4/week @ 2023-07-12 9/week @ 2023-07-19 10/week @ 2023-07-26 5/week @ 2023-08-02 9/week @ 2023-08-09 25/week @ 2023-08-16 22/week @ 2023-08-23 4/week @ 2023-08-30 27/week @ 2023-09-06 23/week @ 2023-09-13 6/week @ 2023-09-20

62 downloads per month

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

~51–95MB
~2M SLoC