Add some docs
This commit is contained in:
parent
4cf83e333b
commit
f0a3af15ae
|
@ -1 +1,6 @@
|
||||||
# 集成到WebServer
|
# 集成到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)
|
||||||
|
|
|
@ -1 +1,31 @@
|
||||||
# Actix-web
|
# 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<Schema>,
|
||||||
|
gql_request: GQLRequest,
|
||||||
|
) -> web::Json<GQLResponse> {
|
||||||
|
web::Json(GQLResponse(gql_request.into_inner().execute(&schema).await))
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## 订阅例子
|
||||||
|
|
||||||
|
```rust
|
||||||
|
async fn index_ws(
|
||||||
|
schema: web::Data<Schema>,
|
||||||
|
req: HttpRequest,
|
||||||
|
payload: web::Payload,
|
||||||
|
) -> Result<HttpResponse> {
|
||||||
|
ws::start_with_protocols(WSSubscription::new(&schema), &["graphql-ws"], &req, payload)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
|
@ -1 +1,29 @@
|
||||||
# Warp
|
# 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;
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in New Issue
Block a user