1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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<Self>; fn parse_from_json(value: serde_json::Value) -> Option<Self>; } #[doc(hidden)] #[async_trait::async_trait] pub trait GQLOutputValue: GQLType { async fn resolve(&self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value>; } #[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<Self>; fn parse_from_json(value: serde_json::Value) -> Option<Self>; fn to_json(&self) -> Result<serde_json::Value>; } impl<T: Scalar> 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<T: Scalar> GQLInputValue for T { fn parse(value: Value) -> Option<Self> { T::parse(value) } fn parse_from_json(value: serde_json::Value) -> Option<Self> { T::parse_from_json(value) } } #[async_trait::async_trait] impl<T: Scalar + Sync> GQLOutputValue for T { async fn resolve(&self, _: &ContextSelectionSet<'_>) -> Result<serde_json::Value> { T::to_json(self) } }