19个版本

0.9.0 2024年5月31日
0.8.1 2023年11月6日
0.8.0 2023年10月17日
0.7.3 2023年3月28日

#316 in 网络编程

Download history 51/week @ 2024-05-03 25/week @ 2024-05-10 177/week @ 2024-05-17 85/week @ 2024-05-24 316/week @ 2024-05-31 137/week @ 2024-06-07 61/week @ 2024-06-14 83/week @ 2024-06-21 129/week @ 2024-06-28 82/week @ 2024-07-05 39/week @ 2024-07-12 79/week @ 2024-07-19 169/week @ 2024-07-26 164/week @ 2024-08-02 91/week @ 2024-08-09 113/week @ 2024-08-16

每月 541 次下载
2 个包 中使用

MIT 许可证

66KB
2K SLoC

动态graphql

Build Status Latest Version Rust Documentation GitHub license

async-graphql 提供可扩展和动态的GraphQL模式定义

用法

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" } })
    );
}

依赖关系

~14–26MB
~468K SLoC