async-graphql/docs/zh-CN/src/integrations_to_actix_web.md
2021-11-16 14:51:20 +08:00

880 B

Actix-web

Async-graphql-actix-web提供了GraphQLRequest提取器用于提取GraphQL请求,和GraphQLResponse用于输出GraphQL响应。

GraphQLSubscription用于创建一个支持Web Socket订阅的Actor。

请求例子

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

async fn index(
    schema: web::Data<Schema>,
    request: GraphQLRequest,
) -> web::Json<GraphQLResponse> {
    web::Json(schema.execute(request.into_inner()).await.into())
}

订阅例子

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

更多例子

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