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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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<Self>;
}

#[async_trait::async_trait]
pub trait GQLOutputValue: GQLType {
    async fn resolve(value: &Self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value>;
}

#[async_trait::async_trait]
pub trait GQLObject: GQLOutputValue {
    fn is_empty() -> bool {
        return false;
    }

    async fn resolve_field(&self, ctx: &Context<'_>, field: &Field) -> Result<serde_json::Value>;

    async fn resolve_inline_fragment(
        &self,
        name: &str,
        ctx: &ContextSelectionSet<'_>,
        result: &mut serde_json::Map<String, serde_json::Value>,
    ) -> 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<Self>;
    fn to_json(&self) -> Result<serde_json::Value>;
}

#[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::GQLInputValue for $ty {
            fn parse(value: &crate::Value) -> Option<Self> {
                <$ty as crate::GQLScalar>::parse(value)
            }
        }

        #[async_trait::async_trait]
        impl crate::GQLOutputValue for $ty {
            async fn resolve(
                value: &Self,
                _: &crate::ContextSelectionSet<'_>,
            ) -> crate::Result<serde_json::Value> {
                value.to_json()
            }
        }
    };
}

#[async_trait::async_trait]
impl<T: GQLObject + Send + Sync> GQLOutputValue for T {
    async fn resolve(value: &Self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value> {
        let mut result = serde_json::Map::<String, serde_json::Value>::new();
        crate::resolver::do_resolve(ctx, value, &mut result).await?;
        Ok(result.into())
    }
}