async-graphql/docs/zh-CN/src/integrations_to_actix_web.md

36 lines
943 B
Markdown
Raw Normal View History

2020-04-15 03:15:30 +00:00
# Actix-web
2020-04-17 10:22:24 +00:00
2020-09-20 05:44:20 +00:00
`Async-graphql-actix-web`提供实现了`actix_web::FromRequest`的`Request`,它其实是`async_graphql::Request`的包装,你可以调用`Request::into_inner`把它转换成一个`async_graphql::Request`。
2020-04-17 10:22:24 +00:00
`WSSubscription`是一个支持Web Socket订阅的Actor。
## 请求例子
你需要把Schema传入`actix_web::App`作为全局数据。
```rust
async fn index(
schema: web::Data<Schema>,
2020-10-14 00:03:51 +00:00
request: async_graphql_actix_web::Request,
2020-09-20 05:44:20 +00:00
) -> web::Json<Response> {
web::Json(Response(schema.execute(request.into_inner()).await)
2020-04-17 10:22:24 +00:00
}
```
## 订阅例子
```rust
async fn index_ws(
schema: web::Data<Schema>,
req: HttpRequest,
payload: web::Payload,
) -> Result<HttpResponse> {
2020-12-15 02:05:44 +00:00
WSSubscription::start(Schema::clone(&*schema), &req, payload)
2020-04-17 10:22:24 +00:00
}
```
2020-10-14 01:10:06 +00:00
## 更多例子
2020-10-14 01:17:00 +00:00
[https://github.com/async-graphql/examples/tree/master/actix-web](https://github.com/async-graphql/examples/tree/master/actix-web)