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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use std::{
    any::Any,
    borrow::Cow,
    fmt::{self, Debug},
    ops::Deref,
};

use futures_util::{future::BoxFuture, Future, FutureExt};
use indexmap::IndexMap;

use crate::{
    dynamic::{InputValue, ObjectAccessor, TypeRef},
    registry::Deprecation,
    Context, Error, Result, Value,
};

/// A value returned from the resolver function
pub struct FieldValue<'a>(pub(crate) FieldValueInner<'a>);

pub(crate) enum FieldValueInner<'a> {
    /// Const value
    Value(Value),
    /// Borrowed any value
    BorrowedAny(&'a (dyn Any + Send + Sync)),
    /// Owned any value
    OwnedAny(Box<dyn Any + Send + Sync>),
    /// A list
    List(Vec<FieldValue<'a>>),
    /// A typed Field value
    WithType {
        /// Field value
        value: Box<FieldValue<'a>>,
        /// Object name
        ty: Cow<'static, str>,
    },
}

impl<'a> From<()> for FieldValue<'a> {
    #[inline]
    fn from(_: ()) -> Self {
        Self(FieldValueInner::Value(Value::Null))
    }
}

impl<'a> From<Value> for FieldValue<'a> {
    #[inline]
    fn from(value: Value) -> Self {
        Self(FieldValueInner::Value(value))
    }
}

impl<'a, T: Into<FieldValue<'a>>> From<Vec<T>> for FieldValue<'a> {
    fn from(values: Vec<T>) -> Self {
        Self(FieldValueInner::List(
            values.into_iter().map(Into::into).collect(),
        ))
    }
}

impl<'a> FieldValue<'a> {
    /// A null value equivalent to `FieldValue::Value(Value::Null)`
    pub const NULL: FieldValue<'a> = Self(FieldValueInner::Value(Value::Null));

    /// A none value equivalent to `None::<FieldValue>`
    ///
    /// It is more convenient to use when your resolver needs to return `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_graphql::dynamic::*;
    ///
    /// let query =
    ///     Object::new("Query").field(Field::new("value", TypeRef::named(TypeRef::INT), |ctx| {
    ///         FieldFuture::new(async move { Ok(FieldValue::NONE) })
    ///     }));
    /// ```
    pub const NONE: Option<FieldValue<'a>> = None;

    /// Returns a `None::<FieldValue>` meaning the resolver no results.
    pub const fn none() -> Option<FieldValue<'a>> {
        None
    }

    /// Create a FieldValue from [`Value`]
    #[inline]
    pub fn value(value: impl Into<Value>) -> Self {
        Self(FieldValueInner::Value(value.into()))
    }

    /// Create a FieldValue from owned any value
    #[inline]
    pub fn owned_any(obj: impl Any + Send + Sync) -> Self {
        Self(FieldValueInner::OwnedAny(Box::new(obj)))
    }

    /// Create a FieldValue from unsized any value
    #[inline]
    pub fn boxed_any(obj: Box<dyn Any + Send + Sync>) -> Self {
        Self(FieldValueInner::OwnedAny(obj))
    }

    /// Create a FieldValue from owned any value
    #[inline]
    pub fn borrowed_any(obj: &'a (dyn Any + Send + Sync)) -> Self {
        Self(FieldValueInner::BorrowedAny(obj))
    }

    /// Create a FieldValue from list
    #[inline]
    pub fn list<I, T>(values: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<FieldValue<'a>>,
    {
        Self(FieldValueInner::List(
            values.into_iter().map(Into::into).collect(),
        ))
    }

    /// Create a FieldValue and specify its type, which must be an object
    ///
    /// NOTE: Fields of type `Interface` or `Union` must return
    /// `FieldValue::WithType`.
    ///
    /// # Examples
    ///
    /// ```
    /// use async_graphql::{dynamic::*, value, Value};
    ///
    /// struct MyObjData {
    ///     a: i32,
    /// }
    ///
    /// let my_obj = Object::new("MyObj").field(Field::new(
    ///     "a",
    ///     TypeRef::named_nn(TypeRef::INT),
    ///     |ctx| FieldFuture::new(async move {
    ///         let data = ctx.parent_value.try_downcast_ref::<MyObjData>()?;
    ///         Ok(Some(Value::from(data.a)))
    ///     }),
    /// ));
    ///
    /// let my_union = Union::new("MyUnion").possible_type(my_obj.type_name());
    ///
    /// let query = Object::new("Query").field(Field::new(
    ///     "obj",
    ///     TypeRef::named_nn(my_union.type_name()),
    ///     |_| FieldFuture::new(async move {
    ///         Ok(Some(FieldValue::owned_any(MyObjData { a: 10 }).with_type("MyObj")))
    ///     }),
    /// ));
    ///
    /// let schema = Schema::build("Query", None, None)
    ///     .register(my_obj)
    ///     .register(my_union)
    ///     .register(query)
    ///     .finish()
    ///     .unwrap();
    ///
    /// # tokio::runtime::Runtime::new().unwrap().block_on(async move {
    /// assert_eq!(
    ///    schema
    ///        .execute("{ obj { ... on MyObj { a } } }")
    ///        .await
    ///        .into_result()
    ///        .unwrap()
    ///        .data,
    ///    value!({ "obj": { "a": 10 } })
    /// );
    /// # });
    /// ```
    pub fn with_type(self, ty: impl Into<Cow<'static, str>>) -> Self {
        Self(FieldValueInner::WithType {
            value: Box::new(self),
            ty: ty.into(),
        })
    }

    /// If the FieldValue is a value, returns the associated
    /// Value. Returns `None` otherwise.
    #[inline]
    pub fn as_value(&self) -> Option<&Value> {
        match &self.0 {
            FieldValueInner::Value(value) => Some(value),
            _ => None,
        }
    }

    /// Like `as_value`, but returns `Result`.
    #[inline]
    pub fn try_to_value(&self) -> Result<&Value> {
        self.as_value()
            .ok_or_else(|| Error::new("internal: not a Value"))
    }

    /// If the FieldValue is a list, returns the associated
    /// vector. Returns `None` otherwise.
    #[inline]
    pub fn as_list(&self) -> Option<&[FieldValue]> {
        match &self.0 {
            FieldValueInner::List(values) => Some(values),
            _ => None,
        }
    }

    /// Like `as_list`, but returns `Result`.
    #[inline]
    pub fn try_to_list(&self) -> Result<&[FieldValue]> {
        self.as_list()
            .ok_or_else(|| Error::new("internal: not a list"))
    }

    /// If the FieldValue is a any, returns the associated
    /// vector. Returns `None` otherwise.
    #[inline]
    pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
        match &self.0 {
            FieldValueInner::BorrowedAny(value) => value.downcast_ref::<T>(),
            FieldValueInner::OwnedAny(value) => value.downcast_ref::<T>(),
            _ => None,
        }
    }

    /// Like `downcast_ref`, but returns `Result`.
    #[inline]
    pub fn try_downcast_ref<T: Any>(&self) -> Result<&T> {
        self.downcast_ref().ok_or_else(|| {
            Error::new(format!(
                "internal: not type \"{}\"",
                std::any::type_name::<T>()
            ))
        })
    }
}

