1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

update juniper example

This commit is contained in:
Rob Ede 2022-02-17 20:37:51 +00:00
parent 7857ac65f8
commit 7a3fc52a70
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
5 changed files with 44 additions and 50 deletions

View File

@ -3,8 +3,6 @@ name = "middleware-http-to-https"
version = "1.0.0" version = "1.0.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
actix-web = {version = "4.0.0-beta.21", features = ["rustls"]} actix-web = {version = "4.0.0-beta.21", features = ["rustls"]}
rustls = "0.20.2" rustls = "0.20.2"

View File

@ -3,8 +3,6 @@ name = "multipart-s3"
version = "1.0.0" version = "1.0.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
actix-web = "4.0.0-rc.1" actix-web = "4.0.0-rc.1"
actix-multipart = "0.4.0-beta.12" actix-multipart = "0.4.0-beta.12"

View File

@ -4,10 +4,13 @@ version = "1.0.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
actix-web = "3" actix-web = "4.0.0-rc.3"
actix-cors = "0.4.0" actix-web-lab = "0.10"
env_logger = "0.8" actix-cors = "0.6.0-beta.10"
serde = "1.0.103"
serde_json = "1.0.44"
serde_derive = "1.0.103"
juniper = "0.15" juniper = "0.15"
env_logger = "0.9"
log = "0.4"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

View File

@ -7,17 +7,16 @@ If you want more advanced example, see also the [juniper-advanced example].
## Usage ## Usage
### server ### Server
```bash ```sh
cd graphql/juniper cd graphql/juniper
cargo run (or ``cargo watch -x run``) cargo run
# Started http server: 127.0.0.1:8080
``` ```
### web client ### Web Client
[http://127.0.0.1:8080/graphiql](http://127.0.0.1:8080/graphiql) Go to <http://localhost:8080/graphiql> in your browser.
_Query example:_ _Query example:_

View File

@ -1,62 +1,58 @@
//! Actix Web juniper example //! Actix Web juniper example
//! //!
//! A simple example integrating juniper in Actix Web //! 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_cors::Cors;
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer}; use actix_web::{
use juniper::http::graphiql::graphiql_source; get, middleware, route,
use juniper::http::GraphQLRequest; web::{self, Data},
App, HttpResponse, HttpServer, Responder,
};
use actix_web_lab::respond::Html;
use juniper::http::{graphiql::graphiql_source, GraphQLRequest};
mod schema; mod schema;
use crate::schema::{create_schema, Schema}; use crate::schema::{create_schema, Schema};
async fn graphiql() -> HttpResponse { /// GraphiQL UI
let html = graphiql_source("http://127.0.0.1:8080/graphql", None); #[get("/graphiql")]
HttpResponse::Ok() async fn graphiql() -> impl Responder {
.content_type("text/html; charset=utf-8") Html(graphiql_source("/graphql", None))
.body(html)
} }
/// GraphQL endpoint
#[route("/graphql", method = "GET", method = "POST")]
async fn graphql( async fn graphql(
st: web::Data<Arc<Schema>>, st: web::Data<Schema>,
data: web::Json<GraphQLRequest>, data: web::Json<GraphQLRequest>,
) -> Result<HttpResponse, Error> { ) -> impl Responder {
let user = web::block(move || { let user = data.execute(&st, &()).await;
let res = data.execute_sync(&st, &()); HttpResponse::Ok().json(user)
serde_json::to_string(&res)
})
.await?;
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(user))
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> io::Result<()> { async fn main() -> io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
env_logger::init();
// Create Juniper schema // 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 || { HttpServer::new(move || {
App::new() 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(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))? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await