use crate::{registry, Context, ContextSelectionSet, Result}; use graphql_parser::query::{Field, Value}; use std::borrow::Cow; pub trait GQLType { fn type_name() -> Cow<'static, str>; fn qualified_type_name() -> String { format!("{}!", Self::type_name()) } fn create_type_info(registry: &mut registry::Registry) -> String; } pub trait GQLInputValue: GQLType + Sized { fn parse(value: &Value) -> Option; } #[async_trait::async_trait] pub trait GQLOutputValue: GQLType { async fn resolve(value: &Self, ctx: &ContextSelectionSet<'_>) -> Result; } #[async_trait::async_trait] pub trait GQLObject: GQLOutputValue { fn is_empty() -> bool { return false; } async fn resolve_field(&self, ctx: &Context<'_>, field: &Field) -> Result; async fn resolve_inline_fragment( &self, name: &str, ctx: &ContextSelectionSet<'_>, result: &mut serde_json::Map, ) -> Result<()>; } pub trait GQLInputObject: GQLInputValue {} pub trait GQLScalar: Sized + Send { fn type_name() -> &'static str; fn description() -> Option<&'static str> { None } fn parse(value: &Value) -> Option; fn to_json(&self) -> Result; } #[macro_export] macro_rules! impl_scalar { ($ty:ty) => { impl crate::GQLType for $ty { fn type_name() -> std::borrow::Cow<'static, str> { std::borrow::Cow::Borrowed(<$ty as crate::GQLScalar>::type_name()) } fn create_type_info(registry: &mut crate::registry::Registry) -> String { registry.create_type::<$ty, _>(|_| crate::registry::Type::Scalar { name: <$ty as crate::GQLScalar>::type_name().to_string(), description: <$ty>::description(), }) } } impl crate::GQLType for &$ty { fn type_name() -> std::borrow::Cow<'static, str> { std::borrow::Cow::Borrowed(<$ty as crate::GQLScalar>::type_name()) } fn create_type_info(registry: &mut crate::registry::Registry) -> String { registry.create_type::<$ty, _>(|_| crate::registry::Type::Scalar { name: <$ty as crate::GQLScalar>::type_name().to_string(), description: <$ty>::description(), }) } } impl crate::GQLInputValue for $ty { fn parse(value: &crate::Value) -> Option { <$ty as crate::GQLScalar>::parse(value) } } #[async_trait::async_trait] impl crate::GQLOutputValue for $ty { async fn resolve( value: &Self, _: &crate::ContextSelectionSet<'_>, ) -> crate::Result { value.to_json() } } #[async_trait::async_trait] impl crate::GQLOutputValue for &$ty { async fn resolve( value: &Self, _: &crate::ContextSelectionSet<'_>, ) -> crate::Result { value.to_json() } } }; } #[async_trait::async_trait] impl GQLOutputValue for T { async fn resolve(value: &Self, ctx: &ContextSelectionSet<'_>) -> Result { let mut result = serde_json::Map::::new(); crate::resolver::do_resolve(ctx, value, &mut result).await?; Ok(result.into()) } }