mirror of
https://github.com/actix/examples
synced 2024-11-24 06:43:00 +01:00
fix juniper example
This commit is contained in:
parent
cbd9c4ee01
commit
9ef1107c0c
@ -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"
|
||||||
|
@ -15,41 +15,44 @@ 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()
|
|
||||||
.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(
|
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type("application/json")
|
.content_type("text/html; charset=utf-8")
|
||||||
.body(user)
|
.body(html)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> io::Result<()> {
|
async fn graphql(
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
st: web::Data<Arc<Schema>>,
|
||||||
env_logger::init();
|
data: web::Json<GraphQLRequest>,
|
||||||
|
) -> Result<HttpResponse, Error> {
|
||||||
// Create Juniper schema
|
let user = web::block(move || {
|
||||||
let schema = std::sync::Arc::new(create_schema());
|
let res = data.execute(&st, &());
|
||||||
|
Ok::<_, serde_json::error::Error>(serde_json::to_string(&res)?)
|
||||||
// Start http server
|
})
|
||||||
HttpServer::new(move || {
|
.await?;
|
||||||
App::new()
|
Ok(HttpResponse::Ok()
|
||||||
.data(schema.clone())
|
.content_type("application/json")
|
||||||
.wrap(middleware::Logger::default())
|
.body(user))
|
||||||
.service(web::resource("/graphql").route(web::post().to_async(graphql)))
|
}
|
||||||
.service(web::resource("/graphiql").route(web::get().to(graphiql)))
|
|
||||||
})
|
#[actix_rt::main]
|
||||||
.bind("127.0.0.1:8080")?
|
async fn main() -> io::Result<()> {
|
||||||
.run()
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user