From f438678fbf183a303bc44fa0087be2139d53f36f Mon Sep 17 00:00:00 2001 From: sunli Date: Fri, 17 Apr 2020 18:22:24 +0800 Subject: [PATCH] Add some docs --- docs/zh-CN/src/integrations | 5 ++++ docs/zh-CN/src/integrations_to_actix_web | 30 ++++++++++++++++++++++++ docs/zh-CN/src/integrations_to_warp | 28 ++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/docs/zh-CN/src/integrations b/docs/zh-CN/src/integrations index 48dc3918..444c33b5 100644 --- a/docs/zh-CN/src/integrations +++ b/docs/zh-CN/src/integrations @@ -1 +1,6 @@ # 集成到WebServer + +`Async-graphql`提供了对一些常用Web Server的集成支持。 + +- Actix-web [async-graphql-actix-web](https://crates.io/crates/async-graphql-actix-web) +- Warp [async-graphql-warp](https://crates.io/crates/async-graphql-warp) diff --git a/docs/zh-CN/src/integrations_to_actix_web b/docs/zh-CN/src/integrations_to_actix_web index efacadad..38c664d5 100644 --- a/docs/zh-CN/src/integrations_to_actix_web +++ b/docs/zh-CN/src/integrations_to_actix_web @@ -1 +1,31 @@ # Actix-web + +`Async-graphql-actix-web`提供`GQLRequest`,它实现了`actix_web::FromRequest`,它其实是QueryBuilder的包装,你可以调用`GQLRequest::into_inner`把它转换成一个`QueryBuilder`。 + +`WSSubscription`是一个支持Web Socket订阅的Actor。 + +## 请求例子 + +你需要把Schema传入`actix_web::App`作为全局数据。 + +```rust +async fn index( + schema: web::Data, + gql_request: GQLRequest, +) -> web::Json { + web::Json(GQLResponse(gql_request.into_inner().execute(&schema).await)) +} + +``` + +## 订阅例子 + +```rust +async fn index_ws( + schema: web::Data, + req: HttpRequest, + payload: web::Payload, +) -> Result { + ws::start_with_protocols(WSSubscription::new(&schema), &["graphql-ws"], &req, payload) +} +``` diff --git a/docs/zh-CN/src/integrations_to_warp b/docs/zh-CN/src/integrations_to_warp index 0f90660e..fa82e397 100644 --- a/docs/zh-CN/src/integrations_to_warp +++ b/docs/zh-CN/src/integrations_to_warp @@ -1 +1,29 @@ # Warp + +`Async-graphql-warp`提供了两个`Filter`,`graphql`和`graphql_subscription`。 + +`graphql`用于执行`Query`和`Mutation`请求,他总是要求POST方法,输出一个包含`Schema`和`QueryBuilder的元组`,你可以在之后组合其它Filter,或者直接调用`QueryBuilder::execute`执行查询。 + +`graphql_subscription`用于实现基于Web Socket的订阅,它输出`warp::Reply`。 + +## 请求例子 + +```rust +let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription); +let filter = async_graphql_warp::graphql(schema).and_then(|(schema, builder): (_, QueryBuilder)| async move { + // 执行查询 + let resp = builder.execute(&schema).await; + + // 返回结果 + Ok::<_, Infallible>(warp::reply::json(&GQLResponse(resp)).into_response()) +}); +warp::serve(filter).run(([0, 0, 0, 0], 8000)).await; +``` + +## 订阅例子 + +```rust +let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot); +let filter = async_graphql_warp::graphql_subscription(schema); +warp::serve(filter).run(([0, 0, 0, 0], 8000)).await; +```