1
0
mirror of https://github.com/actix/examples synced 2024-12-18 00:13:57 +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"
[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"

View File

@ -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<Arc<Schema>>,
data: web::Json<GraphQLRequest>,
) -> Result<HttpResponse, Error> {
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<Arc<Schema>>,
data: web::Json<GraphQLRequest>,
) -> Result<HttpResponse, Error> {
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
}