1
0
mirror of https://github.com/actix/examples synced 2025-02-09 20:25:37 +01:00
2024-07-07 00:23:03 +01:00

57 lines
1.5 KiB
Rust

//! Actix Web juniper example
//!
//! A simple example integrating juniper in Actix Web
use std::{io, sync::Arc};
use actix_cors::Cors;
use actix_web::{
get, middleware, route,
web::{self, Data},
App, HttpResponse, HttpServer, Responder,
};
use juniper::http::{graphiql::graphiql_source, GraphQLRequest};
mod schema;
use crate::schema::{create_schema, Schema};
/// GraphiQL playground UI
#[get("/graphiql")]
async fn graphql_playground() -> impl Responder {
web::Html::new(graphiql_source("/graphql", None))
}
/// GraphQL endpoint
#[route("/graphql", method = "GET", method = "POST")]
async fn graphql(st: web::Data<Schema>, data: web::Json<GraphQLRequest>) -> impl Responder {
let user = data.execute(&st, &()).await;
HttpResponse::Ok().json(user)
}
#[actix_web::main]
async fn main() -> io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
// Create Juniper schema
let schema = Arc::new(create_schema());
log::info!("starting HTTP server on port 8080");
log::info!("GraphiQL playground: http://localhost:8080/graphiql");
// Start HTTP server
HttpServer::new(move || {
App::new()
.app_data(Data::from(schema.clone()))
.service(graphql)
.service(graphql_playground)
// the graphiql UI requires CORS to be enabled
.wrap(Cors::permissive())
.wrap(middleware::Logger::default())
})
.workers(2)
.bind(("127.0.0.1", 8080))?
.run()
.await
}