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
use arbitrary::Result;

use crate::{
    input_value::{InputValue, InputValueDef},
    name::Name,
    DocumentBuilder,
};

/// The `__ArgumentsDef` type represents an arguments definition
///
/// *ArgumentsDefinition*:
///     ( InputValueDefinition* )
///
/// Detailed documentation can be found in [GraphQL spec](https://spec.graphql.org/October2021/#ArgumentsDefinition).
///
#[derive(Debug, Clone, PartialEq)]
pub struct ArgumentsDef {
    pub(crate) input_value_definitions: Vec<InputValueDef>,
}

impl From<ArgumentsDef> for apollo_encoder::ArgumentsDefinition {
    fn from(args_def: ArgumentsDef) -> Self {
        apollo_encoder::ArgumentsDefinition::new(
            args_def
                .input_value_definitions
                .into_iter()
                .map(Into::into)
                .collect(),
        )
    }
}

#[cfg(feature = "parser-impl")]
impl From<apollo_parser::ast::ArgumentsDefinition> for ArgumentsDef {
    fn from(args_def: apollo_parser::ast::ArgumentsDefinition) -> Self {
        Self {
            input_value_definitions: args_def
                .input_value_definitions()
                .map(InputValueDef::from)
                .collect(),
        }
    }
}

/// 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).
#[derive(Debug, Clone, PartialEq)]
pub struct Argument {
    pub(crate) name: Name,
    pub(crate) value: InputValue,
}

impl From<Argument> for apollo_encoder::Argument {
    fn from(arg: Argument) -> Self {
        Self::new(arg.name.into(), arg.value.into())
    }
}

#[cfg(feature = "parser-impl")]
impl From<apollo_parser::ast::Argument> for Argument {
    fn from(argument: apollo_parser::ast::Argument) -> Self {
        Self {
            name: argument.name().unwrap().into(),
            value: argument.value().unwrap().into(),
        }
    }
}

impl<'a> DocumentBuilder<'a> {
    /// Create an arbitrary vector of `Argument`
    pub fn arguments(&mut self) -> Result<Vec<Argument>> {
        let num_arguments = self.u.int_in_range(0..=4)?;
        let arguments = (0..num_arguments)
            .map(|_| self.argument())
            .collect::<Result<Vec<_>>>()?;

        Ok(arguments)
    }

    /// Create an arbitrary vector of `Argument` given ArgumentsDef
    pub fn arguments_with_def(&mut self, args_def: &ArgumentsDef) -> Result<Vec<Argument>> {
        let arguments = args_def
            .input_value_definitions
            .iter()
            .map(|input_val_def| self.argument_with_def(input_val_def))
            .collect::<Result<Vec<_>>>()?;

        Ok(arguments)
    }

    /// Create an arbitrary `Argument`
    pub fn argument(&mut self) -> Result<Argument> {
        let name = self.name()?;
        let value = self.input_value()?;

        Ok(Argument { name, value })
    }

    /// Create an arbitrary `Argument`
    pub fn argument_with_def(&mut self, input_val_def: &InputValueDef) -> Result<Argument> {
        let name = input_val_def.name.clone();
        let value = self.input_value_for_type(&input_val_def.ty)?;

        Ok(Argument { name, value })
    }

    /// Create an arbitrary `ArgumentsDef`
    pub fn arguments_definition(&mut self) -> Result<ArgumentsDef> {
        Ok(ArgumentsDef {
            input_value_definitions: self.input_values_def()?,
        })
    }
}