juniper/juniper_rocket/examples/rocket_server.rs
Christian Legnitto 93deb2862a
Make juniper_rocket_async replace juniper_rocket (#955)
* Make juniper_rocket_async replace juniper_rocket

Now that rocket v0.5 is async and in rc, merge the two juniper projects.

Fixes https://github.com/graphql-rust/juniper/issues/947.

* Remove println
2021-07-07 09:26:22 -07:00

48 lines
1.3 KiB
Rust

use juniper::{
tests::fixtures::starwars::schema::{Database, Query},
EmptyMutation, EmptySubscription, RootNode,
};
use rocket::{response::content, Rocket, State};
type Schema = RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;
#[rocket::get("/")]
fn graphiql() -> content::Html<String> {
juniper_rocket::graphiql_source("/graphql", None)
}
#[rocket::get("/graphql?<request>")]
fn get_graphql_handler(
context: &State<Database>,
request: juniper_rocket::GraphQLRequest,
schema: &State<Schema>,
) -> juniper_rocket::GraphQLResponse {
request.execute_sync(&*schema, &*context)
}
#[rocket::post("/graphql", data = "<request>")]
fn post_graphql_handler(
context: &State<Database>,
request: juniper_rocket::GraphQLRequest,
schema: &State<Schema>,
) -> juniper_rocket::GraphQLResponse {
request.execute_sync(&*schema, &*context)
}
#[rocket::main]
async fn main() {
Rocket::build()
.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");
}