From 9ef1107c0cc58ded833c052c81347ca79cc0e996 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 8 Dec 2019 00:03:53 +0600 Subject: [PATCH] fix juniper example --- juniper/Cargo.toml | 3 +- juniper/src/main.rs | 71 +++++++++++++++++++++++---------------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/juniper/Cargo.toml b/juniper/Cargo.toml index dbff40cd..c9029eeb 100644 --- a/juniper/Cargo.toml +++ b/juniper/Cargo.toml @@ -6,7 +6,8 @@ workspace = ".." edition = "2018" [dependencies] -actix-web = "1.0.9" +actix-web = "2.0.0-alpha.3" +actix-rt = "1.0.0-alpha.3" env_logger = "0.7.1" serde = "1.0.103" serde_json = "1.0.44" diff --git a/juniper/src/main.rs b/juniper/src/main.rs index 5b820e49..7eb4a93f 100644 --- a/juniper/src/main.rs +++ b/juniper/src/main.rs @@ -15,41 +15,44 @@ mod schema; use crate::schema::{create_schema, Schema}; -fn graphiql() -> HttpResponse { - let html = graphiql_source("http://127.0.0.1:8080/graphql"); - HttpResponse::Ok() - .content_type("text/html; charset=utf-8") - .body(html) -} - -fn graphql( - st: web::Data>, - data: web::Json, -) -> Result { - let res = data.execute(&st, &()); - let user = serde_json::to_string(&res)?; - Ok( +async fn graphiql() -> HttpResponse { + let html = graphiql_source("http://127.0.0.1:8080/graphql"); HttpResponse::Ok() - .content_type("application/json") - .body(user) - ) + .content_type("text/html; charset=utf-8") + .body(html) } -fn main() -> io::Result<()> { - std::env::set_var("RUST_LOG", "actix_web=info"); - env_logger::init(); - - // Create Juniper schema - let schema = std::sync::Arc::new(create_schema()); - - // Start http server - HttpServer::new(move || { - App::new() - .data(schema.clone()) - .wrap(middleware::Logger::default()) - .service(web::resource("/graphql").route(web::post().to_async(graphql))) - .service(web::resource("/graphiql").route(web::get().to(graphiql))) - }) - .bind("127.0.0.1:8080")? - .run() +async fn graphql( + st: web::Data>, + data: web::Json, +) -> Result { + let user = web::block(move || { + let res = data.execute(&st, &()); + Ok::<_, serde_json::error::Error>(serde_json::to_string(&res)?) + }) + .await?; + Ok(HttpResponse::Ok() + .content_type("application/json") + .body(user)) +} + +#[actix_rt::main] +async fn main() -> io::Result<()> { + std::env::set_var("RUST_LOG", "actix_web=info"); + env_logger::init(); + + // Create Juniper schema + let schema = std::sync::Arc::new(create_schema()); + + // Start http server + HttpServer::new(move || { + App::new() + .data(schema.clone()) + .wrap(middleware::Logger::default()) + .service(web::resource("/graphql").route(web::post().to(graphql))) + .service(web::resource("/graphiql").route(web::get().to(graphiql))) + }) + .bind("127.0.0.1:8080")? + .start() + .await }