use actix_web::{get, route, web, Error, HttpResponse, Responder}; use juniper::http::{graphiql::graphiql_source, GraphQLRequest}; use crate::{ db::Pool, schemas::root::{create_schema, Context, Schema}, }; /// GraphQL endpoint #[route("/graphql", method = "GET", method = "POST")] pub async fn graphql( pool: web::Data, schema: web::Data, data: web::Json, ) -> Result { let ctx = Context { db_pool: pool.get_ref().to_owned(), }; let res = data.execute(&schema, &ctx).await; Ok(HttpResponse::Ok().json(res)) } /// GraphiQL UI #[get("/graphiql")] async fn graphql_playground() -> impl Responder { web::Html::new(graphiql_source("/graphql", None)) } pub fn register(config: &mut web::ServiceConfig) { config .app_data(web::Data::new(create_schema())) .service(graphql) .service(graphql_playground); }