async-graphql/docs/en/src/integrations_to_actix_web.md

50 lines
1.5 KiB
Markdown
Raw Normal View History

# Actix-web
2020-05-09 21:56:45 +00:00
## Request example
When you define your `actix_web::App` you need to pass in the Schema as data.
```rust
# extern crate async_graphql_actix_web;
# extern crate async_graphql;
# extern crate actix_web;
# use async_graphql::*;
# #[derive(Default,SimpleObject)]
# struct Query { a: i32 }
# let schema = Schema::build(Query::default(), EmptyMutation, EmptySubscription).finish();
use actix_web::{web, HttpRequest, HttpResponse};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse};
2020-05-09 21:56:45 +00:00
async fn index(
// Schema now accessible here
schema: web::Data<Schema<Query, EmptyMutation, EmptySubscription>>,
2021-11-16 06:51:20 +00:00
request: GraphQLRequest,
) -> web::Json<GraphQLResponse> {
web::Json(schema.execute(request.into_inner()).await.into())
2020-05-09 21:56:45 +00:00
}
```
## Subscription example
```rust
# extern crate async_graphql_actix_web;
# extern crate async_graphql;
# extern crate actix_web;
# use async_graphql::*;
# #[derive(Default,SimpleObject)]
# struct Query { a: i32 }
# let schema = Schema::build(Query::default(), EmptyMutation, EmptySubscription).finish();
use actix_web::{web, HttpRequest, HttpResponse};
use async_graphql_actix_web::GraphQLSubscription;
2020-05-09 21:56:45 +00:00
async fn index_ws(
schema: web::Data<Schema<Query, EmptyMutation, EmptySubscription>>,
2020-05-09 21:56:45 +00:00
req: HttpRequest,
payload: web::Payload,
) -> actix_web::Result<HttpResponse> {
2021-11-16 06:51:20 +00:00
GraphQLSubscription::new(Schema::clone(&*schema)).start(&req, payload)
2020-05-09 21:56:45 +00:00
}
```
2020-10-14 01:10:06 +00:00
## More examples
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)