use crate::{registry, ContextSelectionSet, GQLInputValue, GQLOutputValue, GQLType, Result, Value}; use std::borrow::Cow; impl GQLType for Option { fn type_name() -> Cow<'static, str> { T::type_name() } fn qualified_type_name() -> String { T::type_name().to_string() } fn create_type_info(registry: &mut registry::Registry) -> String { T::create_type_info(registry); T::type_name().to_string() } } impl GQLInputValue for Option { fn parse(value: &Value) -> Option { match value { Value::Null => Some(None), _ => Some(GQLInputValue::parse(value)?), } } } #[async_trait::async_trait] impl GQLOutputValue for Option { async fn resolve(&self, ctx: &ContextSelectionSet<'_>) -> Result where { if let Some(inner) = self { inner.resolve(ctx).await } else { Ok(serde_json::Value::Null) } } } impl GQLType for &Option { fn type_name() -> Cow<'static, str> { T::type_name() } fn qualified_type_name() -> String { T::type_name().to_string() } fn create_type_info(registry: &mut registry::Registry) -> String { T::create_type_info(registry); T::type_name().to_string() } } #[async_trait::async_trait] impl GQLOutputValue for &Option { async fn resolve(&self, ctx: &ContextSelectionSet<'_>) -> Result where { if let Some(inner) = self { inner.resolve(ctx).await } else { Ok(serde_json::Value::Null) } } } #[cfg(test)] mod tests { use crate::GQLType; #[test] fn test_optional_type() { assert_eq!(Option::::type_name(), "Int"); assert_eq!(Option::::qualified_type_name(), "Int"); assert_eq!(&Option::::type_name(), "Int"); assert_eq!(&Option::::qualified_type_name(), "Int"); } }