From 1775dea2c9a9dbee7a47a11ae53d926696dcc80d Mon Sep 17 00:00:00 2001 From: pyros2097 Date: Fri, 23 Feb 2018 14:05:34 +0530 Subject: [PATCH] add juniper example --- Cargo.toml | 1 + examples/juniper/Cargo.toml | 17 ++++++++++ examples/juniper/README.md | 15 ++++++++ examples/juniper/src/main.rs | 62 ++++++++++++++++++++++++++++++++++ examples/juniper/src/schema.rs | 58 +++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+) create mode 100644 examples/juniper/Cargo.toml create mode 100644 examples/juniper/README.md create mode 100644 examples/juniper/src/main.rs create mode 100644 examples/juniper/src/schema.rs diff --git a/Cargo.toml b/Cargo.toml index b7999b743..47cc2301b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,6 +99,7 @@ members = [ "./", "examples/basics", "examples/diesel", + "examples/juniper", "examples/r2d2", "examples/json", "examples/hello-world", diff --git a/examples/juniper/Cargo.toml b/examples/juniper/Cargo.toml new file mode 100644 index 000000000..d66edc0c4 --- /dev/null +++ b/examples/juniper/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "juniper-example" +version = "0.1.0" +authors = ["Nikolay Kim "] +workspace = "../.." + +[dependencies] +env_logger = "0.5" +actix = "0.5" +actix-web = { path = "../../" } + +futures = "0.1" +serde = "1.0" +serde_json = "1.0" +serde_derive = "1.0" + +juniper = "0.9.2" diff --git a/examples/juniper/README.md b/examples/juniper/README.md new file mode 100644 index 000000000..2ac0eac4e --- /dev/null +++ b/examples/juniper/README.md @@ -0,0 +1,15 @@ +# juniper + +Juniper integration for Actix web + +### server + +```bash +cd actix-web/examples/juniper +cargo run (or ``cargo watch -x run``) +# Started http server: 127.0.0.1:8080 +``` + +### web client + +[http://127.0.0.1:8080/graphiql](http://127.0.0.1:8080/graphiql) diff --git a/examples/juniper/src/main.rs b/examples/juniper/src/main.rs new file mode 100644 index 000000000..b5c620f1b --- /dev/null +++ b/examples/juniper/src/main.rs @@ -0,0 +1,62 @@ +//! Actix web juniper example +//! +//! Juniper is a graphql framework implemetation for rust +extern crate serde; +extern crate serde_json; +#[macro_use] +extern crate juniper; +extern crate futures; +extern crate actix; +extern crate actix_web; +extern crate env_logger; + +use actix_web::*; +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 { + schema: Schema, +} + +fn graphiql(_req: HttpRequest) -> Result { + let html = graphiql_source("http://localhost:8080/graphql"); + Ok(HttpResponse::build(StatusCode::OK) + .content_type("text/html; charset=utf-8") + .body(html).unwrap()) +} + +fn graphql(req: HttpRequest) -> Box> { + req.json() + .from_err() + .and_then(move |val: GraphQLRequest| { + let response = val.execute(&req.state().schema, &()); + Ok(httpcodes::HTTPOk.build().json(response)?) + }) + .responder() +} + +fn main() { + ::std::env::set_var("RUST_LOG", "actix_web=info"); + let _ = env_logger::init(); + let sys = actix::System::new("juniper-example"); + + // Start http server + let _addr = HttpServer::new(move || { + Application::with_state(State{schema: create_schema() }) + // enable logger + .middleware(middleware::Logger::default()) + .resource("/graphql", |r| r.method(Method::POST).a(graphql)) + .resource("/graphiql", |r| r.method(Method::GET).f(graphiql))}) + .bind("127.0.0.1:8080").unwrap() + .start(); + + println!("Started http server: 127.0.0.1:8080"); + let _ = sys.run(); +} diff --git a/examples/juniper/src/schema.rs b/examples/juniper/src/schema.rs new file mode 100644 index 000000000..2b4cf3042 --- /dev/null +++ b/examples/juniper/src/schema.rs @@ -0,0 +1,58 @@ +use juniper::FieldResult; +use juniper::RootNode; + +#[derive(GraphQLEnum)] +enum Episode { + NewHope, + Empire, + Jedi, +} + +#[derive(GraphQLObject)] +#[graphql(description = "A humanoid creature in the Star Wars universe")] +struct Human { + id: String, + name: String, + appears_in: Vec, + home_planet: String, +} + +#[derive(GraphQLInputObject)] +#[graphql(description = "A humanoid creature in the Star Wars universe")] +struct NewHuman { + name: String, + appears_in: Vec, + home_planet: String, +} + +pub struct QueryRoot; + +graphql_object!(QueryRoot: () |&self| { + field human(&executor, id: String) -> FieldResult { + Ok(Human{ + id: "1234".to_owned(), + name: "Luke".to_owned(), + appears_in: vec![Episode::NewHope], + home_planet: "Mars".to_owned(), + }) + } +}); + +pub struct MutationRoot; + +graphql_object!(MutationRoot: () |&self| { + field createHuman(&executor, new_human: NewHuman) -> FieldResult { + Ok(Human{ + id: "1234".to_owned(), + name: new_human.name, + appears_in: new_human.appears_in, + home_planet: new_human.home_planet, + }) + } +}); + +pub type Schema = RootNode<'static, QueryRoot, MutationRoot>; + +pub fn create_schema() -> Schema { + Schema::new(QueryRoot {}, MutationRoot {}) +}