1
0
mirror of https://github.com/actix/examples synced 2025-04-22 16:44:52 +02:00
2025-03-21 06:07:31 +00:00

37 lines
930 B
Rust

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<Pool>,
schema: web::Data<Schema>,
data: web::Json<GraphQLRequest>,
) -> Result<HttpResponse, Error> {
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);
}