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
use std::fmt;

use crate::{Directive, SelectionSet, VariableDefinition};

/// The OperationDefinition type represents an operation definition
///
/// *OperationDefinition*:
///     OperationType Name? VariableDefinitions? Directives? SelectionSet
///
/// Detailed documentation can be found in [GraphQL spec](https://spec.graphql.org/October2021/#sec-Language.Operations).
///
/// ### Example
/// ```rust
/// use apollo_encoder::{Argument, Field, InlineFragment, Directive, OperationDefinition, OperationType, Selection, SelectionSet, TypeCondition, Type_, Value, VariableDefinition};
/// use indoc::indoc;
///
/// let selection_set = {
///     let sels = vec![
///         Selection::Field(Field::new(String::from("first"))),
///         Selection::Field(Field::new(String::from("second"))),
///     ];
///     let mut sel_set = SelectionSet::new();
///     sels.into_iter().for_each(|sel| sel_set.selection(sel));
///
///     sel_set
/// };
/// let var_def = VariableDefinition::new(
///     String::from("variable_def"),
///     Type_::List {
///         ty: Box::new(Type_::NamedType {
///             name: String::from("Int"),
///         }),
///     },
/// );
/// let mut new_op = OperationDefinition::new(OperationType::Query, selection_set);
/// let mut directive = Directive::new(String::from("testDirective"));
/// directive.arg(Argument::new(
///     String::from("first"),
///     Value::String("one".to_string()),
/// ));
/// new_op.variable_definition(var_def);
/// new_op.directive(directive);
///
/// assert_eq!(
///     new_op.to_string(),
///     indoc! { r#"
///         query($variable_def: [Int]) @testDirective(first: "one") {
///           first
///           second
///         }
///     "#}
/// );
/// ```
#[derive(Debug, Clone)]
pub struct OperationDefinition {
    operation_type: OperationType,
    name: Option<String>,
    variable_definitions: Vec<VariableDefinition>,
    directives: Vec<Directive>,
    selection_set: SelectionSet,
    /// If a document contains only one operation and that operation is a query which defines no variables and
    /// contains no directives then that operation may be represented in a short-hand form which omits the query keyword and operation name.
    shorthand: bool,
}

impl OperationDefinition {
    /// Create a new instance of OperationDef
    pub fn new(operation_type: OperationType, selection_set: SelectionSet) -> Self {
        Self {
            operation_type,
            selection_set,
            name: None,
            variable_definitions: Vec::new(),
            directives: Vec::new(),
            shorthand: false,
        }
    }

    /// Set the operation def's name.
    pub fn name(&mut self, name: Option<String>) {
        self.name = name;
    }

    /// Add a variable definitions.
    pub fn variable_definition(&mut self, variable_definition: VariableDefinition) {
        self.variable_definitions.push(variable_definition);
    }

    /// Add a directive.
    pub fn directive(&mut self, directive: Directive) {
        self.directives.push(directive);
    }

    /// Set this operation as a query shorthand
    /// If a document contains only one operation and that operation is a query which defines no variables and
    /// contains no directives then that operation may be represented in a short-hand form which omits the query keyword and operation name.
    /// Be careful, it will automatically drop variable definitions and directives
    pub fn shorthand(&mut self) {
        self.shorthand = true;
    }
}

impl fmt::Display for OperationDefinition {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let indent_level = 0;

        if !self.shorthand {
            write!(f, "{}", self.operation_type)?;
            if let Some(name) = &self.name {
                write!(f, " {name}")?;
            }

            if !self.variable_definitions.is_empty() {
                write!(f, "(")?;
                for (i, var_def) in self.variable_definitions.iter().enumerate() {
                    if i == self.variable_definitions.len() - 1 {
                        write!(f, "{var_def}")?;
                    } else {
                        write!(f, "{var_def}, ")?;
                    }
                }
                write!(f, ")")?;
            }
            for directive in &self.directives {
                write!(f, " {directive}")?;
            }
            write!(f, " ")?;
        }

        write!(f, "{}", self.selection_set.format_with_indent(indent_level))?;

        Ok(())
    }
}

