async-graphql/docs/zh-CN/src/integrations_to_warp.md
Edward Rudd 3b7ed74d11 correct doc examples so they compile
- examples to fix still
  - error_extensions.md ResultExt example does not compile!
     - trait ErrorExtensions is not implemented for ParseIntError
  - dataloader
     - requires sqlx to work. So we either "stub" it OR we rewrite them simpler to use a  simple "faux" db library
2022-06-02 17:32:12 -04:00

66 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Warp
`Async-graphql-warp`提供了两个`Filter``graphql`和`graphql_subscription`。
`graphql`用于执行`Query`和`Mutation`请求它提取GraphQL请求然后输出一个包含`async_graphql::Schema`和`async_graphql::Request`元组你可以在之后组合其它Filter或者直接调用`Schema::execute`执行查询。
`graphql_subscription`用于实现基于Web Socket的订阅它输出`warp::Reply`。
## 请求例子
```rust
# extern crate async_graphql_warp;
# extern crate async_graphql;
# extern crate warp;
# use async_graphql::*;
# use std::convert::Infallible;
# use warp::Filter;
# struct QueryRoot;
# #[Object]
# impl QueryRoot { async fn version(&self) -> &str { "1.0" } }
# async fn other() {
type MySchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let filter = async_graphql_warp::graphql(schema).and_then(|(schema, request): (MySchema, async_graphql::Request)| async move {
// 执行查询
let resp = schema.execute(request).await;
// 返回结果
Ok::<_, Infallible>(async_graphql_warp::GraphQLResponse::from(resp))
});
warp::serve(filter).run(([0, 0, 0, 0], 8000)).await;
# }
```
## 订阅例子
```rust
# extern crate async_graphql_warp;
# extern crate async_graphql;
# extern crate warp;
# use async_graphql::*;
# use futures_util::stream::{Stream, StreamExt};
# use std::convert::Infallible;
# use warp::Filter;
# struct SubscriptionRoot;
# #[Subscription]
# impl SubscriptionRoot {
# async fn tick(&self) -> impl Stream<Item = i32> {
# futures_util::stream::iter(0..10)
# }
# }
# struct QueryRoot;
# #[Object]
# impl QueryRoot { async fn version(&self) -> &str { "1.0" } }
# async fn other() {
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;
# }
```
## 更多例子
[https://github.com/async-graphql/examples/tree/master/warp](https://github.com/async-graphql/examples/tree/master/warp)