diff --git a/Cargo.toml b/Cargo.toml index ae35eaf6..42390a03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [ diff --git a/README.md b/README.md index d10b0c1d..fbe26467 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ - [ ] Integration examples - [X] Actix-web - [ ] Hyper - - [ ] Tide + - [X] Tide ## License diff --git a/examples/tide.rs b/examples/tide.rs new file mode 100644 index 00000000..a3026111 --- /dev/null +++ b/examples/tide.rs @@ -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; + +async fn index(mut request: Request) -> 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) -> Response { + Response::new(200) + .body_string(playground_source("/")) + .set_mime(mime::TEXT_HTML_UTF_8) +} +async fn gql_graphiql(_request: Request) -> 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 +}