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

1.0 KiB

Actix-web

Async-graphql-actix-web provides an implementation of actix_web::FromRequest for Request. This is actually an abstraction around async_graphql::Request and you can call Request::into_inner to convert it into a async_graphql::Request.

WSSubscription is an Actor that supports WebSocket subscriptions.

Request example

When you define your actix_web::App you need to pass in the Schema as data.

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

Subscription example

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

More examples

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