mod starwars; use actix_web::{guard, web, App, HttpResponse, HttpServer, Result}; use async_graphql::http::{playground_source, GraphQLPlaygroundConfig}; use async_graphql::{EmptyMutation, EmptySubscription, Schema}; use async_graphql_actix_web::{Request, Response}; use starwars::{QueryRoot, StarWars, StarWarsSchema}; async fn index(schema: web::Data, req: Request) -> Response { schema.execute(req.into_inner()).await.into() } async fn index_playground() -> Result { Ok(HttpResponse::Ok() .content_type("text/html; charset=utf-8") .body(playground_source( GraphQLPlaygroundConfig::new("/").subscription_endpoint("/"), ))) } #[actix_web::main] async fn main() -> std::io::Result<()> { let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) .data(StarWars::new()) .finish(); println!("Playground: http://localhost:8000"); HttpServer::new(move || { App::new() .data(schema.clone()) .service(web::resource("/").guard(guard::Post()).to(index)) .service(web::resource("/").guard(guard::Get()).to(index_playground)) }) .bind("127.0.0.1:8000")? .run() .await }