hive-router 0.2.0

GraphQL router for Federation, part of the Hive platform
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
use graphql_tools::parser::query as query_ast;
use serde::{ser::SerializeSeq, Deserialize, Serialize};
use std::{
    collections::BTreeSet,
    fmt::{Debug, Display},
    hash::Hash,
};

use crate::query_planner::{
    ast::merge_path::{Condition, MergePath, Segment},
    ast::value::Value,
    utils::pretty_display::{get_indent, PrettyDisplay},
};

use super::{arguments::ArgumentsMap, selection_item::SelectionItem};

#[derive(Debug, Clone, Default, Deserialize)]
pub struct SelectionSet {
    pub items: Vec<SelectionItem>,
}

impl<'a, T: query_ast::Text<'a>> From<query_ast::SelectionSet<'a, T>> for SelectionSet {
    fn from(selection_set: query_ast::SelectionSet<'a, T>) -> Self {
        Self {
            items: selection_set
                .items
                .into_iter()
                .map(|item| item.into())
                .collect::<Vec<SelectionItem>>(),
        }
    }
}

impl PartialEq for SelectionSet {
    fn eq(&self, other: &Self) -> bool {
        self.items == other.items
    }
}

impl Eq for SelectionSet {}

impl Display for SelectionSet {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.items.is_empty() {
            return Ok(());
        }

        write!(f, "{{")?;
        for (i, item) in self.items.iter().enumerate() {
            if i + 1 == self.items.len() {
                write!(f, "{}", item)?;
            } else {
                write!(f, "{} ", item)?;
            }
        }
        write!(f, "}}")?;
        Ok(())
    }
}

impl SelectionSet {
    pub fn cost(&self) -> u64 {
        let mut cost = 1;

        for node in &self.items {
            cost += node.cost();
        }

        cost
    }

    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    pub fn contains(&self, other: &Self) -> bool {
        selection_items_are_subset_of(&self.items, &other.items)
    }

    pub fn variable_usages(&self) -> BTreeSet<String> {
        self.items
            .iter()
            .flat_map(|item| item.variable_usages())
            .collect()
    }

    pub fn strip_for_plan_input(&self) -> Self {
        SelectionSet {
            items: self
                .items
                .iter()
                .map(|item| item.strip_for_plan_input())
                .collect(),
        }
    }

    pub fn entities_field(&self) -> Option<&FieldSelection> {
        self.items.iter().find_map(|item| {
            if let SelectionItem::Field(field) = item {
                if field.name == "_entities" {
                    return Some(field);
                }
            }
            None
        })
    }

    pub fn iter_fields_and_fragments_of_same_type<'a>(
        &'a self,
        type_name: &'a str,
    ) -> impl Iterator<Item = &'a FieldSelection> + 'a {
        let mut stack = vec![self.items.iter()];

        std::iter::from_fn(move || loop {
            let iter = stack.last_mut()?;

            match iter.next() {
                Some(SelectionItem::Field(field)) => {
                    return Some(field);
                }

                Some(SelectionItem::InlineFragment(fragment))
                    if fragment.type_condition == type_name =>
                {
                    stack.push(fragment.selections.items.iter());
                }

                Some(_) => {}

                None => {
                    stack.pop();
                }
            }
        })
    }
}

impl Hash for SelectionSet {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.items.hash(state);
    }
}

impl Serialize for SelectionSet {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut seq = serializer.serialize_seq(Some(self.items.len()))?;
        for e in &self.items {
            seq.serialize_element(&e)?;
        }
        seq.end()
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct FieldSelection {
    pub name: String,
    #[serde(skip_serializing_if = "SelectionSet::is_empty")]
    pub selections: SelectionSet,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alias: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arguments: Option<ArgumentsMap>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub skip_if: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_if: Option<String>,
    /// Whether to skip this field in response projection.
    /// Defaults to `false`.
    /// The only case so far for it is when the selection set becomes empty,
    /// due to a field being `@skip(if: true)` or `@include(if: false)`,
    /// and used to avoid sending empty selection sets,
    /// and to project an empty object.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub omit_from_response: bool,
}

impl Hash for FieldSelection {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.name.hash(state);

        if let Some(alias) = &self.alias {
            alias.hash(state);
        }

        self.selections.hash(state);

        if let Some(arguments) = &self.arguments {
            arguments.hash(state);
        }
    }
}

impl PartialEq for FieldSelection {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
            && self.alias == other.alias
            && self.arguments() == other.arguments()
    }
}

