1
0
mirror of https://github.com/actix/examples synced 2024-11-24 06:43:00 +01:00

fix juniper example

This commit is contained in:
Nikolay Kim 2019-12-08 00:03:53 +06:00
parent cbd9c4ee01
commit 9ef1107c0c
2 changed files with 39 additions and 35 deletions

View File

@ -6,7 +6,8 @@ workspace = ".."
edition = "2018" edition = "2018"
[dependencies] [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" env_logger = "0.7.1"
serde = "1.0.103" serde = "1.0.103"
serde_json = "1.0.44" serde_json = "1.0.44"

View File

@ -15,27 +15,29 @@ mod schema;
use crate::schema::{create_schema, Schema}; use crate::schema::{create_schema, Schema};
fn graphiql() -> HttpResponse { async fn graphiql() -> HttpResponse {
let html = graphiql_source("http://127.0.0.1:8080/graphql"); let html = graphiql_source("http://127.0.0.1:8080/graphql");
HttpResponse::Ok() HttpResponse::Ok()
.content_type("text/html; charset=utf-8") .content_type("text/html; charset=utf-8")
.body(html) .body(html)
} }
fn graphql( async fn graphql(
st: web::Data<Arc<Schema>>, st: web::Data<Arc<Schema>>,
data: web::Json<GraphQLRequest>, data: web::Json<GraphQLRequest>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let user = web::block(move || {
let res = data.execute(&st, &()); let res = data.execute(&st, &());
let user = serde_json::to_string(&res)?; Ok::<_, serde_json::error::Error>(serde_json::to_string(&res)?)
Ok( })
HttpResponse::Ok() .await?;
Ok(HttpResponse::Ok()
.content_type("application/json") .content_type("application/json")
.body(user) .body(user))
)
} }
fn main() -> io::Result<()> { #[actix_rt::main]
async fn main() -> io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info"); std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init(); env_logger::init();
@ -47,9 +49,10 @@ fn main() -> io::Result<()> {
App::new() App::new()
.data(schema.clone()) .data(schema.clone())
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.service(web::resource("/graphql").route(web::post().to_async(graphql))) .service(web::resource("/graphql").route(web::post().to(graphql)))
.service(web::resource("/graphiql").route(web::get().to(graphiql))) .service(web::resource("/graphiql").route(web::get().to(graphiql)))
}) })
.bind("127.0.0.1:8080")? .bind("127.0.0.1:8080")?
.run() .start()
.await
} }