1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-12-04 20:31:55 +01:00
actix-web/examples/juniper/src/main.rs

113 lines
3.0 KiB
Rust
Raw Normal View History

2018-02-28 06:11:24 +01:00
//! Actix web juniper example
//!
//! A simple example integrating juniper in actix-web
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate juniper;
extern crate futures;
extern crate actix;
extern crate actix_web;
extern crate env_logger;
2018-03-31 03:54:38 +02:00
use actix::prelude::*;
2018-03-31 09:16:55 +02:00
use actix_web::{
middleware, http, server,
App, AsyncResponder, HttpRequest, HttpResponse, HttpMessage, Error};
2018-02-28 06:11:24 +01:00
use juniper::http::graphiql::graphiql_source;
use juniper::http::GraphQLRequest;
use futures::future::Future;
mod schema;
use schema::Schema;
use schema::create_schema;
struct State {
executor: Addr<Syn, GraphQLExecutor>,
}
#[derive(Serialize, Deserialize)]
pub struct GraphQLData(GraphQLRequest);
impl Message for GraphQLData {
type Result = Result<String, Error>;
}
pub struct GraphQLExecutor {
schema: std::sync::Arc<Schema>
}
impl GraphQLExecutor {
fn new(schema: std::sync::Arc<Schema>) -> GraphQLExecutor {
GraphQLExecutor {
schema: schema,
}
}
}
impl Actor for GraphQLExecutor {
type Context = SyncContext<Self>;
}
impl Handler<GraphQLData> for GraphQLExecutor {
type Result = Result<String, Error>;
fn handle(&mut self, msg: GraphQLData, _: &mut Self::Context) -> Self::Result {
let res = msg.0.execute(&self.schema, &());
let res_text = serde_json::to_string(&res)?;
Ok(res_text)
}
}
2018-03-31 03:54:38 +02:00
fn graphiql(_req: HttpRequest<State>) -> Result<HttpResponse, Error> {
let html = graphiql_source("http://127.0.0.1:8080/graphql");
2018-03-31 03:54:38 +02:00
Ok(HttpResponse::Ok()
2018-02-28 06:11:24 +01:00
.content_type("text/html; charset=utf-8")
2018-03-31 08:37:15 +02:00
.body(html))
2018-02-28 06:11:24 +01:00
}
fn graphql(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
let executor = req.state().executor.clone();
req.json()
.from_err()
.and_then(move |val: GraphQLData| {
executor.send(val)
.from_err()
.and_then(|res| {
match res {
2018-03-31 08:37:15 +02:00
Ok(user) => Ok(HttpResponse::Ok().body(user)),
2018-03-31 03:54:38 +02:00
Err(_) => Ok(HttpResponse::InternalServerError().into())
2018-02-28 06:11:24 +01:00
}
})
})
.responder()
}
fn main() {
::std::env::set_var("RUST_LOG", "actix_web=info");
let _ = env_logger::init();
let sys = actix::System::new("juniper-example");
let schema = std::sync::Arc::new(create_schema());
let addr = SyncArbiter::start(3, move || {
GraphQLExecutor::new(schema.clone())
});
// Start http server
2018-03-31 03:54:38 +02:00
let _ = server::new(move || {
2018-03-31 09:16:55 +02:00
App::with_state(State{executor: addr.clone()})
2018-02-28 06:11:24 +01:00
// enable logger
.middleware(middleware::Logger::default())
2018-03-31 03:54:38 +02:00
.resource("/graphql", |r| r.method(http::Method::POST).h(graphql))
.resource("/graphiql", |r| r.method(http::Method::GET).h(graphiql))})
2018-02-28 06:11:24 +01:00
.bind("127.0.0.1:8080").unwrap()
.start();
println!("Started http server: 127.0.0.1:8080");
let _ = sys.run();
}