impl FieldSelection {
    pub fn with_new_selections(&self, selections: SelectionSet) -> Self {
        FieldSelection {
            name: self.name.clone(),
            alias: self.alias.clone(),
            selections,
            arguments: self.arguments.clone(),
            skip_if: self.skip_if.clone(),
            include_if: self.include_if.clone(),
            omit_from_response: self.omit_from_response,
        }
    }

    /// Returns the unique identifier of the field within the selection set.
    /// This means, the alias or the field name if no alias is present.
    pub fn selection_identifier(&self) -> &str {
        match &self.alias {
            Some(alias) => alias,
            None => &self.name,
        }
    }

    /// Calculates a hash value based on the arguments of the field selection.
    /// If no arguments are present, returns 0.
    /// This is used to determine if two field selections are equal, and to avoid conflicts in the selection sets we produce.
    pub fn arguments_hash(&self) -> u64 {
        if let Some(arguments) = &self.arguments {
            return arguments.hash_u64();
        }

        0
    }

    pub fn is_leaf(&self) -> bool {
        self.selections.is_empty()
    }

    pub fn new_typename() -> Self {
        FieldSelection {
            name: "__typename".to_string(),
            alias: None,
            selections: SelectionSet::default(),
            arguments: None,
            skip_if: None,
            include_if: None,
            omit_from_response: false,
        }
    }

    // Returns a field selection that skips the `__typename` field in the response.
    pub fn new_skipped_typename() -> Self {
        FieldSelection {
            name: "__typename".to_string(),
            alias: None,
            selections: SelectionSet::default(),
            arguments: None,
            skip_if: None,
            include_if: None,
            omit_from_response: true,
        }
    }

    pub fn variable_usages(&self) -> BTreeSet<String> {
        let mut usages = BTreeSet::new();

        if let Some(arguments) = &self.arguments {
            for value in arguments.values() {
                usages.extend(value.variable_usages());
            }
        }

        if let Some(include_if) = &self.include_if {
            usages.insert(include_if.clone());
        }

        if let Some(skip_if) = &self.skip_if {
            usages.insert(skip_if.clone());
        }

        usages.extend(self.selections.variable_usages());
        usages
    }

    pub fn arguments(&self) -> Option<&ArgumentsMap> {
        match &self.arguments {
            Some(arguments) => {
                if arguments.is_empty() {
                    None
                } else {
                    Some(arguments)
                }
            }
            None => None,
        }
    }

    pub fn is_introspection_field(&self) -> bool {
        self.name.starts_with("__")
    }

    /// Returns the name of the variable if this field represents a representation variable.
    pub fn representations_variable_name(&self) -> Option<&str> {
        let value = self.arguments.as_ref()?.get_argument("representations")?;

        match value {
            Value::Variable(variable_name) => Some(variable_name.as_str()),
            _ => None,
        }
    }
}

#[derive(Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InlineFragmentSelection {
    pub type_condition: String,
    pub selections: SelectionSet,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub skip_if: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_if: Option<String>,
}

impl InlineFragmentSelection {
    pub fn variable_usages(&self) -> BTreeSet<String> {
        let mut usages = BTreeSet::new();

        if let Some(include_if) = &self.include_if {
            usages.insert(include_if.clone());
        }

        if let Some(skip_if) = &self.skip_if {
            usages.insert(skip_if.clone());
        }

        usages.extend(self.selections.variable_usages());
        usages
    }

    pub fn with_new_selections(&self, selections: SelectionSet) -> Self {
        InlineFragmentSelection {
            type_condition: self.type_condition.clone(),
            selections,
            skip_if: self.skip_if.clone(),
            include_if: self.include_if.clone(),
        }
    }
}

impl Hash for InlineFragmentSelection {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.type_condition.hash(state);
        self.selections.hash(state);
    }
}

impl Display for FieldSelection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(alias) = &self.alias {
            write!(f, "{}: ", alias)?;
        }

        write!(f, "{}", self.name)?;

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

        if let Some(skip_if) = &self.skip_if {
            write!(f, " @skip(if: ${})", skip_if)?;
        }

        if let Some(include_if) = &self.include_if {
            write!(f, " @include(if: ${})", include_if)?;
        }

        write!(f, "{}", self.selections)
    }
}

