From 7a3fc52a70119bfd2c75409cea5cf694a5cad904 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 17 Feb 2022 20:37:51 +0000 Subject: [PATCH] update juniper example --- basics/middleware-http-to-https/Cargo.toml | 2 - forms/multipart-s3/Cargo.toml | 2 - graphql/juniper/Cargo.toml | 15 +++-- graphql/juniper/README.md | 11 ++-- graphql/juniper/src/main.rs | 64 ++++++++++------------ 5 files changed, 44 insertions(+), 50 deletions(-) diff --git a/basics/middleware-http-to-https/Cargo.toml b/basics/middleware-http-to-https/Cargo.toml index 5ecdbfc..de47b0b 100644 --- a/basics/middleware-http-to-https/Cargo.toml +++ b/basics/middleware-http-to-https/Cargo.toml @@ -3,8 +3,6 @@ name = "middleware-http-to-https" version = "1.0.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] actix-web = {version = "4.0.0-beta.21", features = ["rustls"]} rustls = "0.20.2" diff --git a/forms/multipart-s3/Cargo.toml b/forms/multipart-s3/Cargo.toml index a361598..b6056ee 100644 --- a/forms/multipart-s3/Cargo.toml +++ b/forms/multipart-s3/Cargo.toml @@ -3,8 +3,6 @@ name = "multipart-s3" version = "1.0.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] actix-web = "4.0.0-rc.1" actix-multipart = "0.4.0-beta.12" diff --git a/graphql/juniper/Cargo.toml b/graphql/juniper/Cargo.toml index d8be387..40244d2 100644 --- a/graphql/juniper/Cargo.toml +++ b/graphql/juniper/Cargo.toml @@ -4,10 +4,13 @@ version = "1.0.0" edition = "2021" [dependencies] -actix-web = "3" -actix-cors = "0.4.0" -env_logger = "0.8" -serde = "1.0.103" -serde_json = "1.0.44" -serde_derive = "1.0.103" +actix-web = "4.0.0-rc.3" +actix-web-lab = "0.10" +actix-cors = "0.6.0-beta.10" + juniper = "0.15" + +env_logger = "0.9" +log = "0.4" +serde = { version = "1", features = ["derive"] } +serde_json = "1" diff --git a/graphql/juniper/README.md b/graphql/juniper/README.md index 982db64..1a70f46 100644 --- a/graphql/juniper/README.md +++ b/graphql/juniper/README.md @@ -7,17 +7,16 @@ If you want more advanced example, see also the [juniper-advanced example]. ## Usage -### server +### Server -```bash +```sh cd graphql/juniper -cargo run (or ``cargo watch -x run``) -# Started http server: 127.0.0.1:8080 +cargo run ``` -### web client +### Web Client -[http://127.0.0.1:8080/graphiql](http://127.0.0.1:8080/graphiql) +Go to in your browser. _Query example:_ diff --git a/graphql/juniper/src/main.rs b/graphql/juniper/src/main.rs index 69ac0e8..3a50ab5 100644 --- a/graphql/juniper/src/main.rs +++ b/graphql/juniper/src/main.rs @@ -1,62 +1,58 @@ //! Actix Web juniper example //! //! A simple example integrating juniper in Actix Web -use std::io; -use std::sync::Arc; + +use std::{io, sync::Arc}; use actix_cors::Cors; -use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer}; -use juniper::http::graphiql::graphiql_source; -use juniper::http::GraphQLRequest; +use actix_web::{ + get, middleware, route, + web::{self, Data}, + App, HttpResponse, HttpServer, Responder, +}; +use actix_web_lab::respond::Html; +use juniper::http::{graphiql::graphiql_source, GraphQLRequest}; mod schema; use crate::schema::{create_schema, Schema}; -async fn graphiql() -> HttpResponse { - let html = graphiql_source("http://127.0.0.1:8080/graphql", None); - HttpResponse::Ok() - .content_type("text/html; charset=utf-8") - .body(html) +/// GraphiQL UI +#[get("/graphiql")] +async fn graphiql() -> impl Responder { + Html(graphiql_source("/graphql", None)) } +/// GraphQL endpoint +#[route("/graphql", method = "GET", method = "POST")] async fn graphql( - st: web::Data>, + st: web::Data, data: web::Json, -) -> Result { - let user = web::block(move || { - let res = data.execute_sync(&st, &()); - serde_json::to_string(&res) - }) - .await?; - Ok(HttpResponse::Ok() - .content_type("application/json") - .body(user)) +) -> impl Responder { + let user = data.execute(&st, &()).await; + HttpResponse::Ok().json(user) } #[actix_web::main] async fn main() -> io::Result<()> { - std::env::set_var("RUST_LOG", "actix_web=info"); - env_logger::init(); + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); // Create Juniper schema - let schema = std::sync::Arc::new(create_schema()); + let schema = Arc::new(create_schema()); - // Start http server + log::info!("starting HTTP server at http://localhost:8080"); + + // Start HTTP server HttpServer::new(move || { App::new() - .data(schema.clone()) + .app_data(Data::from(schema.clone())) + .service(graphql) + .service(graphiql) + // the graphiql UI requires CORS to be enabled + .wrap(Cors::permissive()) .wrap(middleware::Logger::default()) - .wrap( - Cors::new() - .allowed_methods(vec!["POST", "GET"]) - .supports_credentials() - .max_age(3600) - .finish(), - ) - .service(web::resource("/graphql").route(web::post().to(graphql))) - .service(web::resource("/graphiql").route(web::get().to(graphiql))) }) + .workers(2) .bind(("127.0.0.1", 8080))? .run() .await