1
0
mirror of https://github.com/actix/examples synced 2025-01-22 22:05:57 +01:00

37 lines
930 B
Rust
Raw Permalink Normal View History

2022-02-17 20:59:04 +00:00
use actix_web::{get, route, web, Error, HttpResponse, Responder};
use juniper::http::{graphiql::graphiql_source, GraphQLRequest};
2022-02-17 20:59:04 +00:00
use crate::{
db::Pool,
schemas::root::{create_schema, Context, Schema},
};
2022-02-17 20:59:04 +00:00
/// GraphQL endpoint
#[route("/graphql", method = "GET", method = "POST")]
2019-12-07 23:59:24 +06:00
pub async fn graphql(
pool: web::Data<Pool>,
schema: web::Data<Schema>,
data: web::Json<GraphQLRequest>,
2019-12-07 23:59:24 +06:00
) -> Result<HttpResponse, Error> {
let ctx = Context {
2022-07-09 23:11:36 +01:00
db_pool: pool.get_ref().to_owned(),
};
2019-12-07 23:59:24 +06:00
2022-02-17 20:59:04 +00:00
let res = data.execute(&schema, &ctx).await;
Ok(HttpResponse::Ok().json(res))
}
2022-02-17 20:59:04 +00:00
/// GraphiQL UI
#[get("/graphiql")]
async fn graphql_playground() -> impl Responder {
2024-07-07 00:23:03 +01:00
web::Html::new(graphiql_source("/graphql", None))
}
pub fn register(config: &mut web::ServiceConfig) {
config
2022-02-17 20:59:04 +00:00
.app_data(web::Data::new(create_schema()))
.service(graphql)
.service(graphql_playground);
2019-12-07 23:59:24 +06:00
}