impl PrettyDisplay for FieldSelection {
    fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>, depth: usize) -> std::fmt::Result {
        let indent = get_indent(depth);

        let alias_str = match &self.alias {
            Some(alias_name) => format!("{}: ", alias_name),
            None => String::new(),
        };

        let args_str = match &self.arguments() {
            Some(arguments) => format!("({})", arguments),
            None => String::new(),
        };

        write!(f, "{indent}{}{}{}", alias_str, self.name, args_str)?;

        if let Some(skip_if) = &self.skip_if {
            write!(f, " @skip(if: ${})", skip_if)?;
        }

        if let Some(include_if) = &self.include_if {
            write!(f, " @include(if: ${})", include_if)?;
        }

        if self.is_leaf() {
            return writeln!(f);
        }

        writeln!(f, " {{")?;
        self.selections.pretty_fmt(f, depth + 1)?;
        writeln!(f, "{indent}}}")
    }
}

impl PrettyDisplay for SelectionSet {
    fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>, depth: usize) -> std::fmt::Result {
        for item in self.items.iter() {
            item.pretty_fmt(f, depth)?;
        }

        Ok(())
    }
}

impl Display for InlineFragmentSelection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "...on {}", self.type_condition)?;
        if let Some(skip_if) = &self.skip_if {
            write!(f, " @skip(if: ${})", skip_if)?;
        }
        if let Some(include_if) = &self.include_if {
            write!(f, " @include(if: ${})", include_if)?;
        }
        write!(f, "{}", self.selections)
    }
}

impl PrettyDisplay for InlineFragmentSelection {
    fn pretty_fmt(&self, f: &mut std::fmt::Formatter<'_>, depth: usize) -> std::fmt::Result {
        let indent = get_indent(depth);

        write!(f, "{indent}... on {} ", self.type_condition)?;
        if let Some(skip_if) = &self.skip_if {
            write!(f, "@skip(if: ${}) ", skip_if)?;
        }
        if let Some(include_if) = &self.include_if {
            write!(f, "@include(if: ${}) ", include_if)?;
        }

        writeln!(f, "{{")?;

        self.selections.pretty_fmt(f, depth + 1)?;
        writeln!(f, "{indent}}}")
    }
}

pub fn selection_items_are_subset_of(source: &[SelectionItem], target: &[SelectionItem]) -> bool {
    target.iter().all(|target_node| {
        source
            .iter()
            .any(|source_node| selection_item_is_subset_of(source_node, target_node))
    })
}

fn selection_item_is_subset_of(source: &SelectionItem, target: &SelectionItem) -> bool {
    match (source, target) {
        (SelectionItem::Field(source_field), SelectionItem::Field(target_field)) => {
            if source_field.name != target_field.name {
                return false;
            }

            if source_field.is_leaf() != target_field.is_leaf() {
                return false;
            }

            selection_items_are_subset_of(
                &source_field.selections.items,
                &target_field.selections.items,
            )
        }
        // TODO: support fragments
        _ => false,
    }
}

pub fn merge_selection_set(target: &mut SelectionSet, source: &SelectionSet, as_first: bool) {
    if source.items.is_empty() {
        return;
    }

    let mut pending_items = Vec::with_capacity(source.items.len());
    for source_item in source.items.iter() {
        let mut found = false;
        for target_item in target.items.iter_mut() {
            match (source_item, target_item) {
                (SelectionItem::Field(source_field), SelectionItem::Field(target_field))
                    if source_field == target_field
                        && field_condition_equal(
                            &Option::<Condition>::from(source_field),
                            target_field,
                        ) =>
                {
                    found = true;
                    merge_field_omit_from_response(target_field, source_field);
                    merge_selection_set(
                        &mut target_field.selections,
                        &source_field.selections,
                        as_first,
                    );
                    break;
                }
                (
                    SelectionItem::InlineFragment(source_fragment),
                    SelectionItem::InlineFragment(target_fragment),
                ) if source_fragment.type_condition == target_fragment.type_condition
                    && fragment_condition_equal(
                        &Option::<Condition>::from(source_fragment),
                        target_fragment,
                    ) =>
                {
                    found = true;
                    merge_selection_set(
                        &mut target_fragment.selections,
                        &source_fragment.selections,
                        as_first,
                    );
                    break;
                }
                _ => {}
            }
        }

        if !found {
            pending_items.push(source_item.clone())
        }
    }

    if !pending_items.is_empty() {
        if as_first {
            let mut new_items = pending_items;
            new_items.append(&mut target.items);
            target.items = new_items;
        } else {
            target.items.extend(pending_items);
        }
    }
}

