From 7e12c01f0b257c59cef5ac404c4cf90d5aeb5e81 Mon Sep 17 00:00:00 2001 From: Robert Nelson Date: Wed, 27 Apr 2022 21:59:45 -0700 Subject: [PATCH] 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 218c8f25..b9ae5701 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 3eed302a..86312700 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 00000000..42d1a6bc --- /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 + } +}