mirror of
https://github.com/actix/examples
synced 2025-01-22 22:05:57 +01:00
37 lines
930 B
Rust
37 lines
930 B
Rust
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<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);
|
|
}
|