2020-04-15 03:15:30 +00:00
|
|
|
|
# Warp
|
2020-04-17 10:22:24 +00:00
|
|
|
|
|
|
|
|
|
`Async-graphql-warp`提供了两个`Filter`,`graphql`和`graphql_subscription`。
|
|
|
|
|
|
2020-09-11 15:38:18 +00:00
|
|
|
|
`graphql`用于执行`Query`和`Mutation`请求,他总是要求POST方法,输出一个包含`async_graphql::Schema`和`async_graphql::Request的元组`,你可以在之后组合其它Filter,或者直接调用`Schema::execute`执行查询。
|
2020-04-17 10:22:24 +00:00
|
|
|
|
|
|
|
|
|
`graphql_subscription`用于实现基于Web Socket的订阅,它输出`warp::Reply`。
|
|
|
|
|
|
|
|
|
|
## 请求例子
|
|
|
|
|
|
|
|
|
|
```rust
|
2020-09-11 15:38:18 +00:00
|
|
|
|
type MySchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;
|
|
|
|
|
|
2020-04-17 10:22:24 +00:00
|
|
|
|
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
|
2020-09-11 15:38:18 +00:00
|
|
|
|
let filter = async_graphql_warp::graphql(schema).and_then(|(schema, request): (MySchema, async_graphql::Request)| async move {
|
2020-04-17 10:22:24 +00:00
|
|
|
|
// 执行查询
|
2020-09-11 15:38:18 +00:00
|
|
|
|
let resp = schema.execute(request).await;
|
2020-04-17 10:22:24 +00:00
|
|
|
|
|
|
|
|
|
// 返回结果
|
2020-10-14 00:03:51 +00:00
|
|
|
|
Ok::<_, Infallible>(async_graphql_warp::Response::from(resp))
|
2020-04-17 10:22:24 +00:00
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
|
```
|