2019-11-15 14:50:35 -06:00
|
|
|
#![feature(decl_macro, proc_macro_hygiene)]
|
|
|
|
|
|
|
|
use juniper::{
|
2020-07-16 02:46:37 -05:00
|
|
|
tests::fixtures::starwars::{model::Database, schema::Query},
|
2020-04-10 03:10:24 -05:00
|
|
|
EmptyMutation, EmptySubscription, RootNode,
|
2019-11-15 14:50:35 -06:00
|
|
|
};
|
2020-06-30 10:13:15 -05:00
|
|
|
use rocket::{response::content, State};
|
2019-11-15 14:50:35 -06:00
|
|
|
|
2020-04-10 03:10:24 -05:00
|
|
|
type Schema = RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;
|
2019-11-15 14:50:35 -06:00
|
|
|
|
|
|
|
#[rocket::get("/")]
|
|
|
|
fn graphiql() -> content::Html<String> {
|
2020-04-10 03:10:24 -05:00
|
|
|
juniper_rocket_async::graphiql_source("/graphql")
|
2019-11-15 14:50:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[rocket::get("/graphql?<request>")]
|
|
|
|
fn get_graphql_handler(
|
|
|
|
context: State<Database>,
|
2020-04-10 03:10:24 -05:00
|
|
|
request: juniper_rocket_async::GraphQLRequest,
|
2019-11-15 14:50:35 -06:00
|
|
|
schema: State<Schema>,
|
2020-04-10 03:10:24 -05:00
|
|
|
) -> juniper_rocket_async::GraphQLResponse {
|
2020-03-10 00:40:26 -05:00
|
|
|
request.execute_sync(&schema, &context)
|
2019-11-15 14:50:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[rocket::post("/graphql", data = "<request>")]
|
|
|
|
fn post_graphql_handler(
|
|
|
|
context: State<Database>,
|
2020-04-10 03:10:24 -05:00
|
|
|
request: juniper_rocket_async::GraphQLRequest,
|
2019-11-15 14:50:35 -06:00
|
|
|
schema: State<Schema>,
|
2020-04-10 03:10:24 -05:00
|
|
|
) -> juniper_rocket_async::GraphQLResponse {
|
2020-03-10 00:40:26 -05:00
|
|
|
request.execute_sync(&schema, &context)
|
2019-11-15 14:50:35 -06:00
|
|
|
}
|
|
|
|
|
2020-06-23 06:17:27 -05:00
|
|
|
#[rocket::main]
|
|
|
|
async fn main() {
|
2019-11-15 14:50:35 -06:00
|
|
|
rocket::ignite()
|
|
|
|
.manage(Database::new())
|
2020-04-10 03:10:24 -05:00
|
|
|
.manage(Schema::new(
|
|
|
|
Query,
|
|
|
|
EmptyMutation::<Database>::new(),
|
|
|
|
EmptySubscription::<Database>::new(),
|
|
|
|
))
|
2019-11-15 14:50:35 -06:00
|
|
|
.mount(
|
|
|
|
"/",
|
|
|
|
rocket::routes![graphiql, get_graphql_handler, post_graphql_handler],
|
|
|
|
)
|
2020-04-10 03:10:24 -05:00
|
|
|
.launch()
|
2020-06-23 06:17:27 -05:00
|
|
|
.await
|
2020-04-10 03:10:24 -05:00
|
|
|
.expect("server to launch");
|
2019-11-15 14:50:35 -06:00
|
|
|
}
|