Skip to content

Commit 168114f

Browse files
committed
Use async Mutex in juniper_actix to fix latest nightly errors
1 parent a8759b0 commit 168114f

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

juniper/src/tests/fixtures/starwars/schema.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub struct Human {
7878
name: String,
7979
friend_ids: Vec<String>,
8080
appears_in: Vec<Episode>,
81+
#[allow(dead_code)]
8182
secret_backstory: Option<String>,
8283
home_planet: Option<String>,
8384
}
@@ -164,6 +165,7 @@ pub struct Droid {
164165
name: String,
165166
friend_ids: Vec<String>,
166167
appears_in: Vec<Episode>,
168+
#[allow(dead_code)]
167169
secret_backstory: Option<String>,
168170
primary_function: Option<String>,
169171
}

juniper_actix/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ documentation = "https://docs.rs/juniper_actix"
99
repository = "https://github.com/graphql-rust/juniper"
1010

1111
[features]
12-
subscriptions = ["juniper_graphql_ws"]
12+
subscriptions = ["juniper_graphql_ws", "tokio"]
1313

1414
[dependencies]
1515
actix = "0.12"
@@ -26,12 +26,13 @@ futures = "0.3"
2626
serde = { version = "1.0", features = ["derive"] }
2727
serde_json = "1.0"
2828
thiserror = "1.0"
29+
tokio = { version = "1.0", features = ["sync"], optional = true }
2930

3031
[dev-dependencies]
3132
actix-rt = "2"
3233
actix-cors = "0.6.0-beta.2"
3334
actix-identity = "0.4.0-beta.2"
34-
tokio = "1"
35+
tokio = "1.0"
3536
async-stream = "0.3"
3637
actix-test = "0.1.0-beta.3"
3738

juniper_actix/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,14 @@ pub async fn playground_handler(
211211
/// [1]: https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md
212212
#[cfg(feature = "subscriptions")]
213213
pub mod subscriptions {
214-
use std::{
215-
fmt,
216-
sync::{Arc, Mutex},
217-
};
214+
use std::{fmt, sync::Arc};
218215

219216
use actix::{prelude::*, Actor, StreamHandler};
220217
use actix_web::{
221218
http::header::{HeaderName, HeaderValue},
222219
web, HttpRequest, HttpResponse,
223220
};
224221
use actix_web_actors::ws;
225-
226222
use juniper::{
227223
futures::{
228224
stream::{SplitSink, SplitStream, StreamExt},
@@ -231,6 +227,7 @@ pub mod subscriptions {
231227
GraphQLSubscriptionType, GraphQLTypeAsync, RootNode, ScalarValue,
232228
};
233229
use juniper_graphql_ws::{ArcSchema, ClientMessage, Connection, Init, ServerMessage};
230+
use tokio::sync::Mutex;
234231

235232
/// Serves the graphql-ws protocol over a WebSocket connection.
236233
///
@@ -326,8 +323,9 @@ pub mod subscriptions {
326323
let tx = self.graphql_tx.clone();
327324

328325
async move {
329-
let mut tx = tx.lock().unwrap();
330-
tx.send(msg)
326+
tx.lock()
327+
.await
328+
.send(msg)
331329
.await
332330
.expect("Infallible: this should not happen");
333331
}
@@ -363,7 +361,7 @@ pub mod subscriptions {
363361
let addr = ctx.address();
364362

365363
let fut = async move {
366-
let mut stream = stream.lock().unwrap();
364+
let mut stream = stream.lock().await;
367365
while let Some(message) = stream.next().await {
368366
// sending the message to self so that it can be forwarded back to the client
369367
addr.do_send(ServerMessageWrapper { message });

0 commit comments

Comments
 (0)