//! 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 actix_web_lab::respond::Html; use juniper::http::{graphiql::graphiql_source, GraphQLRequest}; mod schema; use crate::schema::{create_schema, Schema}; /// GraphiQL UI #[get("/graphiql")] async fn graphiql() -> impl Responder { Html(graphiql_source("/graphql", None)) } /// GraphQL endpoint #[route("/graphql", method = "GET", method = "POST")] async fn graphql( st: web::Data, data: web::Json, ) -> 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 at http://localhost:8080"); // Start HTTP server HttpServer::new(move || { App::new() .app_data(Data::from(schema.clone())) .service(graphql) .service(graphiql) // 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 }