juniper/juniper_rocket_async/examples/rocket_server.rs

51 lines
1.4 KiB
Rust
Raw Normal View History

#![feature(decl_macro, proc_macro_hygiene)]
use juniper::{
tests::fixtures::starwars::{model::Database, schema::Query},
EmptyMutation, EmptySubscription, RootNode,
};
use rocket::{response::content, State};
type Schema = RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;
#[rocket::get("/")]
fn graphiql() -> content::Html<String> {
juniper_rocket_async::graphiql_source("/graphql")
}
#[rocket::get("/graphql?<request>")]
fn get_graphql_handler(
context: State<Database>,
request: juniper_rocket_async::GraphQLRequest,
schema: State<Schema>,
) -> juniper_rocket_async::GraphQLResponse {
request.execute_sync(&schema, &context)
}
#[rocket::post("/graphql", data = "<request>")]
fn post_graphql_handler(
context: State<Database>,
request: juniper_rocket_async::GraphQLRequest,
schema: State<Schema>,
) -> juniper_rocket_async::GraphQLResponse {
request.execute_sync(&schema, &context)
}
#[rocket::main]
async fn main() {
rocket::ignite()
.manage(Database::new())
.manage(Schema::new(
Query,
EmptyMutation::<Database>::new(),
EmptySubscription::<Database>::new(),
))
.mount(
"/",
rocket::routes![graphiql, get_graphql_handler, post_graphql_handler],
)
.launch()
.await
.expect("server to launch");
}