use crate::{registry, ContextSelectionSet, Result}; use graphql_parser::query::Value; use std::borrow::Cow; #[doc(hidden)] 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; } #[doc(hidden)] pub trait GQLInputValue: GQLType + Sized { fn parse(value: &Value) -> Option; fn parse_from_json(value: &serde_json::Value) -> Option; } #[doc(hidden)] #[async_trait::async_trait] pub trait GQLOutputValue: GQLType { async fn resolve(&self, ctx: &ContextSelectionSet<'_>) -> Result; } #[doc(hidden)] pub trait GQLObject: GQLOutputValue {} #[doc(hidden)] pub trait GQLInputObject: GQLInputValue {} pub trait Scalar: Sized + Send { fn type_name() -> &'static str; fn description() -> Option<&'static str> { None } fn parse(value: &Value) -> Option; fn parse_from_json(value: &serde_json::Value) -> Option; fn to_json(&self) -> Result; } impl GQLType for T { fn type_name() -> Cow<'static, str> { Cow::Borrowed(T::type_name()) } fn create_type_info(registry: &mut registry::Registry) -> String { registry.create_type(&T::type_name(), |_| registry::Type::Scalar { name: T::type_name().to_string(), description: T::description(), }) } } impl GQLInputValue for T { fn parse(value: &Value) -> Option { T::parse(value) } fn parse_from_json(value: &serde_json::Value) -> Option { T::parse_from_json(value) } } #[async_trait::async_trait] impl GQLOutputValue for T { async fn resolve(&self, _: &ContextSelectionSet<'_>) -> Result { T::to_json(self) } }