Skip to content

Commit 739cc3b

Browse files
engylemureLegNeato
andauthored
Upgraded tokio, warp, hyper and actix (#912)
* Upgraded tokio, warp, hyper and actix * Code formatting * actix-web temporary version fix specification * Error handling fix on juniper_rocket Co-authored-by: Christian Legnitto <[email protected]>
1 parent de4c0e9 commit 739cc3b

File tree

28 files changed

+136
-119
lines changed

28 files changed

+136
-119
lines changed

docs/book/content/advanced/subscriptions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send
5151
#[graphql_subscription(context = Database)]
5252
impl Subscription {
5353
async fn hello_world() -> StringStream {
54-
let stream = tokio::stream::iter(vec![
54+
let stream = futures::stream::iter(vec![
5555
Ok(String::from("Hello")),
5656
Ok(String::from("World!"))
5757
]);
@@ -123,7 +123,7 @@ where [`Connection`][Connection] is a `Stream` of values returned by the operati
123123
# impl Subscription {
124124
# async fn hello_world() -> StringStream {
125125
# let stream =
126-
# tokio::stream::iter(vec![Ok(String::from("Hello")), Ok(String::from("World!"))]);
126+
# futures::stream::iter(vec![Ok(String::from("Hello")), Ok(String::from("World!"))]);
127127
# Box::pin(stream)
128128
# }
129129
# }

docs/book/tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ iron = "0.5"
1616
mount = "0.4"
1717
skeptic = "0.13"
1818
serde_json = "1.0"
19-
tokio = { version = "0.2", features = ["blocking", "macros", "rt-core", "rt-util", "stream"] }
19+
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
2020
uuid = "0.8"
2121

2222
[build-dependencies]

examples/actix_subscriptions/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ authors = ["Mihai Dinculescu <[email protected]>"]
66
publish = false
77

88
[dependencies]
9-
actix-web = "3.3"
10-
actix-cors = "0.5"
9+
actix-web = "4.0.0-beta.5"
10+
actix-cors = "0.6.0-beta.1"
1111
futures = "0.3"
12-
tokio = { version = "0.2", features = ["macros", "rt-core"] }
1312
env_logger = "0.8"
1413
serde = "1.0"
1514
serde_json = "1.0"
16-
rand = "0.7"
17-
15+
rand = "0.8"
16+
tokio = "1.0"
17+
async-stream = "0.3"
1818
juniper = { path = "../../juniper", features = ["expose-test-schema"] }
1919
juniper_actix = { path = "../../juniper_actix", features = ["subscriptions"] }
2020
juniper_graphql_ws = { path = "../../juniper_graphql_ws" }

examples/actix_subscriptions/src/main.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{env, pin::Pin, time::Duration};
22

33
use actix_cors::Cors;
4-
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
4+
use actix_web::{http::header, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
55

66
use juniper::{
77
graphql_object, graphql_subscription,
@@ -64,27 +64,29 @@ impl Subscription {
6464

6565
use rand::{rngs::StdRng, Rng, SeedableRng};
6666
let mut rng = StdRng::from_entropy();
67-
68-
let stream = tokio::time::interval(Duration::from_secs(3)).map(move |_| {
67+
let mut interval = tokio::time::interval(Duration::from_secs(5));
68+
let stream = async_stream::stream! {
6969
counter += 1;
70-
71-
if counter == 2 {
72-
Err(FieldError::new(
73-
"some field error from handler",
74-
Value::Scalar(DefaultScalarValue::String(
75-
"some additional string".to_string(),
76-
)),
77-
))
78-
} else {
79-
let random_id = rng.gen_range(1000, 1005).to_string();
80-
let human = context.get_human(&random_id).unwrap().clone();
81-
82-
Ok(RandomHuman {
83-
id: human.id().to_owned(),
84-
name: human.name().unwrap().to_owned(),
85-
})
70+
loop {
71+
interval.tick().await;
72+
if counter == 2 {
73+
yield Err(FieldError::new(
74+
"some field error from handler",
75+
Value::Scalar(DefaultScalarValue::String(
76+
"some additional string".to_string(),
77+
)),
78+
))
79+
} else {
80+
let random_id = rng.gen_range(1000..1005).to_string();
81+
let human = context.get_human(&random_id).unwrap().clone();
82+
83+
yield Ok(RandomHuman {
84+
id: human.id().to_owned(),
85+
name: human.name().unwrap().to_owned(),
86+
})
87+
}
8688
}
87-
});
89+
};
8890

8991
Box::pin(stream)
9092
}
@@ -117,7 +119,10 @@ async fn main() -> std::io::Result<()> {
117119
.wrap(middleware::Logger::default())
118120
.wrap(
119121
Cors::default()
122+
.allowed_origin("http://127.0.0.1:8080")
120123
.allowed_methods(vec!["POST", "GET"])
124+
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
125+
.allowed_header(header::CONTENT_TYPE)
121126
.supports_credentials()
122127
.max_age(3600),
123128
)
@@ -130,7 +135,7 @@ async fn main() -> std::io::Result<()> {
130135
.service(web::resource("/playground").route(web::get().to(playground)))
131136
.default_service(web::route().to(|| {
132137
HttpResponse::Found()
133-
.header("location", "/playground")
138+
.append_header((header::LOCATION, "/playground"))
134139
.finish()
135140
}))
136141
})

examples/basic_subscriptions/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ authors = ["Jordao Rosario <[email protected]>"]
1111
futures = "0.3"
1212
serde = { version = "1.0", features = ["derive"] }
1313
serde_json = "1.0"
14-
tokio = { version = "0.2", features = ["rt-core", "macros", "stream"] }
14+
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
1515

1616
juniper = { path = "../../juniper" }
1717
juniper_subscriptions = { path = "../../juniper_subscriptions" }

examples/basic_subscriptions/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send
3737
impl Subscription {
3838
async fn hello_world() -> StringStream {
3939
let stream =
40-
tokio::stream::iter(vec![Ok(String::from("Hello")), Ok(String::from("World!"))]);
40+
futures::stream::iter(vec![Ok(String::from("Hello")), Ok(String::from("World!"))]);
4141
Box::pin(stream)
4242
}
4343
}

examples/warp_async/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ env_logger = "0.8.1"
1313
futures = "0.3.1"
1414
log = "0.4.8"
1515
reqwest = { version = "0.11", features = ["rustls-tls"] }
16-
tokio = { version = "0.2", features = ["rt-core", "macros"] }
17-
warp = "0.2"
16+
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
17+
warp = "0.3"

examples/warp_subscriptions/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ futures = "0.3.1"
1010
log = "0.4.8"
1111
serde = { version = "1.0", features = ["derive"] }
1212
serde_json = "1.0"
13-
tokio = { version = "0.2", features = ["rt-core", "macros"] }
14-
warp = "0.2.1"
15-
13+
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
14+
warp = "0.3"
15+
async-stream = "0.3"
1616
juniper = { path = "../../juniper" }
1717
juniper_graphql_ws = { path = "../../juniper_graphql_ws" }
1818
juniper_warp = { path = "../../juniper_warp", features = ["subscriptions"] }

examples/warp_subscriptions/src/main.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,27 @@ struct Subscription;
109109
impl Subscription {
110110
async fn users() -> UsersStream {
111111
let mut counter = 0;
112-
let stream = tokio::time::interval(Duration::from_secs(5)).map(move |_| {
112+
let mut interval = tokio::time::interval(Duration::from_secs(5));
113+
let stream = async_stream::stream! {
113114
counter += 1;
114-
if counter == 2 {
115-
Err(FieldError::new(
116-
"some field error from handler",
117-
Value::Scalar(DefaultScalarValue::String(
118-
"some additional string".to_string(),
119-
)),
120-
))
121-
} else {
122-
Ok(User {
123-
id: counter,
124-
kind: UserKind::Admin,
125-
name: "stream user".to_string(),
126-
})
115+
loop {
116+
interval.tick().await;
117+
if counter == 2 {
118+
yield Err(FieldError::new(
119+
"some field error from handler",
120+
Value::Scalar(DefaultScalarValue::String(
121+
"some additional string".to_string(),
122+
)),
123+
))
124+
} else {
125+
yield Ok(User {
126+
id: counter,
127+
kind: UserKind::Admin,
128+
name: "stream user".to_string(),
129+
})
130+
}
127131
}
128-
});
132+
};
129133

130134
Box::pin(stream)
131135
}

integration_tests/async_await/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ publish = false
88
[dependencies]
99
juniper = { path = "../../juniper" }
1010
futures = "0.3.1"
11-
tokio = { version = "0.2", features = ["rt-core", "time", "macros"] }
11+
tokio = { version = "1", features = ["rt", "time", "macros"] }

0 commit comments

Comments
 (0)