2020-03-06 01:07:35 +00:00
|
|
|
mod starwars;
|
|
|
|
|
2020-04-01 08:53:49 +00:00
|
|
|
use async_graphql::http::{graphiql_source, playground_source, GQLRequest, GQLResponse};
|
2020-03-19 09:20:12 +00:00
|
|
|
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
|
2020-04-01 08:53:49 +00:00
|
|
|
use futures::TryFutureExt;
|
2020-03-06 01:07:35 +00:00
|
|
|
use mime;
|
|
|
|
use tide::{self, Request, Response};
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
type StarWarsSchema = Schema<starwars::QueryRoot, EmptyMutation, EmptySubscription>;
|
2020-03-06 01:07:35 +00:00
|
|
|
|
|
|
|
async fn index(mut request: Request<StarWarsSchema>) -> Response {
|
|
|
|
let gql_request: GQLRequest = request.body_json().await.unwrap();
|
|
|
|
let schema = request.state();
|
2020-04-02 04:53:53 +00:00
|
|
|
let gql_response = futures::future::ready(gql_request.into_query_builder(schema))
|
2020-04-01 08:53:49 +00:00
|
|
|
.and_then(|builder| builder.execute())
|
|
|
|
.await;
|
|
|
|
Response::new(200)
|
|
|
|
.body_json(&GQLResponse(gql_response))
|
|
|
|
.unwrap()
|
2020-03-06 01:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn gql_playground(_request: Request<StarWarsSchema>) -> Response {
|
|
|
|
Response::new(200)
|
2020-03-17 09:26:59 +00:00
|
|
|
.body_string(playground_source("/", None))
|
2020-03-06 01:07:35 +00:00
|
|
|
.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<()> {
|
2020-03-07 10:03:19 +00:00
|
|
|
let mut app = tide::with_state(
|
2020-03-29 12:02:52 +00:00
|
|
|
Schema::build(starwars::QueryRoot, EmptyMutation, EmptySubscription)
|
|
|
|
.data(starwars::StarWars::new())
|
|
|
|
.finish(),
|
2020-03-07 10:03:19 +00:00
|
|
|
);
|
2020-03-06 01:07:35 +00:00
|
|
|
app.at("/").post(index);
|
|
|
|
app.at("/").get(gql_playground);
|
|
|
|
app.at("/graphiql").get(gql_graphiql);
|
|
|
|
app.listen("0.0.0.0:8000").await
|
|
|
|
}
|