mirror of
https://github.com/actix/examples
synced 2024-11-24 14:53:00 +01:00
40 lines
1.1 KiB
Rust
40 lines
1.1 KiB
Rust
use actix_web::{web, Error, HttpResponse};
|
|
use juniper::http::graphiql::graphiql_source;
|
|
use juniper::http::GraphQLRequest;
|
|
|
|
use crate::db::Pool;
|
|
use crate::schemas::root::{create_schema, Context, Schema};
|
|
|
|
pub async fn graphql(
|
|
pool: web::Data<Pool>,
|
|
schema: web::Data<Schema>,
|
|
data: web::Json<GraphQLRequest>,
|
|
) -> Result<HttpResponse, Error> {
|
|
let ctx = Context {
|
|
dbpool: pool.get_ref().to_owned(),
|
|
};
|
|
let res = web::block(move || {
|
|
let res = data.execute_sync(&schema, &ctx);
|
|
serde_json::to_string(&res)
|
|
})
|
|
.await
|
|
.map_err(Error::from)?;
|
|
|
|
Ok(HttpResponse::Ok()
|
|
.content_type("application/json")
|
|
.body(res))
|
|
}
|
|
|
|
pub async fn graphql_playground() -> HttpResponse {
|
|
HttpResponse::Ok()
|
|
.content_type("text/html; charset=utf-8")
|
|
.body(graphiql_source("/graphql", None))
|
|
}
|
|
|
|
pub fn register(config: &mut web::ServiceConfig) {
|
|
config
|
|
.data(create_schema())
|
|
.route("/graphql", web::post().to(graphql))
|
|
.route("/graphiql", web::get().to(graphql_playground));
|
|
}
|