type BoxResolveFut<'a> = BoxFuture<'a, Result<Option<FieldValue<'a>>>>;

/// A context for resolver function
pub struct ResolverContext<'a> {
    /// GraphQL context
    pub ctx: &'a Context<'a>,
    /// Field arguments
    pub args: ObjectAccessor<'a>,
    /// Parent value
    pub parent_value: &'a FieldValue<'a>,
}

impl<'a> Deref for ResolverContext<'a> {
    type Target = Context<'a>;

    fn deref(&self) -> &Self::Target {
        self.ctx
    }
}

/// A future that returned from field resolver
pub enum FieldFuture<'a> {
    /// A pure value without any async operation
    Value(Option<FieldValue<'a>>),

    /// A future that returned from field resolver
    Future(BoxResolveFut<'a>),
}

impl<'a> FieldFuture<'a> {
    /// Create a `FieldFuture` from a `Future`
    pub fn new<Fut, R>(future: Fut) -> Self
    where
        Fut: Future<Output = Result<Option<R>>> + Send + 'a,
        R: Into<FieldValue<'a>> + Send,
    {
        FieldFuture::Future(
            async move {
                let res = future.await?;
                Ok(res.map(Into::into))
            }
            .boxed(),
        )
    }

    /// Create a `FieldFuture` from a `Value`
    pub fn from_value(value: Option<Value>) -> Self {
        FieldFuture::Value(value.map(FieldValue::from))
    }
}

pub(crate) type BoxResolverFn =
    Box<(dyn for<'a> Fn(ResolverContext<'a>) -> FieldFuture<'a> + Send + Sync)>;

/// A GraphQL field
pub struct Field {
    pub(crate) name: String,
    pub(crate) description: Option<String>,
    pub(crate) arguments: IndexMap<String, InputValue>,
    pub(crate) ty: TypeRef,
    pub(crate) ty_str: String,
    pub(crate) resolver_fn: BoxResolverFn,
    pub(crate) deprecation: Deprecation,
    pub(crate) external: bool,
    pub(crate) requires: Option<String>,
    pub(crate) provides: Option<String>,
    pub(crate) shareable: bool,
    pub(crate) inaccessible: bool,
    pub(crate) tags: Vec<String>,
    pub(crate) override_from: Option<String>,
}

impl Debug for Field {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Field")
            .field("name", &self.name)
            .field("description", &self.description)
            .field("arguments", &self.arguments)
            .field("ty", &self.ty)
            .field("deprecation", &self.deprecation)
            .finish()
    }
}

impl Field {
    /// Create a GraphQL field
    pub fn new<N, T, F>(name: N, ty: T, resolver_fn: F) -> Self
    where
        N: Into<String>,
        T: Into<TypeRef>,
        F: for<'a> Fn(ResolverContext<'a>) -> FieldFuture<'a> + Send + Sync + 'static,
    {
        let ty = ty.into();
        Self {
            name: name.into(),
            description: None,
            arguments: Default::default(),
            ty_str: ty.to_string(),
            ty,
            resolver_fn: Box::new(resolver_fn),
            deprecation: Deprecation::NoDeprecated,
            external: false,
            requires: None,
            provides: None,
            shareable: false,
            inaccessible: false,
            tags: Vec::new(),
            override_from: None,
        }
    }

    impl_set_description!();
    impl_set_deprecation!();
    impl_set_external!();
    impl_set_requires!();
    impl_set_provides!();
    impl_set_shareable!();
    impl_set_inaccessible!();
    impl_set_tags!();
    impl_set_override_from!();

    /// Add an argument to the field
    #[inline]
    pub fn argument(mut self, input_value: InputValue) -> Self {
        self.arguments.insert(input_value.name.clone(), input_value);
        self
    }
}