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
use crate::{dynamic::TypeRef, registry::MetaInputValue, Value};

/// A GraphQL input value type
#[derive(Debug)]
pub struct InputValue {
    pub(crate) name: String,
    pub(crate) description: Option<String>,
    pub(crate) ty: TypeRef,
    pub(crate) default_value: Option<Value>,
    pub(crate) inaccessible: bool,
    pub(crate) tags: Vec<String>,
}

impl InputValue {
    /// Create a GraphQL input value type
    #[inline]
    pub fn new(name: impl Into<String>, ty: impl Into<TypeRef>) -> Self {
        Self {
            name: name.into(),
            description: None,
            ty: ty.into(),
            default_value: None,
            inaccessible: false,
            tags: Vec::new(),
        }
    }

    impl_set_description!();
    impl_set_inaccessible!();
    impl_set_tags!();

    /// Set the default value
    #[inline]
    pub fn default_value(self, value: impl Into<Value>) -> Self {
        Self {
            default_value: Some(value.into()),
            ..self
        }
    }

    pub(crate) fn to_meta_input_value(&self) -> MetaInputValue {
        MetaInputValue {
            name: self.name.clone(),
            description: self.description.clone(),
            ty: self.ty.to_string(),
            default_value: self
                .default_value
                .as_ref()
                .map(std::string::ToString::to_string),
            visible: None,
            inaccessible: self.inaccessible,
            tags: self.tags.clone(),
            is_secret: false,
        }
    }
}