18 unstable releases (7 breaking)

0.8.1 Nov 6, 2023
0.7.3 Mar 28, 2023

#22 in #schema-definition

Download history 66/week @ 2024-01-04 142/week @ 2024-01-11 118/week @ 2024-01-18 64/week @ 2024-01-25 28/week @ 2024-02-01 46/week @ 2024-02-08 290/week @ 2024-02-15 331/week @ 2024-02-22 95/week @ 2024-02-29 77/week @ 2024-03-07 118/week @ 2024-03-14 29/week @ 2024-03-21 20/week @ 2024-03-28 38/week @ 2024-04-04 100/week @ 2024-04-11 42/week @ 2024-04-18

201 downloads per month
Used in 3 crates (via dynamic-graphql)

MIT license

210KB
6K SLoC

Dynamic graphql

Build Status Latest Version Rust Documentation GitHub license

extendable and dynamic graphql schema definition for async-graphql

Usage

use dynamic_graphql::App;

mod foo {
    use dynamic_graphql::{App, ExpandObject, ExpandObjectFields, SimpleObject};

    use super::root::Query;

    #[derive(SimpleObject)]
    pub struct Foo {
        id: String,
        name: String,
    }

    #[derive(ExpandObject)]
    pub struct FooQuery<'a>(&'a Query);

    #[ExpandObjectFields]
    impl FooQuery<'_> {
        async fn foo(id: String) -> Foo {
            Foo {
                id,
                name: "test".to_string(),
            }
        }
    }

    #[derive(App)]
    pub struct FooApp(FooQuery<'static>);
}

mod root {
    use dynamic_graphql::SimpleObject;

    #[derive(SimpleObject)]
    #[graphql(root)]
    pub struct Query;
}

#[derive(App)]
struct App(root::Query, foo::FooApp);

#[tokio::test]
async fn test() {
    let schema = App::create_schema().finish().unwrap();

    let sdl = schema.sdl();

    assert_eq!(
        &sdl,
        r#"

type Foo {
	id: String!
	name: String!
}



type Query {
	foo(id: String!): Foo!
}


schema {
	query: Query
}
"#
    );

    let query = r#"
        query {
            foo(id: "1") {
                id
                name
            }
        }
    "#;

    let res = schema.execute(query).await;
    let data = res.data.into_json().unwrap();

    assert_eq!(
        data,
        serde_json::json!({ "foo": { "id": "1", "name": "test" } })
    );
}

Dependencies

~3.5MB
~67K SLoC