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

67 lines
2.1 KiB
Markdown
Raw Normal View History

2020-04-15 03:15:30 +00:00
# Warp
2020-05-09 21:56:45 +00:00
For `Async-graphql-warp`, two `Filter` integrations are provided: `graphql` and `graphql_subscription`.
2021-11-16 06:51:20 +00:00
The `graphql` filter is used for execution `Query` and `Mutation` requests. It extracts GraphQL request and outputs `async_graphql::Schema` and `async_graphql::Request`.
2020-09-11 15:38:18 +00:00
You can combine other filters later, or directly call `Schema::execute` to execute the query.
2020-05-09 21:56:45 +00:00
`graphql_subscription` is used to implement WebSocket subscriptions. It outputs `warp::Reply`.
## Request example
```rust
# extern crate async_graphql_warp;
# extern crate async_graphql;
# extern crate warp;
# use async_graphql::*;
# use std::convert::Infallible;
# use warp::Filter;
# struct QueryRoot;
# #[Object]
# impl QueryRoot { async fn version(&self) -> &str { "1.0" } }
# async fn other() {
2020-09-11 15:38:18 +00:00
type MySchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;
2020-05-09 21:56:45 +00:00
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
2020-09-11 15:38:18 +00:00
let filter = async_graphql_warp::graphql(schema).and_then(|(schema, request): (MySchema, async_graphql::Request)| async move {
2020-05-09 21:56:45 +00:00
// Execute query
2020-09-11 15:38:18 +00:00
let resp = schema.execute(request).await;
2020-05-09 21:56:45 +00:00
// Return result
Ok::<_, Infallible>(async_graphql_warp::GraphQLResponse::from(resp))
2020-05-09 21:56:45 +00:00
});
warp::serve(filter).run(([0, 0, 0, 0], 8000)).await;
# }
2020-05-09 21:56:45 +00:00
```
## Subscription example
```rust
# extern crate async_graphql_warp;
# extern crate async_graphql;
# extern crate warp;
# use async_graphql::*;
# use futures_util::stream::{Stream, StreamExt};
# use std::convert::Infallible;
# use warp::Filter;
# struct SubscriptionRoot;
# #[Subscription]
# impl SubscriptionRoot {
# async fn tick(&self) -> impl Stream<Item = i32> {
# futures_util::stream::iter(0..10)
# }
# }
# struct QueryRoot;
# #[Object]
# impl QueryRoot { async fn version(&self) -> &str { "1.0" } }
# async fn other() {
2020-05-09 21:56:45 +00:00
let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);
let filter = async_graphql_warp::graphql_subscription(schema);
warp::serve(filter).run(([0, 0, 0, 0], 8000)).await;
# }
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/warp](https://github.com/async-graphql/examples/tree/master/warp)