translate section 6 (integrations)

This commit is contained in:
Ethan Fast 2020-05-09 14:56:45 -07:00
parent 5e7a7f0df1
commit ddceb05e69
3 changed files with 66 additions and 0 deletions

View File

@ -1 +1,8 @@
# Integrations
`Async-Graphql` supports several common Rust web servers.
- Actix-web [async-graphql-actix-web](https://crates.io/crates/async-graphql-actix-web)
- Warp [async-graphql-warp](https://crates.io/crates/async-graphql-warp)
**Even if the server you are currently using is not in the above list, it is quite simple to implement similar functionality yourself**

View File

@ -1 +1,32 @@
# Actix-web
`Async-graphql-actix-web` provides an implementation of `actix_web::FromRequest` for `GQLRequest`. This is actually an abstraction around `QueryBuilder` and you can call `GQLRequest::into_inner` to convert it into a `QueryBuilder`
`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.
```rust
async fn index(
// Schema now accessible here
schema: web::Data<Schema>,
gql_request: GQLRequest,
) -> web::Json<GQLResponse> {
web::Json(GQLResponse(gql_request.into_inner().execute(&schema).await))
}
```
## Subscription example
```rust
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)
}
```

View File

@ -1 +1,29 @@
# Warp
For `Async-graphql-warp`, two `Filter` integrations are provided: `graphql` and `graphql_subscription`.
The `graphql` filter is used for execution `Query` and `Mutation` requests. It always asks for the POST method and outputs a `Schema` via `QueryBuilder`. You can combine other filters later, or directly call `QueryBuilder::execute` to execute the query.
`graphql_subscription` is used to implement WebSocket subscriptions. It outputs `warp::Reply`.
## Request example
```rust
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let filter = async_graphql_warp::graphql(schema).and_then(|(schema, builder): (_, QueryBuilder)| async move {
// Execute query
let resp = builder.execute(&schema).await;
// Return result
Ok::<_, Infallible>(warp::reply::json(&GQLResponse(resp)).into_response())
});
warp::serve(filter).run(([0, 0, 0, 0], 8000)).await;
```
## Subscription example
```rust
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;
```