Add an example for tide

I haven't used tide before,
but it looks quite similar to
actix-web.
I modeled this example on the actix-web one,
and it seems to have worked perfectly.
This commit is contained in:
Dusty Phillips 2020-03-05 21:07:35 -04:00
parent 02c3acf451
commit 60f3b38b38
3 changed files with 41 additions and 1 deletions

View File

@ -34,6 +34,8 @@ async-std = { version = "1.5.0", features = ["attributes"] }
actix-web = "2.0.0"
actix-rt = "1.0.0"
slab = "0.4.2"
tide = "0.6.0"
mime = "0.3.16"
[workspace]
members = [

View File

@ -86,7 +86,7 @@
- [ ] Integration examples
- [X] Actix-web
- [ ] Hyper
- [ ] Tide
- [X] Tide
## License

38
examples/tide.rs Normal file
View File

@ -0,0 +1,38 @@
mod starwars;
use async_graphql::http::{graphiql_source, playground_source, GQLRequest};
use async_graphql::{GQLEmptyMutation, Schema};
use mime;
use tide::{self, Request, Response};
type StarWarsSchema = Schema<starwars::QueryRoot, GQLEmptyMutation>;
async fn index(mut request: Request<StarWarsSchema>) -> Response {
let gql_request: GQLRequest = request.body_json().await.unwrap();
let schema = request.state();
let gql_response = gql_request.execute(schema).await;
Response::new(200).body_json(&gql_response).unwrap()
}
async fn gql_playground(_request: Request<StarWarsSchema>) -> Response {
Response::new(200)
.body_string(playground_source("/"))
.set_mime(mime::TEXT_HTML_UTF_8)
}
async fn gql_graphiql(_request: Request<StarWarsSchema>) -> Response {
Response::new(200)
.body_string(graphiql_source("/"))
.set_mime(mime::TEXT_HTML_UTF_8)
}
#[async_std::main]
async fn main() -> std::io::Result<()> {
let mut app = tide::with_state(Schema::new(
starwars::QueryRoot(starwars::StarWars::new()),
GQLEmptyMutation,
));
app.at("/").post(index);
app.at("/").get(gql_playground);
app.at("/graphiql").get(gql_graphiql);
app.listen("0.0.0.0:8000").await
}