async-graphql/docs/zh-CN/src/integrations_to_actix_web.md
2020-12-15 10:05:44 +08:00

943 B

Actix-web

Async-graphql-actix-web提供实现了actix_web::FromRequestRequest,它其实是async_graphql::Request的包装,你可以调用Request::into_inner把它转换成一个async_graphql::Request

WSSubscription是一个支持Web Socket订阅的Actor。

请求例子

你需要把Schema传入actix_web::App作为全局数据。

async fn index(
    schema: web::Data<Schema>,
    request: async_graphql_actix_web::Request,
) -> web::Json<Response> {
    web::Json(Response(schema.execute(request.into_inner()).await)
}

订阅例子

async fn index_ws(
    schema: web::Data<Schema>,
    req: HttpRequest,
    payload: web::Payload,
) -> Result<HttpResponse> {
    WSSubscription::start(Schema::clone(&*schema), &req, payload)
}

更多例子

https://github.com/async-graphql/examples/tree/master/actix-web