#[inline]
fn merge_field_omit_from_response(target: &mut FieldSelection, source: &FieldSelection) {
    // A skipped `__typename` is only there for an empty object.
    // If the client explicitly requested `__typename`, keep the visible field.
    if target.name == "__typename" {
        target.omit_from_response &= source.omit_from_response;
    }
}

pub fn find_selection_set_by_path_mut<'a>(
    root_selection_set: &'a mut SelectionSet,
    path: &MergePath,
) -> Option<&'a mut SelectionSet> {
    let mut current_selection_set = root_selection_set;

    for path_element in path.inner.iter() {
        match path_element {
            Segment::List => {
                continue;
            }
            Segment::TypeCondition(type_names, condition) => {
                let next_selection_set_option =
                    current_selection_set
                        .items
                        .iter_mut()
                        .find_map(|item| match item {
                            SelectionItem::Field(_) => None,
                            SelectionItem::InlineFragment(f) => {
                                if type_names.contains(&f.type_condition)
                                    && fragment_condition_equal(condition, f)
                                {
                                    Some(&mut f.selections)
                                } else {
                                    None
                                }
                            }
                            SelectionItem::FragmentSpread(_) => None,
                        });

                match next_selection_set_option {
                    Some(next_set) => {
                        current_selection_set = next_set;
                    }
                    None => {
                        return None;
                    }
                }
            }
            Segment::Field(field_seg, args_hash, condition) => {
                let next_selection_set_option =
                    current_selection_set
                        .items
                        .iter_mut()
                        .find_map(|item| match item {
                            SelectionItem::Field(field) => {
                                if field.selection_identifier() == field_seg.response_key()
                                    && field.arguments_hash() == *args_hash
                                    && field_condition_equal(condition, field)
                                {
                                    Some(&mut field.selections)
                                } else {
                                    None
                                }
                            }
                            SelectionItem::InlineFragment(..) => None,
                            SelectionItem::FragmentSpread(_) => None,
                        });

                match next_selection_set_option {
                    Some(next_set) => {
                        current_selection_set = next_set;
                    }
                    None => {
                        return None;
                    }
                }
            }
        }
    }
    Some(current_selection_set)
}

pub fn field_condition_equal(cond: &Option<Condition>, field: &FieldSelection) -> bool {
    match cond {
        Some(cond) => match cond {
            Condition::Include(var_name) => {
                field.include_if.as_ref().is_some_and(|v| v == var_name) && field.skip_if.is_none()
            }
            Condition::Skip(var_name) => {
                field.skip_if.as_ref().is_some_and(|v| v == var_name) && field.include_if.is_none()
            }
            Condition::SkipAndInclude { skip, include } => {
                field.skip_if.as_ref().is_some_and(|v| v == skip)
                    && field.include_if.as_ref().is_some_and(|v| v == include)
            }
        },
        None => field.include_if.is_none() && field.skip_if.is_none(),
    }
}

fn fragment_condition_equal(cond: &Option<Condition>, fragment: &InlineFragmentSelection) -> bool {
    match cond {
        Some(cond) => match cond {
            Condition::Include(var_name) => {
                fragment.include_if.as_ref().is_some_and(|v| v == var_name)
                    && fragment.skip_if.is_none()
            }
            Condition::Skip(var_name) => {
                fragment.skip_if.as_ref().is_some_and(|v| v == var_name)
                    && fragment.include_if.is_none()
            }
            Condition::SkipAndInclude { skip, include } => {
                fragment.skip_if.as_ref().is_some_and(|v| v == skip)
                    && fragment.include_if.as_ref().is_some_and(|v| v == include)
            }
        },
        None => fragment.include_if.is_none() && fragment.skip_if.is_none(),
    }
}

