async_graphql/dynamic/
type.rs

1use crate::{
2    dynamic::{Enum, InputObject, Interface, Object, Scalar, SchemaError, Subscription, Union},
3    registry::Registry,
4    Upload,
5};
6
7/// A GraphQL type
8#[derive(Debug)]
9pub enum Type {
10    /// Scalar
11    Scalar(Scalar),
12    /// Object
13    Object(Object),
14    /// Input object
15    InputObject(InputObject),
16    /// Enum
17    Enum(Enum),
18    /// Interface
19    Interface(Interface),
20    /// Union
21    Union(Union),
22    /// Subscription
23    Subscription(Subscription),
24    /// Upload
25    Upload,
26}
27
28impl Type {
29    pub(crate) fn name(&self) -> &str {
30        match self {
31            Type::Scalar(scalar) => &scalar.name,
32            Type::Object(object) => &object.name,
33            Type::InputObject(input_object) => &input_object.name,
34            Type::Enum(e) => &e.name,
35            Type::Interface(interface) => &interface.name,
36            Type::Union(union) => &union.name,
37            Type::Subscription(subscription) => &subscription.name,
38            Type::Upload => "Upload",
39        }
40    }
41
42    #[inline]
43    pub(crate) fn as_object(&self) -> Option<&Object> {
44        if let Type::Object(obj) = self {
45            Some(obj)
46        } else {
47            None
48        }
49    }
50
51    #[inline]
52    pub(crate) fn as_interface(&self) -> Option<&Interface> {
53        if let Type::Interface(interface) = self {
54            Some(interface)
55        } else {
56            None
57        }
58    }
59
60    #[inline]
61    pub(crate) fn as_input_object(&self) -> Option<&InputObject> {
62        if let Type::InputObject(obj) = self {
63            Some(obj)
64        } else {
65            None
66        }
67    }
68
69    #[inline]
70    pub(crate) fn as_subscription(&self) -> Option<&Subscription> {
71        if let Type::Subscription(subscription) = self {
72            Some(subscription)
73        } else {
74            None
75        }
76    }
77
78    pub(crate) fn is_output_type(&self) -> bool {
79        match self {
80            Type::Scalar(_) => true,
81            Type::Object(_) => true,
82            Type::InputObject(_) => false,
83            Type::Enum(_) => true,
84            Type::Interface(_) => true,
85            Type::Union(_) => true,
86            Type::Subscription(_) => false,
87            Type::Upload => false,
88        }
89    }
90
91    pub(crate) fn is_input_type(&self) -> bool {
92        match self {
93            Type::Scalar(_) => true,
94            Type::Object(_) => false,
95            Type::InputObject(_) => true,
96            Type::Enum(_) => true,
97            Type::Interface(_) => false,
98            Type::Union(_) => false,
99            Type::Subscription(_) => false,
100            Type::Upload => true,
101        }
102    }
103
104    pub(crate) fn register(&self, registry: &mut Registry) -> Result<(), SchemaError> {
105        if registry.types.contains_key(self.name()) {
106            return Err(format!("Type \"{0}\" already exists", self.name()).into());
107        }
108
109        match self {
110            Type::Scalar(scalar) => scalar.register(registry),
111            Type::Object(object) => object.register(registry),
112            Type::InputObject(input_object) => input_object.register(registry),
113            Type::Enum(e) => e.register(registry),
114            Type::Interface(interface) => interface.register(registry),
115            Type::Union(union) => union.register(registry),
116            Type::Subscription(subscription) => subscription.register(registry),
117            Type::Upload => {
118                <Upload as crate::InputType>::create_type_info(registry);
119                Ok(())
120            }
121        }
122    }
123}
124
125impl From<Scalar> for Type {
126    #[inline]
127    fn from(scalar: Scalar) -> Self {
128        Type::Scalar(scalar)
129    }
130}
131
132impl From<Object> for Type {
133    #[inline]
134    fn from(obj: Object) -> Self {
135        Type::Object(obj)
136    }
137}
138
139impl From<InputObject> for Type {
140    #[inline]
141    fn from(obj: InputObject) -> Self {
142        Type::InputObject(obj)
143    }
144}
145
146impl From<Enum> for Type {
147    #[inline]
148    fn from(e: Enum) -> Self {
149        Type::Enum(e)
150    }
151}
152
153impl From<Interface> for Type {
154    #[inline]
155    fn from(interface: Interface) -> Self {
156        Type::Interface(interface)
157    }
158}
159
160impl From<Union> for Type {
161    #[inline]
162    fn from(union: Union) -> Self {
163        Type::Union(union)
164    }
165}
166
167impl From<Subscription> for Type {
168    #[inline]
169    fn from(subscription: Subscription) -> Self {
170        Type::Subscription(subscription)
171    }
172}