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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use super::*;
use fmt::Debug;
use once_cell::sync::Lazy;
use query_structure::ast::ModelId;
use std::{borrow::Cow, fmt};

#[derive(Debug, Clone)]
pub struct OutputType<'a> {
    is_list: bool,
    pub inner: InnerOutputType<'a>,
}

#[derive(Debug, Clone)]
pub enum InnerOutputType<'a> {
    Enum(EnumType),
    Object(ObjectType<'a>),
    Scalar(ScalarType),
}

impl<'a> OutputType<'a> {
    pub fn non_list(inner: InnerOutputType<'a>) -> Self {
        OutputType { is_list: false, inner }
    }

    pub(crate) fn list(containing: InnerOutputType<'a>) -> Self {
        OutputType {
            is_list: true,
            inner: containing,
        }
    }

    pub fn object(containing: ObjectType<'a>) -> Self {
        OutputType::non_list(InnerOutputType::Object(containing))
    }

    pub(crate) fn string() -> InnerOutputType<'a> {
        InnerOutputType::Scalar(ScalarType::String)
    }

    pub(crate) fn int() -> InnerOutputType<'a> {
        InnerOutputType::Scalar(ScalarType::Int)
    }

    pub(crate) fn bigint() -> InnerOutputType<'a> {
        InnerOutputType::Scalar(ScalarType::BigInt)
    }

    pub(crate) fn float() -> InnerOutputType<'a> {
        InnerOutputType::Scalar(ScalarType::Float)
    }

    pub(crate) fn decimal() -> InnerOutputType<'a> {
        InnerOutputType::Scalar(ScalarType::Decimal)
    }

    pub(crate) fn boolean() -> InnerOutputType<'a> {
        InnerOutputType::Scalar(ScalarType::Boolean)
    }

    pub(crate) fn enum_type(containing: EnumType) -> InnerOutputType<'a> {
        InnerOutputType::Enum(containing)
    }

    pub(crate) fn date_time() -> InnerOutputType<'a> {
        InnerOutputType::Scalar(ScalarType::DateTime)
    }

    pub(crate) fn json() -> InnerOutputType<'a> {
        InnerOutputType::Scalar(ScalarType::Json)
    }

    pub(crate) fn uuid() -> InnerOutputType<'a> {
        InnerOutputType::Scalar(ScalarType::UUID)
    }

    pub(crate) fn bytes() -> InnerOutputType<'a> {
        InnerOutputType::Scalar(ScalarType::Bytes)
    }

    /// Attempts to recurse through the type until an object type is found.
    /// Returns Some(ObjectTypeStrongRef) if ab object type is found, None otherwise.
    pub fn as_object_type<'b>(&'b self) -> Option<&'b ObjectType<'a>> {
        match &self.inner {
            InnerOutputType::Object(obj) => Some(obj),
            _ => None,
        }
    }

    pub fn is_list(&self) -> bool {
        self.is_list
    }

    pub fn is_object(&self) -> bool {
        matches!(self.inner, InnerOutputType::Object(_))
    }

    pub fn is_scalar(&self) -> bool {
        matches!(self.inner, InnerOutputType::Scalar(_))
    }

    pub fn is_enum(&self) -> bool {
        matches!(self.inner, InnerOutputType::Enum(_))
    }

    pub fn is_scalar_list(&self) -> bool {
        self.is_list() && self.is_scalar()
    }

    pub fn is_enum_list(&self) -> bool {
        self.is_list() && self.is_enum()
    }
}

type OutputObjectFields<'a> =
    Arc<Lazy<Vec<OutputField<'a>>, Box<dyn FnOnce() -> Vec<OutputField<'a>> + Send + Sync + 'a>>>;

#[derive(Clone)]
pub struct ObjectType<'a> {
    pub(crate) identifier: Identifier,
    pub(crate) fields: OutputObjectFields<'a>,

    // Object types can directly map to models.
    pub(crate) model: Option<ModelId>,
    _heh: (),
}

impl Debug for ObjectType<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ObjectType")
            .field("identifier", &self.identifier)
            .field("model", &self.model)
            .finish()
    }
}

impl<'a> ObjectType<'a> {
    pub(crate) fn new(
        identifier: Identifier,
        fields: impl FnOnce() -> Vec<OutputField<'a>> + Send + Sync + 'a,
    ) -> Self {
        let lazy = Lazy::<Vec<OutputField<'_>>, _>::new(
            Box::new(fields) as Box<dyn FnOnce() -> Vec<OutputField<'a>> + Send + Sync + 'a>
        );
        ObjectType {
            identifier,
            fields: Arc::new(lazy),
            model: None,
            _heh: (),
        }
    }

    pub fn identifier(&self) -> &Identifier {
        &self.identifier
    }

    pub fn name(&self) -> String {
        self.identifier.name()
    }

    pub fn get_fields(&self) -> &[OutputField<'a>] {
        (*self.fields).as_ref()
    }

    pub fn find_field(&self, name: &str) -> Option<&OutputField<'a>> {
        self.get_fields().iter().find(|f| f.name == name)
    }
}

type OutputFieldArguments<'a> =
    Option<Arc<Lazy<Vec<InputField<'a>>, Box<dyn FnOnce() -> Vec<InputField<'a>> + Send + Sync + 'a>>>>;

#[derive(Clone)]
pub struct OutputField<'a> {
    pub(crate) name: Cow<'a, str>,
    pub field_type: OutputType<'a>,

    /// Arguments are input fields, but positioned in context of an output field
    /// instead of being attached to an input object.
    pub(super) arguments: OutputFieldArguments<'a>,

    /// Indicates the presence of the field on the higher output objects.
    /// States whether or not the field can be null.
    pub is_nullable: bool,

    pub(super) query_info: Option<QueryInfo>,
}

impl Debug for OutputField<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("OutputField<'_>")
            .field("name", &self.name)
            .finish_non_exhaustive()
    }
}

impl<'a> OutputField<'a> {
    pub fn name(&self) -> &Cow<'a, str> {
        &self.name
    }

    pub fn arguments(&self) -> &[InputField<'a>] {
        self.arguments.as_ref().map(|f| (**f).as_slice()).unwrap_or(&[])
    }

    pub(crate) fn nullable(mut self) -> Self {
        self.is_nullable = true;
        self
    }

    pub(crate) fn nullable_if(self, condition: bool) -> Self {
        if condition {
            self.nullable()
        } else {
            self
        }
    }

    pub fn model(&self) -> Option<ModelId> {
        self.query_info.as_ref().and_then(|info| info.model)
    }

    pub fn field_type(&self) -> &OutputType<'a> {
        &self.field_type
    }

    pub fn is_find_unique(&self) -> bool {
        matches!(
            self.query_tag(),
            Some(&QueryTag::FindUnique | QueryTag::FindUniqueOrThrow)
        )
    }

    /// Relevant for resolving top level queries.
    pub fn query_info(&self) -> Option<&QueryInfo> {
        self.query_info.as_ref()
    }

    pub fn query_tag(&self) -> Option<&QueryTag> {
        self.query_info().map(|info| &info.tag)
    }

    // Is relation determines whether the given output field maps to a a relation, i.e.
    // is an object and that object is backed by a model, meaning that it is not an scalar list
    pub fn maps_to_relation(&self) -> bool {
        let o = self.field_type.as_object_type();
        o.is_some() && o.unwrap().model.is_some()
    }
}