/// The OperationType type represents the kind of operation
///
/// *OperationType*:
///     query | mutation | subscription
///
/// Detailed documentation can be found in [GraphQL spec](https://spec.graphql.org/October2021/#OperationType).
#[derive(Debug, Clone)]
pub enum OperationType {
    /// Represents a query operation
    Query,
    /// Represents a mutation operation
    Mutation,
    /// Represents a subscription operation
    Subscription,
}

impl fmt::Display for OperationType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OperationType::Query => write!(f, "query"),
            OperationType::Mutation => write!(f, "mutation"),
            OperationType::Subscription => write!(f, "subscription"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{field::Field, Argument, FragmentSpread, Selection, Type_, Value};
    use indoc::indoc;

    #[test]
    fn it_encodes_a_query_operation() {
        let selection_set = {
            let sels = vec![
                Selection::Field(Field::new(String::from("first"))),
                Selection::Field(Field::new(String::from("second"))),
            ];
            let mut sel_set = SelectionSet::new();
            sels.into_iter().for_each(|sel| sel_set.selection(sel));

            sel_set
        };
        let var_def = VariableDefinition::new(
            String::from("variable_def"),
            Type_::List {
                ty: Box::new(Type_::NamedType {
                    name: String::from("Int"),
                }),
            },
        );
        let mut new_op = OperationDefinition::new(OperationType::Query, selection_set);
        let mut directive = Directive::new(String::from("testDirective"));
        directive.arg(Argument::new(
            String::from("first"),
            Value::String("one".to_string()),
        ));
        new_op.variable_definition(var_def);
        new_op.directive(directive);

        assert_eq!(
            new_op.to_string(),
            indoc! { r#"
                query($variable_def: [Int]) @testDirective(first: "one") {
                  first
                  second
                }
            "#}
        );
    }

    #[test]
    fn it_encodes_a_shorthand_query_operation() {
        let selection_set = {
            let sels = vec![
                Selection::Field(Field::new(String::from("first"))),
                Selection::Field(Field::new(String::from("second"))),
            ];
            let mut sel_set = SelectionSet::new();
            sels.into_iter().for_each(|sel| sel_set.selection(sel));

            sel_set
        };
        let mut new_op = OperationDefinition::new(OperationType::Query, selection_set);
        new_op.shorthand();

        assert_eq!(
            new_op.to_string(),
            indoc! { r#"
                {
                  first
                  second
                }
            "#}
        );
    }

    #[test]
    fn it_encodes_a_deeper_query_operation() {
        // ----- Selection set creation
        let fourth_field = Field::new("fourth".to_string());
        let mut third_field = Field::new("third".to_string());
        third_field.selection_set(Some(SelectionSet::with_selections(vec![Selection::Field(
            fourth_field,
        )])));
        let mut second_field = Field::new("second".to_string());
        second_field.selection_set(Some(SelectionSet::with_selections(vec![Selection::Field(
            third_field,
        )])));

        let mut first_field = Field::new("first".to_string());
        first_field.selection_set(Some(SelectionSet::with_selections(vec![Selection::Field(
            second_field,
        )])));

        let selections = vec![
            Selection::Field(first_field),
            Selection::FragmentSpread(FragmentSpread::new(String::from("myFragment"))),
        ];
        let mut selection_set = SelectionSet::new();
        selections
            .into_iter()
            .for_each(|s| selection_set.selection(s));
        // -------------------------

        let var_def = VariableDefinition::new(
            String::from("variable_def"),
            Type_::List {
                ty: Box::new(Type_::NamedType {
                    name: String::from("Int"),
                }),
            },
        );
        let mut new_op = OperationDefinition::new(OperationType::Query, selection_set);
        let mut directive = Directive::new(String::from("testDirective"));
        directive.arg(Argument::new(
            String::from("first"),
            Value::String("one".to_string()),
        ));
        new_op.variable_definition(var_def);
        new_op.directive(directive);

        assert_eq!(
            new_op.to_string(),
            indoc! { r#"
                query($variable_def: [Int]) @testDirective(first: "one") {
                  first {
                    second {
                      third {
                        fourth
                      }
                    }
                  }
                  ...myFragment
                }
            "#}
        );
    }
}