use actix_web::{Error, HttpResponse, Responder, get, route, web}; use juniper::http::{GraphQLRequest, graphiql::graphiql_source}; use crate::{ db::Pool, schemas::root::{Context, Schema, create_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); }