From 7e37bef4a16b79c321b6c7c938a05970e8b34568 Mon Sep 17 00:00:00 2001 From: Jens Krause Date: Sat, 7 Dec 2019 18:59:35 +0100 Subject: [PATCH] Update Juniper example (#197) --- juniper/Cargo.toml | 15 ++++++------ juniper/README.md | 60 +++++++++++++++++++++++++++++++++++++++++++-- juniper/src/main.rs | 60 +++++++++++++++++++++------------------------ 3 files changed, 93 insertions(+), 42 deletions(-) diff --git a/juniper/Cargo.toml b/juniper/Cargo.toml index d3c02049..dbff40cd 100644 --- a/juniper/Cargo.toml +++ b/juniper/Cargo.toml @@ -1,15 +1,14 @@ [package] name = "juniper-example" -version = "0.1.0" +version = "0.2.0" authors = ["pyros2097 "] workspace = ".." edition = "2018" [dependencies] -actix-web = "1.0.0" -env_logger = "0.6" -futures = "0.1" -serde = "1.0" -serde_json = "1.0" -serde_derive = "1.0" -juniper = "0.9.2" +actix-web = "1.0.9" +env_logger = "0.7.1" +serde = "1.0.103" +serde_json = "1.0.44" +serde_derive = "1.0.103" +juniper = "0.14.1" diff --git a/juniper/README.md b/juniper/README.md index 9181ba0d..949603ca 100644 --- a/juniper/README.md +++ b/juniper/README.md @@ -1,6 +1,8 @@ -# juniper +# Juniper -Juniper integration for Actix web +[Juniper](https://github.com/graphql-rust/juniper) integration for Actix web + +## Usage ### server @@ -13,3 +15,57 @@ cargo run (or ``cargo watch -x run``) ### web client [http://127.0.0.1:8080/graphiql](http://127.0.0.1:8080/graphiql) + +_Query example:_ +```graphql +{ + human(id: "1234") { + name + appearsIn + homePlanet + } +} +``` +_Result:_ +```json +{ + "data": { + "human": { + "name": "Luke", + "appearsIn": [ + "NEW_HOPE" + ], + "homePlanet": "Mars" + } + } +} +``` + +_Mutation example:_ + +```graphql +mutation { + createHuman(newHuman: {name: "Fresh Kid Ice", appearsIn: EMPIRE, homePlanet: "earth"}) { + id + name + appearsIn + homePlanet + } +} +``` + +_Result:_ +```json +{ + "data": { + "createHuman": { + "id": "1234", + "name": "Fresh Kid Ice", + "appearsIn": [ + "EMPIRE" + ], + "homePlanet": "earth" + } + } +} +``` diff --git a/juniper/src/main.rs b/juniper/src/main.rs index 2615d34e..5b820e49 100644 --- a/juniper/src/main.rs +++ b/juniper/src/main.rs @@ -8,7 +8,6 @@ use std::sync::Arc; extern crate juniper; use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer}; -use futures::future::Future; use juniper::http::graphiql::graphiql_source; use juniper::http::GraphQLRequest; @@ -17,43 +16,40 @@ mod schema; use crate::schema::{create_schema, Schema}; fn graphiql() -> HttpResponse { - let html = graphiql_source("http://127.0.0.1:8080/graphql"); - HttpResponse::Ok() - .content_type("text/html; charset=utf-8") - .body(html) + 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>, - data: web::Json, -) -> impl Future { - web::block(move || { - let res = data.execute(&st, &()); - Ok::<_, serde_json::error::Error>(serde_json::to_string(&res)?) - }) - .map_err(Error::from) - .and_then(|user| { - Ok(HttpResponse::Ok() - .content_type("application/json") - .body(user)) - }) + st: web::Data>, + data: web::Json, +) -> Result { + let res = data.execute(&st, &()); + let user = serde_json::to_string(&res)?; + Ok( + HttpResponse::Ok() + .content_type("application/json") + .body(user) + ) } fn main() -> io::Result<()> { - std::env::set_var("RUST_LOG", "actix_web=info"); - env_logger::init(); + std::env::set_var("RUST_LOG", "actix_web=info"); + env_logger::init(); - // Create Juniper schema - let schema = std::sync::Arc::new(create_schema()); + // 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_async(graphql))) - .service(web::resource("/graphiql").route(web::get().to(graphiql))) - }) - .bind("127.0.0.1:8080")? - .run() + // Start http server + HttpServer::new(move || { + App::new() + .data(schema.clone()) + .wrap(middleware::Logger::default()) + .service(web::resource("/graphql").route(web::post().to_async(graphql))) + .service(web::resource("/graphiql").route(web::get().to(graphiql))) + }) + .bind("127.0.0.1:8080")? + .run() }