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
use std::fmt;
use crate::{InputValueDefinition, Value};
/// The `ArgumentsDefinition` type represents an arguments definition
///
/// *ArgumentsDefinition*:
/// ( InputValueDefinition* )
///
/// Detailed documentation can be found in [GraphQL spec](https://spec.graphql.org/October2021/#ArgumentsDefinition).
///
/// ### Example
/// ```rust
/// use apollo_encoder::{ArgumentsDefinition, InputValueDefinition, Type_};
/// use indoc::indoc;
///
/// let input_value_defs = vec![
/// InputValueDefinition::new(
/// String::from("first"),
/// Type_::NamedType {
/// name: String::from("Int"),
/// },
/// ),
/// InputValueDefinition::new(
/// String::from("second"),
/// Type_::List {
/// ty: Box::new(Type_::NamedType {
/// name: String::from("Int"),
/// }),
/// },
/// ),
/// ];
/// let arguments_def = ArgumentsDefinition::with_values(input_value_defs);
///
/// assert_eq!(arguments_def.to_string(), r#"(first: Int, second: [Int])"#);
/// ```
#[derive(Debug, Default, PartialEq, Clone)]
pub struct ArgumentsDefinition {
pub(crate) input_values: Vec<InputValueDefinition>,
}
impl ArgumentsDefinition {
/// Create a new instance of Argument definition.
pub fn new() -> Self {
Self {
input_values: Vec::new(),
}
}
/// Create a new instance of ArgumentsDefinition given Input Value Definitions.
pub fn with_values(input_values: Vec<InputValueDefinition>) -> Self {
Self { input_values }
}
/// Add an InputValueDefinition to Arguments Definition
pub fn input_value(&mut self, input_value: InputValueDefinition) {
self.input_values.push(input_value)
}
}
impl fmt::Display for ArgumentsDefinition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(")?;
for (i, input_val_def) in self.input_values.iter().enumerate() {
// this branch multilines input value definitions, like this:
// two(
// """This is a description of the \`argument\` argument."""
// argument: InputType!
// ): Type
if input_val_def.description.is_some() {
if i != self.input_values.len() - 1 {
write!(f, "{},", input_val_def)?;
} else {
writeln!(f, "{}", input_val_def)?;
}
// with no descriptions we single line input value definitions:
// two(argument: InputType!): Type
} else if i != self.input_values.len() - 1 {
write!(f, "{}, ", input_val_def)?;
} else {
write!(f, "{}", input_val_def)?;
}
}
if self
.input_values
.iter()
.any(|input| input.description.is_some())
{
write!(f, " )")
} else {
write!(f, ")")
}
}
}
/// The `Argument` type represents an argument
///
/// *Argument*:
/// Name: Value
///
/// Detailed documentation can be found in [GraphQL spec](https://spec.graphql.org/October2021/#sec-Language.Arguments).
///
/// ### Example
/// ```rust
/// use apollo_encoder::{Argument, Value};
///
/// let argument = Argument::new(String::from("argName"), Value::String("value".to_string()));
/// assert_eq!(argument.to_string(), r#"argName: "value""#);
/// ```
#[derive(Debug, PartialEq, Clone)]
pub struct Argument {
name: String,
value: Value,
}
impl Argument {
/// Create a new instance of Argument.
pub fn new(name: String, value: Value) -> Self {
Self { name, value }
}
}
impl fmt::Display for Argument {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.name, self.value)
}
}
#[cfg(test)]
mod tests {
use crate::Type_;
use super::*;
#[test]
fn it_encodes_argument() {
let argument = Argument::new(String::from("argName"), Value::String("value".to_string()));
assert_eq!(argument.to_string(), r#"argName: "value""#);
}
#[test]
fn it_encodes_arguments_definitions() {
let input_value_defs = vec![
InputValueDefinition::new(
String::from("first"),
Type_::NamedType {
name: String::from("Int"),
},
),
InputValueDefinition::new(
String::from("second"),
Type_::List {
ty: Box::new(Type_::NamedType {
name: String::from("Int"),
}),
},
),
];
let arguments_def = ArgumentsDefinition::with_values(input_value_defs);
assert_eq!(arguments_def.to_string(), r#"(first: Int, second: [Int])"#);
}
}