use juniper::{ tests::fixtures::starwars::schema::{Database, Query}, EmptyMutation, EmptySubscription, RootNode, }; use rocket::{response::content::RawHtml, routes, State}; type Schema = RootNode<'static, Query, EmptyMutation, EmptySubscription>; #[rocket::get("/")] async fn homepage() -> RawHtml<&'static str> { RawHtml( "

juniper_rocket/simple example

\
visit GraphiQL
\
visit GraphQL Playground
\ ", ) } #[rocket::get("/graphiql")] fn graphiql() -> RawHtml { juniper_rocket::graphiql_source("/graphql", None) } #[rocket::get("/playground")] fn playground() -> RawHtml { juniper_rocket::playground_source("/graphql", None) } // GET request accepts query parameters like these: // ?query= // &operationName= // &variables= // See details here: https://graphql.org/learn/serving-over-http#get-request #[rocket::get("/graphql?")] async fn get_graphql( db: &State, request: juniper_rocket::GraphQLRequest, schema: &State, ) -> juniper_rocket::GraphQLResponse { request.execute(schema, db).await } #[rocket::post("/graphql", data = "")] async fn post_graphql( db: &State, request: juniper_rocket::GraphQLRequest, schema: &State, ) -> juniper_rocket::GraphQLResponse { request.execute(schema, db).await } #[rocket::main] async fn main() { _ = rocket::build() .manage(Database::new()) .manage(Schema::new( Query, EmptyMutation::new(), EmptySubscription::new(), )) .mount( "/", routes![homepage, graphiql, playground, get_graphql, post_graphql], ) .launch() .await .expect("server to launch"); }