Upgrade reqwest
to 0.10 version and use rustls
to remove transitive OpenSSL (#677)
- revive `warp_async` example (#659) Co-authored-by: Allan Calix <contact@allancalix.com> Co-authored-by: Kai Ren <tyranron@gmail.com>
This commit is contained in:
parent
6dd6abbadc
commit
f19d498254
4 changed files with 39 additions and 51 deletions
|
@ -1,20 +1,16 @@
|
||||||
[package]
|
[package]
|
||||||
name = "warp_async"
|
name = "warp_async"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Christoph Herzog <chris@theduke.at>"]
|
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
authors = ["Christoph Herzog <chris@theduke.at>"]
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
log = "0.4.8"
|
juniper = { git = "https://github.com/graphql-rust/juniper" }
|
||||||
|
juniper_warp = { git = "https://github.com/graphql-rust/juniper" }
|
||||||
|
|
||||||
env_logger = "0.6.2"
|
env_logger = "0.6.2"
|
||||||
warp = "0.1.19"
|
|
||||||
futures = { version = "0.3.1", features = ["compat"] }
|
futures = { version = "0.3.1", features = ["compat"] }
|
||||||
reqwest = "0.9.19"
|
log = "0.4.8"
|
||||||
|
reqwest = { version = "0.10", features = ["rustls-tls"] }
|
||||||
tokio = { version = "0.2", features = ["rt-core", "macros"] }
|
tokio = { version = "0.2", features = ["rt-core", "macros"] }
|
||||||
|
warp = "0.2"
|
||||||
juniper_codegen = { git = "https://github.com/graphql-rust/juniper", branch = "async-await", features = ["async"] }
|
|
||||||
juniper = { git = "https://github.com/graphql-rust/juniper", branch = "async-await", features = ["async"] }
|
|
||||||
juniper_warp = { git = "https://github.com/graphql-rust/juniper", branch = "async-await", features = ["async"] }
|
|
||||||
|
|
||||||
|
|
|
@ -1,30 +1,29 @@
|
||||||
//!
|
|
||||||
//! This example demonstrates async/await usage with warp.
|
//! This example demonstrates async/await usage with warp.
|
||||||
//! NOTE: this uses tokio 0.1 , not the alpha tokio 0.2.
|
|
||||||
|
|
||||||
use juniper::{EmptyMutation, EmptySubscription, RootNode, FieldError};
|
use juniper::{
|
||||||
|
graphql_object, EmptyMutation, EmptySubscription, FieldError, GraphQLEnum, RootNode,
|
||||||
|
};
|
||||||
use warp::{http::Response, Filter};
|
use warp::{http::Response, Filter};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
struct Context {
|
struct Context;
|
||||||
|
|
||||||
}
|
|
||||||
impl juniper::Context for Context {}
|
impl juniper::Context for Context {}
|
||||||
|
|
||||||
#[derive(juniper::GraphQLEnum, Clone, Copy)]
|
#[derive(Clone, Copy, Debug, GraphQLEnum)]
|
||||||
enum UserKind {
|
enum UserKind {
|
||||||
Admin,
|
Admin,
|
||||||
User,
|
User,
|
||||||
Guest,
|
Guest,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
struct User {
|
struct User {
|
||||||
id: i32,
|
id: i32,
|
||||||
kind: UserKind,
|
kind: UserKind,
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[juniper::graphql_object(Context = Context)]
|
#[graphql_object(Context = Context)]
|
||||||
impl User {
|
impl User {
|
||||||
fn id(&self) -> i32 {
|
fn id(&self) -> i32 {
|
||||||
self.id
|
self.id
|
||||||
|
@ -43,45 +42,38 @@ impl User {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
struct Query;
|
struct Query;
|
||||||
|
|
||||||
#[juniper::graphql_object(Context = Context)]
|
#[graphql_object(Context = Context)]
|
||||||
impl Query {
|
impl Query {
|
||||||
async fn users() -> Vec<User> {
|
async fn users() -> Vec<User> {
|
||||||
vec![
|
vec![User {
|
||||||
User{
|
|
||||||
id: 1,
|
id: 1,
|
||||||
kind: UserKind::Admin,
|
kind: UserKind::Admin,
|
||||||
name: "user1".into(),
|
name: "user1".into(),
|
||||||
},
|
}]
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch a URL and return the response body text.
|
/// Fetch a URL and return the response body text.
|
||||||
async fn request(url: String) -> Result<String, FieldError> {
|
async fn request(url: String) -> Result<String, FieldError> {
|
||||||
use futures::{ compat::{Stream01CompatExt, Future01CompatExt}, stream::TryStreamExt};
|
Ok(reqwest::get(&url).await?.text().await?)
|
||||||
|
|
||||||
let res = reqwest::r#async::Client::new()
|
|
||||||
.get(&url)
|
|
||||||
.send()
|
|
||||||
.compat()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let body_raw = res.into_body().compat().try_concat().await?;
|
|
||||||
let body = std::str::from_utf8(&body_raw).unwrap_or("invalid utf8");
|
|
||||||
Ok(body.to_string())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Schema = RootNode<'static, Query, EmptyMutation<Context>, EmptySubscription<Context>>;
|
type Schema = RootNode<'static, Query, EmptyMutation<Context>, EmptySubscription<Context>>;
|
||||||
|
|
||||||
fn schema() -> Schema {
|
fn schema() -> Schema {
|
||||||
Schema::new(Query, EmptyMutation::<Context>::new(), EmptySubscription::<Context>::new())
|
Schema::new(
|
||||||
|
Query,
|
||||||
|
EmptyMutation::<Context>::new(),
|
||||||
|
EmptySubscription::<Context>::new(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
::std::env::set_var("RUST_LOG", "warp_async");
|
std::env::set_var("RUST_LOG", "warp_async");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let log = warp::log("warp_server");
|
let log = warp::log("warp_server");
|
||||||
|
@ -96,13 +88,13 @@ async fn main() {
|
||||||
|
|
||||||
log::info!("Listening on 127.0.0.1:8080");
|
log::info!("Listening on 127.0.0.1:8080");
|
||||||
|
|
||||||
let state = warp::any().map(move || Context{} );
|
let state = warp::any().map(|| Context);
|
||||||
let graphql_filter = juniper_warp::make_graphql_filter(schema(), state.boxed());
|
let graphql_filter = juniper_warp::make_graphql_filter(schema(), state.boxed());
|
||||||
|
|
||||||
warp::serve(
|
warp::serve(
|
||||||
warp::get()
|
warp::get()
|
||||||
.and(warp::path("graphiql"))
|
.and(warp::path("graphiql"))
|
||||||
.and(juniper_warp::graphiql_filter("/graphql"))
|
.and(juniper_warp::graphiql_filter("/graphql", None))
|
||||||
.or(homepage)
|
.or(homepage)
|
||||||
.or(warp::path("graphql").and(graphql_filter))
|
.or(warp::path("graphql").and(graphql_filter))
|
||||||
.with(log),
|
.with(log),
|
||||||
|
|
|
@ -14,11 +14,11 @@ url = "2"
|
||||||
juniper = { version = "0.14.2", default-features = false, path = "../juniper"}
|
juniper = { version = "0.14.2", default-features = false, path = "../juniper"}
|
||||||
tokio = "0.2"
|
tokio = "0.2"
|
||||||
hyper = "0.13"
|
hyper = "0.13"
|
||||||
futures = { version = "0.3.1" }
|
futures = "0.3.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pretty_env_logger = "0.2"
|
pretty_env_logger = "0.2"
|
||||||
reqwest = "0.9"
|
reqwest = { version = "0.10", features = ["blocking", "rustls-tls"] }
|
||||||
|
|
||||||
[dev-dependencies.juniper]
|
[dev-dependencies.juniper]
|
||||||
version = "0.14.2"
|
version = "0.14.2"
|
||||||
|
|
|
@ -322,7 +322,7 @@ mod tests {
|
||||||
tests::{model::Database, schema::Query},
|
tests::{model::Database, schema::Query},
|
||||||
EmptyMutation, EmptySubscription, RootNode,
|
EmptyMutation, EmptySubscription, RootNode,
|
||||||
};
|
};
|
||||||
use reqwest::{self, Response as ReqwestResponse};
|
use reqwest::{self, blocking::Response as ReqwestResponse};
|
||||||
use std::{net::SocketAddr, sync::Arc, thread, time::Duration};
|
use std::{net::SocketAddr, sync::Arc, thread, time::Duration};
|
||||||
|
|
||||||
struct TestHyperIntegration {
|
struct TestHyperIntegration {
|
||||||
|
@ -332,12 +332,12 @@ mod tests {
|
||||||
impl http_tests::HttpIntegration for TestHyperIntegration {
|
impl http_tests::HttpIntegration for TestHyperIntegration {
|
||||||
fn get(&self, url: &str) -> http_tests::TestResponse {
|
fn get(&self, url: &str) -> http_tests::TestResponse {
|
||||||
let url = format!("http://127.0.0.1:{}/graphql{}", self.port, url);
|
let url = format!("http://127.0.0.1:{}/graphql{}", self.port, url);
|
||||||
make_test_response(reqwest::get(&url).expect(&format!("failed GET {}", url)))
|
make_test_response(reqwest::blocking::get(&url).expect(&format!("failed GET {}", url)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn post_json(&self, url: &str, body: &str) -> http_tests::TestResponse {
|
fn post_json(&self, url: &str, body: &str) -> http_tests::TestResponse {
|
||||||
let url = format!("http://127.0.0.1:{}/graphql{}", self.port, url);
|
let url = format!("http://127.0.0.1:{}/graphql{}", self.port, url);
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::blocking::Client::new();
|
||||||
let res = client
|
let res = client
|
||||||
.post(&url)
|
.post(&url)
|
||||||
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
.header(reqwest::header::CONTENT_TYPE, "application/json")
|
||||||
|
@ -349,7 +349,7 @@ mod tests {
|
||||||
|
|
||||||
fn post_graphql(&self, url: &str, body: &str) -> http_tests::TestResponse {
|
fn post_graphql(&self, url: &str, body: &str) -> http_tests::TestResponse {
|
||||||
let url = format!("http://127.0.0.1:{}/graphql{}", self.port, url);
|
let url = format!("http://127.0.0.1:{}/graphql{}", self.port, url);
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::blocking::Client::new();
|
||||||
let res = client
|
let res = client
|
||||||
.post(&url)
|
.post(&url)
|
||||||
.header(reqwest::header::CONTENT_TYPE, "application/graphql")
|
.header(reqwest::header::CONTENT_TYPE, "application/graphql")
|
||||||
|
@ -360,15 +360,15 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_test_response(mut response: ReqwestResponse) -> http_tests::TestResponse {
|
fn make_test_response(response: ReqwestResponse) -> http_tests::TestResponse {
|
||||||
let status_code = response.status().as_u16() as i32;
|
let status_code = response.status().as_u16() as i32;
|
||||||
let body = response.text().unwrap();
|
|
||||||
let content_type_header = response.headers().get(reqwest::header::CONTENT_TYPE);
|
let content_type_header = response.headers().get(reqwest::header::CONTENT_TYPE);
|
||||||
let content_type = if let Some(ct) = content_type_header {
|
let content_type = if let Some(ct) = content_type_header {
|
||||||
format!("{}", ct.to_str().unwrap())
|
format!("{}", ct.to_str().unwrap())
|
||||||
} else {
|
} else {
|
||||||
String::default()
|
String::default()
|
||||||
};
|
};
|
||||||
|
let body = response.text().unwrap();
|
||||||
|
|
||||||
http_tests::TestResponse {
|
http_tests::TestResponse {
|
||||||
status_code,
|
status_code,
|
||||||
|
|
Loading…
Reference in a new issue