2018-04-13 09:18:42 +08: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 actix;
|
|
|
|
extern crate actix_web;
|
|
|
|
extern crate env_logger;
|
2018-05-08 11:08:43 -07:00
|
|
|
extern crate futures;
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
use actix::prelude::*;
|
2018-05-20 21:03:29 -07:00
|
|
|
use actix_web::{
|
|
|
|
http, middleware, server, App, AsyncResponder, Error, FutureResponse, HttpRequest,
|
|
|
|
HttpResponse, Json, State,
|
|
|
|
};
|
2018-05-08 11:08:43 -07:00
|
|
|
use futures::future::Future;
|
2018-04-13 09:18:42 +08:00
|
|
|
use juniper::http::graphiql::graphiql_source;
|
|
|
|
use juniper::http::GraphQLRequest;
|
|
|
|
|
|
|
|
mod schema;
|
|
|
|
|
|
|
|
use schema::create_schema;
|
2018-05-08 11:08:43 -07:00
|
|
|
use schema::Schema;
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
struct AppState {
|
2018-07-16 12:36:53 +06:00
|
|
|
executor: Addr<GraphQLExecutor>,
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GraphQLData(GraphQLRequest);
|
|
|
|
|
|
|
|
impl Message for GraphQLData {
|
|
|
|
type Result = Result<String, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GraphQLExecutor {
|
2018-05-08 11:08:43 -07:00
|
|
|
schema: std::sync::Arc<Schema>,
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GraphQLExecutor {
|
|
|
|
fn new(schema: std::sync::Arc<Schema>) -> GraphQLExecutor {
|
2018-05-08 11:08:43 -07:00
|
|
|
GraphQLExecutor { schema: schema }
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-16 12:36:53 +06:00
|
|
|
fn graphiql(_req: &HttpRequest<AppState>) -> Result<HttpResponse, Error> {
|
2018-04-13 09:18:42 +08:00
|
|
|
let html = graphiql_source("http://127.0.0.1:8080/graphql");
|
|
|
|
Ok(HttpResponse::Ok()
|
2018-05-08 11:08:43 -07:00
|
|
|
.content_type("text/html; charset=utf-8")
|
|
|
|
.body(html))
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
2018-05-08 11:08:43 -07:00
|
|
|
fn graphql(
|
2018-06-01 11:31:53 -07:00
|
|
|
(st, data): (State<AppState>, Json<GraphQLData>),
|
2018-05-08 11:08:43 -07:00
|
|
|
) -> FutureResponse<HttpResponse> {
|
|
|
|
st.executor
|
|
|
|
.send(data.0)
|
2018-04-13 09:18:42 +08:00
|
|
|
.from_err()
|
2018-05-08 11:08:43 -07:00
|
|
|
.and_then(|res| match res {
|
|
|
|
Ok(user) => Ok(HttpResponse::Ok()
|
|
|
|
.content_type("application/json")
|
|
|
|
.body(user)),
|
|
|
|
Err(_) => Ok(HttpResponse::InternalServerError().into()),
|
2018-04-13 09:18:42 +08:00
|
|
|
})
|
|
|
|
.responder()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
|
|
|
env_logger::init();
|
|
|
|
let sys = actix::System::new("juniper-example");
|
|
|
|
|
|
|
|
let schema = std::sync::Arc::new(create_schema());
|
2018-05-08 11:08:43 -07:00
|
|
|
let addr = SyncArbiter::start(3, move || GraphQLExecutor::new(schema.clone()));
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
// Start http server
|
|
|
|
server::new(move || {
|
2019-03-09 18:03:09 -08:00
|
|
|
App::with_state(AppState {
|
|
|
|
executor: addr.clone(),
|
|
|
|
})
|
|
|
|
// enable logger
|
|
|
|
.middleware(middleware::Logger::default())
|
|
|
|
.resource("/graphql", |r| r.method(http::Method::POST).with(graphql))
|
|
|
|
.resource("/graphiql", |r| r.method(http::Method::GET).h(graphiql))
|
|
|
|
})
|
|
|
|
.bind("127.0.0.1:8080")
|
|
|
|
.unwrap()
|
|
|
|
.start();
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
println!("Started http server: 127.0.0.1:8080");
|
|
|
|
let _ = sys.run();
|
|
|
|
}
|