mirror of https://github.com/fafhrd91/actix-web
improve juniper example
This commit is contained in:
parent
e2acb0030a
commit
46a9c71521
|
@ -15,3 +15,4 @@ serde_json = "1.0"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
|
|
||||||
juniper = "0.9.2"
|
juniper = "0.9.2"
|
||||||
|
lazy_static = "1.0"
|
||||||
|
|
|
@ -1,15 +1,20 @@
|
||||||
//! Actix web juniper example
|
//! Actix web juniper example
|
||||||
//!
|
//!
|
||||||
//! Juniper is a graphql framework implemetation for rust
|
//! A simple example integrating juniper in actix-web
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
|
#[macro_use]
|
||||||
extern crate juniper;
|
extern crate juniper;
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
extern crate actix;
|
extern crate actix;
|
||||||
extern crate actix_web;
|
extern crate actix_web;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
|
||||||
|
use actix::*;
|
||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
use juniper::http::graphiql::graphiql_source;
|
use juniper::http::graphiql::graphiql_source;
|
||||||
use juniper::http::GraphQLRequest;
|
use juniper::http::GraphQLRequest;
|
||||||
|
@ -21,8 +26,35 @@ mod schema;
|
||||||
use schema::Schema;
|
use schema::Schema;
|
||||||
use schema::create_schema;
|
use schema::create_schema;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref SCHEMA: Schema = create_schema();
|
||||||
|
}
|
||||||
|
|
||||||
struct State {
|
struct State {
|
||||||
schema: Schema,
|
executor: Addr<Syn, GraphQLExecutor>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct GraphQLData(GraphQLRequest);
|
||||||
|
|
||||||
|
impl Message for GraphQLData {
|
||||||
|
type Result = Result<String, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct GraphQLExecutor;
|
||||||
|
|
||||||
|
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(&SCHEMA, &());
|
||||||
|
let res_text = serde_json::to_string(&res)?;
|
||||||
|
Ok(res_text)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn graphiql(_req: HttpRequest<State>) -> Result<HttpResponse> {
|
fn graphiql(_req: HttpRequest<State>) -> Result<HttpResponse> {
|
||||||
|
@ -33,11 +65,18 @@ fn graphiql(_req: HttpRequest<State>) -> Result<HttpResponse> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn graphql(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
fn graphql(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
||||||
|
|
||||||
req.json()
|
req.json()
|
||||||
.from_err()
|
.from_err()
|
||||||
.and_then(move |val: GraphQLRequest| {
|
.and_then(move |val: GraphQLData| {
|
||||||
let response = val.execute(&req.state().schema, &());
|
req.state().executor.send(val)
|
||||||
Ok(httpcodes::HTTPOk.build().json(response)?)
|
.from_err()
|
||||||
|
.and_then(|res| {
|
||||||
|
match res {
|
||||||
|
Ok(user) => Ok(httpcodes::HTTPOk.build().body(user)?),
|
||||||
|
Err(_) => Ok(httpcodes::HTTPInternalServerError.into())
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.responder()
|
.responder()
|
||||||
}
|
}
|
||||||
|
@ -47,9 +86,13 @@ fn main() {
|
||||||
let _ = env_logger::init();
|
let _ = env_logger::init();
|
||||||
let sys = actix::System::new("juniper-example");
|
let sys = actix::System::new("juniper-example");
|
||||||
|
|
||||||
|
let addr = SyncArbiter::start(3, || {
|
||||||
|
GraphQLExecutor{}
|
||||||
|
});
|
||||||
|
|
||||||
// Start http server
|
// Start http server
|
||||||
let _addr = HttpServer::new(move || {
|
let _addr = HttpServer::new(move || {
|
||||||
Application::with_state(State{schema: create_schema() })
|
Application::with_state(State{executor: addr.clone()})
|
||||||
// enable logger
|
// enable logger
|
||||||
.middleware(middleware::Logger::default())
|
.middleware(middleware::Logger::default())
|
||||||
.resource("/graphql", |r| r.method(Method::POST).a(graphql))
|
.resource("/graphql", |r| r.method(Method::POST).a(graphql))
|
||||||
|
|
Loading…
Reference in New Issue