apollo_encoder/
input_field.rs

1use std::fmt;
2
3use crate::{Directive, StringValue, Type_};
4
5#[derive(Debug, PartialEq, Clone)]
6/// Input Field in a given Input Object.
7/// A GraphQL Input Object defines a set of input fields; the input fields are
8/// either scalars, enums, or other input objects. Input fields are similar to
9/// Fields, but can have a default value.
10///
11/// ### Example
12/// ```rust
13/// use apollo_encoder::{Type_, InputField};
14///
15/// let ty_1 = Type_::NamedType {
16///     name: "CatBreed".to_string(),
17/// };
18///
19/// let mut field = InputField::new("cat".to_string(), ty_1);
20/// field.default_value("\"Norwegian Forest\"".to_string());
21///
22/// assert_eq!(field.to_string(), r#"  cat: CatBreed = "Norwegian Forest""#);
23/// ```
24pub struct InputField {
25    // Name must return a String.
26    name: String,
27    // Description may return a String.
28    description: Option<StringValue>,
29    // Type must return a __Type that represents the type of value returned by this field.
30    type_: Type_,
31    // Default value for this input field.
32    default_value: Option<String>,
33    /// Contains all directives for this input value definition
34    directives: Vec<Directive>,
35}
36
37impl InputField {
38    /// Create a new instance of InputField.
39    pub fn new(name: String, type_: Type_) -> Self {
40        Self {
41            description: None,
42            name,
43            type_,
44            default_value: None,
45            directives: Vec::new(),
46        }
47    }
48
49    /// Set the InputField's description.
50    pub fn description(&mut self, description: String) {
51        self.description = Some(StringValue::Field {
52            source: description,
53        });
54    }
55
56    /// Set the InputField's default value.
57    pub fn default_value(&mut self, default_value: String) {
58        self.default_value = Some(default_value);
59    }
60
61    /// Add a directive.
62    pub fn directive(&mut self, directive: Directive) {
63        self.directives.push(directive)
64    }
65}
66
67impl fmt::Display for InputField {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        if let Some(description) = &self.description {
70            writeln!(f, "{description}")?;
71        }
72
73        write!(f, "  {}: {}", self.name, self.type_)?;
74        if let Some(default) = &self.default_value {
75            write!(f, " = {default}")?;
76        }
77
78        for directive in &self.directives {
79            write!(f, " {directive}")?;
80        }
81
82        Ok(())
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use crate::{Argument, Value};
89
90    use super::*;
91    use pretty_assertions::assert_eq;
92
93    #[test]
94    fn it_encodes_fields_with_defaults() {
95        let ty_1 = Type_::NamedType {
96            name: "CatBreed".to_string(),
97        };
98
99        let mut field = InputField::new("cat".to_string(), ty_1);
100        field.default_value("\"Norwegian Forest\"".to_string());
101
102        assert_eq!(field.to_string(), r#"  cat: CatBreed = "Norwegian Forest""#);
103    }
104    #[test]
105    fn it_encodes_fields_with_directives() {
106        let ty_1 = Type_::NamedType {
107            name: "CatBreed".to_string(),
108        };
109        let mut directive = Directive::new(String::from("testDirective"));
110        directive.arg(Argument::new(String::from("first"), Value::Int(1)));
111
112        let mut field = InputField::new("cat".to_string(), ty_1);
113        field.default_value("\"Norwegian Forest\"".to_string());
114        field.directive(directive);
115
116        assert_eq!(
117            field.to_string(),
118            r#"  cat: CatBreed = "Norwegian Forest" @testDirective(first: 1)"#
119        );
120    }
121}