async-graphql/docs/zh-CN/src/integrations_to_actix_web.md
2020-04-17 19:38:43 +08:00

797 B
Raw Blame History

Actix-web

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

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

请求例子

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

async fn index(
    schema: web::Data<Schema>,
    gql_request: GQLRequest,
) -> web::Json<GQLResponse> {
    web::Json(GQLResponse(gql_request.into_inner().execute(&schema).await))
}

订阅例子

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)
}