/// Find the arguments conflicts between two selections.
/// Returns a vector of tuples containing the indices of conflicting fields in both "source" and "other"
/// Both indices are returned in order to allow for easy resolution of conflicts later, in either side.
pub fn find_arguments_conflicts(
    source: &SelectionSet,
    other: &SelectionSet,
) -> Vec<(usize, usize)> {
    other
        .items
        .iter()
        .enumerate()
        .filter_map(|(index, other_selection)| {
            if let SelectionItem::Field(other_field) = other_selection {
                let other_identifier = other_field.selection_identifier();
                let other_args_hash = other_field.arguments_hash();

                let existing_in_self =
                    source
                        .items
                        .iter()
                        .enumerate()
                        .find_map(|(self_index, self_selection)| {
                            if let SelectionItem::Field(self_field) = self_selection {
                                // If the field selection identifier matches and the arguments hash is different,
                                // then it means that we can't merge the two input siblings
                                if self_field.selection_identifier() == other_identifier
                                    && self_field.arguments_hash() != other_args_hash
                                {
                                    return Some(self_index);
                                }
                            }

                            None
                        });

                if let Some(existing_index) = existing_in_self {
                    return Some((existing_index, index));
                }

                return None;
            }

            None
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use crate::query_planner::ast::value::Value;

    use super::*;

    #[test]
    fn print_alias_selection_set() {
        let selection_set = SelectionSet {
            items: vec![
                SelectionItem::Field(FieldSelection {
                    name: "field1".to_string(),
                    selections: SelectionSet::default(),
                    alias: Some("f".to_string()),
                    arguments: None,
                    skip_if: None,
                    include_if: None,
                    omit_from_response: false,
                }),
                SelectionItem::Field(FieldSelection {
                    name: "field2".to_string(),
                    selections: SelectionSet {
                        items: vec![SelectionItem::Field(FieldSelection {
                            name: "nested".to_string(),
                            selections: SelectionSet::default(),
                            alias: Some("n".to_string()),
                            arguments: Some(("a".to_string(), Value::Int(1)).into()),
                            skip_if: None,
                            include_if: None,
                            omit_from_response: false,
                        })],
                    },
                    alias: Some("f2".to_string()),
                    arguments: None,
                    skip_if: None,
                    include_if: None,
                    omit_from_response: false,
                }),
            ],
        };

        insta::assert_snapshot!(
          selection_set,
          @"{f: field1 f2: field2{n: nested(a: 1)}}"
        )
    }

    #[test]
    fn print_simple_selection_set() {
        let selection_set = SelectionSet {
            items: vec![SelectionItem::Field(FieldSelection {
                name: "field1".to_string(),
                selections: SelectionSet::default(),
                alias: None,
                arguments: None,
                skip_if: None,
                include_if: None,
                omit_from_response: false,
            })],
        };

        insta::assert_snapshot!(
          selection_set,
          @"{field1}"
        )
    }

    #[test]
    fn selection_set_with_arguments() {
        let selection_set = SelectionSet {
            items: vec![SelectionItem::Field(FieldSelection {
                name: "field1".to_string(),
                selections: SelectionSet::default(),
                alias: None,
                arguments: Some(vec![("id".to_string(), Value::Int(1))].into()),
                skip_if: None,
                include_if: None,
                omit_from_response: false,
            })],
        };

        insta::assert_snapshot!(
          selection_set,
          @"{field1(id: 1)}"
        )
    }

    #[test]
    fn complex_selection_set() {
        let selection_set = SelectionSet {
            items: vec![SelectionItem::Field(FieldSelection {
                name: "field1".to_string(),
                selections: SelectionSet::default(),
                alias: None,
                arguments: Some(
                    vec![
                        ("id".to_string(), Value::Int(1)),
                        ("name".to_string(), Value::String("test".to_string())),
                        (
                            "list".to_string(),
                            Value::List(vec![Value::Int(1), Value::Int(2)]),
                        ),
                        (
                            "obj".to_string(),
                            Value::Object(
                                vec![("key".to_string(), Value::String("value".to_string()))]
                                    .into_iter()
                                    .collect(),
                            ),
                        ),
                    ]
                    .into(),
                ),
                skip_if: None,
                include_if: None,
                omit_from_response: false,
            })],
        };

        insta::assert_snapshot!(
          selection_set,
          @r#"{field1(id: 1, list: [1, 2], name: "test", obj: {key: "value"})}"#
        )
    }

    #[test]
    fn merge_selection_set_keeps_fields_with_different_conditions_separate() {
        let mut target = SelectionSet {
            items: vec![SelectionItem::Field(FieldSelection {
                name: "reviews".to_string(),
                selections: SelectionSet::default(),
                alias: None,
                arguments: None,
                skip_if: None,
                include_if: Some("first".to_string()),
                omit_from_response: false,
            })],
        };
        let source = SelectionSet {
            items: vec![SelectionItem::Field(FieldSelection {
                name: "reviews".to_string(),
                selections: SelectionSet::default(),
                alias: None,
                arguments: None,
                skip_if: None,
                include_if: Some("second".to_string()),
                omit_from_response: false,
            })],
        };

        merge_selection_set(&mut target, &source, false);

        assert_eq!(target.items.len(), 2);
    }

    #[test]
    fn merge_selection_set_merges_fields_with_same_condition() {
        let mut target = SelectionSet {
            items: vec![SelectionItem::Field(FieldSelection {
                name: "reviews".to_string(),
                selections: SelectionSet {
                    items: vec![SelectionItem::Field(FieldSelection {
                        name: "id".to_string(),
                        selections: SelectionSet::default(),
                        alias: None,
                        arguments: None,
                        skip_if: None,
                        include_if: None,
                        omit_from_response: false,
                    })],
                },
                alias: None,
                arguments: None,
                skip_if: None,
                include_if: Some("cond".to_string()),
                omit_from_response: false,
            })],
        };
        let source = SelectionSet {
            items: vec![SelectionItem::Field(FieldSelection {
                name: "reviews".to_string(),
                selections: SelectionSet {
                    items: vec![SelectionItem::Field(FieldSelection {
                        name: "body".to_string(),
                        selections: SelectionSet::default(),
                        alias: None,
                        arguments: None,
                        skip_if: None,
                        include_if: None,
                        omit_from_response: false,
                    })],
                },
                alias: None,
                arguments: None,
                skip_if: None,
                include_if: Some("cond".to_string()),
                omit_from_response: false,
            })],
        };

        merge_selection_set(&mut target, &source, false);

        assert_eq!(target.items.len(), 1);

        let SelectionItem::Field(field) = &target.items[0] else {
            panic!("expected field selection");
        };

        assert_eq!(field.selections.items.len(), 2);
    }

    #[test]
    fn merge_selection_set_keeps_inline_fragments_with_different_conditions_separate() {
        let mut target = SelectionSet {
            items: vec![SelectionItem::InlineFragment(InlineFragmentSelection {
                type_condition: "User".to_string(),
                selections: SelectionSet::default(),
                skip_if: None,
                include_if: Some("first".to_string()),
            })],
        };
        let source = SelectionSet {
            items: vec![SelectionItem::InlineFragment(InlineFragmentSelection {
                type_condition: "User".to_string(),
                selections: SelectionSet::default(),
                skip_if: None,
                include_if: Some("second".to_string()),
            })],
        };

        merge_selection_set(&mut target, &source, false);

        assert_eq!(target.items.len(), 2);
    }

    #[test]
    fn merge_selection_set_prefers_visible_typename_over_skipped() {
        let mut target = SelectionSet {
            items: vec![SelectionItem::Field(FieldSelection::new_skipped_typename())],
        };
        let source = SelectionSet {
            items: vec![SelectionItem::Field(FieldSelection::new_typename())],
        };

        merge_selection_set(&mut target, &source, false);

        let [SelectionItem::Field(field)] = target.items.as_slice() else {
            panic!("expected exactly one __typename field");
        };

        assert_eq!(field.name, "__typename");
        assert!(!field.omit_from_response);
    }

    #[test]
    // Field Condition should only be equal to Condition::SkipAndInclude
    fn field_condition_skip_and_include() {
        let skip_cond = Some(Condition::Skip("skip".to_string()));
        let include_cond: Option<Condition> = Some(Condition::Include("include".to_string()));
        let skip_and_include_cond = Some(Condition::SkipAndInclude {
            skip: "skip".to_string(),
            include: "include".to_string(),
        });
        let field = FieldSelection {
            name: "name".to_string(),
            selections: SelectionSet::default(),
            alias: None,
            arguments: None,
            skip_if: Some("skip".to_string()),
            include_if: Some("include".to_string()),
            omit_from_response: false,
        };
        assert!(!field_condition_equal(&skip_cond, &field));
        assert!(!field_condition_equal(&include_cond, &field));
        assert!(field_condition_equal(&skip_and_include_cond, &field));
    }

    #[test]
    // Fragment Condition should only be equal to Condition::SkipAndInclude
    fn fragment_condition_skip_and_include() {
        let skip_cond = Some(Condition::Skip("skip".to_string()));
        let include_cond: Option<Condition> = Some(Condition::Include("include".to_string()));
        let skip_and_include_cond = Some(Condition::SkipAndInclude {
            skip: "skip".to_string(),
            include: "include".to_string(),
        });
        let fragment = InlineFragmentSelection {
            type_condition: "Product".to_string(),
            selections: SelectionSet::default(),
            skip_if: Some("skip".to_string()),
            include_if: Some("include".to_string()),
        };
        assert!(!fragment_condition_equal(&skip_cond, &fragment));
        assert!(!fragment_condition_equal(&include_cond, &fragment));
        assert!(fragment_condition_equal(&skip_and_include_cond, &fragment));
    }
}