use crate::redispool::RedisPool; use self::types::World; use juniper::{graphql_object, FieldResult, ID}; use rocket::response::content::RawHtml; pub mod types; pub struct Query; #[graphql_object(context = Context)] impl Query { fn world(id: ID) -> FieldResult> { Ok(Some(World { id })) } fn allWorlds() -> FieldResult> { Ok(vec![ World { id: ID::from("1".to_string()), }, World { id: ID::from("10".to_string()), }, World { id: ID::from("13".to_string()), }, World { id: ID::from("17".to_string()), }, World { id: ID::from("19".to_string()), }, World { id: ID::from("40".to_string()), }, World { id: ID::from("1000".to_string()), }, World { id: ID::from("2000".to_string()), }, ]) } } pub struct Context { con: RedisPool, } impl juniper::Context for Context {} #[get("/graphiql")] pub fn graphiql() -> RawHtml { juniper_rocket::graphiql_source("/graphql", None) } #[get("/")] pub fn playground() -> RawHtml { juniper_rocket::playground_source("/graphql", None) } #[post("/", data = "")] pub async fn post_graphql( query: juniper_rocket::GraphQLRequest, schema: &rocket::State, con: &RedisPool, ) -> juniper_rocket::GraphQLResponse { query.execute(&*schema, &Context { con: con.clone() }).await } #[get("/?")] pub async fn get_graphql( query: juniper_rocket::GraphQLRequest, schema: &rocket::State, con: &RedisPool, ) -> juniper_rocket::GraphQLResponse { query.execute(&*schema, &Context { con: con.clone() }).await } pub type Schema = juniper::RootNode< 'static, Query, juniper::EmptyMutation, juniper::EmptySubscription, >; pub fn schema() -> Schema { Schema::new( Query, juniper::EmptyMutation::::new(), juniper::EmptySubscription::::new(), ) }