From d70daee130dcec2ebc1ff91bfb17bbe37404a67c Mon Sep 17 00:00:00 2001 From: Robert Nelson Date: Tue, 19 Apr 2022 10:57:57 -0700 Subject: [PATCH 1/2] Implemented OutputType for tokio RwLock --- Cargo.toml | 2 ++ src/types/external/mod.rs | 1 + src/types/external/tokio/mod.rs | 1 + src/types/external/tokio/sync/mod.rs | 2 ++ src/types/external/tokio/sync/rw_lock.rs | 26 ++++++++++++++++++++++++ 5 files changed, 32 insertions(+) create mode 100644 src/types/external/tokio/mod.rs create mode 100644 src/types/external/tokio/sync/mod.rs create mode 100644 src/types/external/tokio/sync/rw_lock.rs diff --git a/Cargo.toml b/Cargo.toml index ace952fca..218c8f25a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ decimal = ["rust_decimal"] cbor = ["serde_cbor"] chrono-duration = ["chrono", "iso8601-duration"] password-strength-validator = ["zxcvbn"] +tokio-rw-lock = ["tokio"] [dependencies] async-graphql-derive = { path = "derive", version = "3.0.38" } @@ -66,6 +67,7 @@ rust_decimal = { version = "1.14.3", optional = true } url = { version = "2.2.1", optional = true } smol_str = { version = "0.1.21", optional = true } time = { version = "0.3.5", optional = true, features = ["parsing", "formatting", "macros"] } +tokio = { version = "1.17.0", features = ["sync"], optional = true } # Non-feature optional dependencies blocking = { version = "1.0.2", optional = true } diff --git a/src/types/external/mod.rs b/src/types/external/mod.rs index 3082e3045..0f164ef9b 100644 --- a/src/types/external/mod.rs +++ b/src/types/external/mod.rs @@ -11,6 +11,7 @@ mod list; mod non_zero_integers; mod optional; mod string; +mod tokio; #[cfg(feature = "bson")] mod bson; diff --git a/src/types/external/tokio/mod.rs b/src/types/external/tokio/mod.rs new file mode 100644 index 000000000..d086d5bd6 --- /dev/null +++ b/src/types/external/tokio/mod.rs @@ -0,0 +1 @@ +pub mod sync; diff --git a/src/types/external/tokio/sync/mod.rs b/src/types/external/tokio/sync/mod.rs new file mode 100644 index 000000000..3eed302ab --- /dev/null +++ b/src/types/external/tokio/sync/mod.rs @@ -0,0 +1,2 @@ +#[cfg(feature = "tokio-rw-lock")] +mod rw_lock; diff --git a/src/types/external/tokio/sync/rw_lock.rs b/src/types/external/tokio/sync/rw_lock.rs new file mode 100644 index 000000000..de28940b5 --- /dev/null +++ b/src/types/external/tokio/sync/rw_lock.rs @@ -0,0 +1,26 @@ +use tokio::sync::RwLock; +use std::borrow::Cow; + +use async_graphql_parser::types::Field; + +use crate::{registry, ContextSelectionSet, OutputType, Positioned, ServerResult, Value}; + +#[async_trait::async_trait] +impl OutputType for RwLock +{ + fn type_name() -> Cow<'static, str> { + T::type_name() + } + + fn create_type_info(registry: &mut registry::Registry) -> String { + ::create_type_info(registry) + } + + async fn resolve( + &self, + ctx: &ContextSelectionSet<'_>, + field: &Positioned, + ) -> ServerResult { + self.read().await.resolve(ctx, field).await + } +} From d0b9c949e5871d942404dff66516950d5ed47a64 Mon Sep 17 00:00:00 2001 From: Robert Nelson Date: Wed, 27 Apr 2022 21:59:45 -0700 Subject: [PATCH 2/2] Implemented OutputType for tokio Mutex --- Cargo.toml | 2 +- src/types/external/tokio/sync/mod.rs | 4 +++- src/types/external/tokio/sync/mutex.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/types/external/tokio/sync/mutex.rs diff --git a/Cargo.toml b/Cargo.toml index 218c8f25a..b9ae5701d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ decimal = ["rust_decimal"] cbor = ["serde_cbor"] chrono-duration = ["chrono", "iso8601-duration"] password-strength-validator = ["zxcvbn"] -tokio-rw-lock = ["tokio"] +tokio-sync = ["tokio"] [dependencies] async-graphql-derive = { path = "derive", version = "3.0.38" } diff --git a/src/types/external/tokio/sync/mod.rs b/src/types/external/tokio/sync/mod.rs index 3eed302ab..86312700e 100644 --- a/src/types/external/tokio/sync/mod.rs +++ b/src/types/external/tokio/sync/mod.rs @@ -1,2 +1,4 @@ -#[cfg(feature = "tokio-rw-lock")] +#[cfg(feature = "tokio-sync")] mod rw_lock; +#[cfg(feature = "tokio-sync")] +mod mutex; diff --git a/src/types/external/tokio/sync/mutex.rs b/src/types/external/tokio/sync/mutex.rs new file mode 100644 index 000000000..42d1a6bc9 --- /dev/null +++ b/src/types/external/tokio/sync/mutex.rs @@ -0,0 +1,26 @@ +use tokio::sync::Mutex; +use std::borrow::Cow; + +use async_graphql_parser::types::Field; + +use crate::{registry, ContextSelectionSet, OutputType, Positioned, ServerResult, Value}; + +#[async_trait::async_trait] +impl OutputType for Mutex +{ + fn type_name() -> Cow<'static, str> { + T::type_name() + } + + fn create_type_info(registry: &mut registry::Registry) -> String { + ::create_type_info(registry) + } + + async fn resolve( + &self, + ctx: &ContextSelectionSet<'_>, + field: &Positioned, + ) -> ServerResult { + self.lock().await.resolve(ctx, field).await + } +}