1
0
mirror of https://github.com/actix/examples synced 2025-02-02 09:39:03 +01:00

38 lines
953 B
Rust
Raw Normal View History

2022-02-17 20:59:04 +00:00
use actix_web::{get, route, web, Error, HttpResponse, Responder};
use actix_web_lab::respond::Html;
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 {
dbpool: 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 {
Html(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
}