-
Notifications
You must be signed in to change notification settings - Fork 53
/
main.rs
36 lines (30 loc) · 1.12 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, Schema,
};
use async_graphql_rocket::{Request, Response};
use rocket::{response::content, routes, State};
use starwars::{QueryRoot, StarWars};
pub type StarWarsSchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;
#[rocket::get("/")]
fn graphql_playground() -> content::Html<String> {
content::Html(playground_source(GraphQLPlaygroundConfig::new("/")))
}
#[rocket::post("/?<query..>")]
async fn graphql_query(schema: State<'_, StarWarsSchema>, query: Request) -> Response {
query.execute(&schema).await
}
#[rocket::post("/", data = "<request>", format = "application/json")]
async fn graphql_request(schema: State<'_, StarWarsSchema>, request: Request) -> Response {
request.execute(&schema).await
}
#[rocket::launch]
fn rocket() -> rocket::Rocket {
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
.data(StarWars::new())
.finish();
rocket::ignite().manage(schema).mount(
"/",
routes![graphql_query, graphql_request, graphql_playground],
)
}