apollo-router 1.61.13

A configurable, high-performance routing runtime for Apollo Federation 🚀
Documentation
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
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;

use apollo_compiler::ast;
use apollo_compiler::schema::FieldLookupError;
use tower::BoxError;

/// Transform a document with the given visitor.
pub(crate) fn document(
    visitor: &mut impl Visitor,
    document: &ast::Document,
) -> Result<ast::Document, BoxError> {
    let mut new = ast::Document {
        sources: document.sources.clone(),
        definitions: Vec::new(),
    };

    // go through the fragments and order them, starting with the ones that reference no other fragments
    // then the ones that depend only on the first one, and so on
    // This allows visitors like authorization to have all the required information if they encounter
    // a fragment spread while filtering a fragment
    let mut fragment_visitor = FragmentOrderVisitor::new();
    fragment_visitor.visit_document(document);
    let ordered_fragments = fragment_visitor.ordered_fragments();

    visitor.state().reset();

    // Then walk again through the fragments: if a fragment is entirely filtered, we want to
    // remove the spread too
    for def in ordered_fragments {
        visitor.state().used_fragments.clear();
        visitor.state().used_variables.clear();

        if let Some(new_def) = visitor.fragment_definition(def)? {
            // keep the list of used variables per fragment, as we need to use it to know which variables are used
            // in a query
            let used_variables = visitor.state().used_variables.clone();

            // keep the list of used fragments per fragment, as we need to use it to gather used variables later
            // unfortunately, we may not know the variable used for those fragments at this point, as they may not
            // have been processed yet
            let local_used_fragments = visitor.state().used_fragments.clone();

            visitor.state().defined_fragments.insert(
                def.name.as_str().to_string(),
                DefinedFragment {
                    fragment: new_def,
                    used_variables,
                    used_fragments: local_used_fragments,
                },
            );
        }
    }

    // keeps the list of fragments used in the produced document (some fragment spreads might have been removed)
    let mut used_fragments = HashSet::new();

    for definition in &document.definitions {
        if let ast::Definition::OperationDefinition(def) = definition {
            let root_type = visitor
                .schema()
                .root_operation(def.operation_type)
                .ok_or("missing root operation definition")?
                .clone();

            // we reset the used_fragments and used_variables lists for each operation
            visitor.state().used_fragments.clear();
            visitor.state().used_variables.clear();
            if let Some(mut new_def) = visitor.operation(&root_type, def)? {
                let mut local_used_fragments = visitor.state().used_fragments.clone();

                // gather the entire list of fragments used in this operation
                loop {
                    let mut new_local_used_fragments = local_used_fragments.clone();
                    for fragment_name in local_used_fragments.iter() {
                        if let Some(defined_fragment) = visitor
                            .state()
                            .defined_fragments
                            .get(fragment_name.as_str())
                        {
                            new_local_used_fragments
                                .extend(defined_fragment.used_fragments.clone());
                        }
                    }

                    // no more changes, we can stop
                    if new_local_used_fragments.len() == local_used_fragments.len() {
                        break;
                    }
                    local_used_fragments = new_local_used_fragments;
                }

                // add to the list of used variables all the variables used in the fragment spreads
                for fragment_name in local_used_fragments.iter() {
                    if let Some(defined_fragment_used_variables) = visitor
                        .state()
                        .defined_fragments
                        .get(fragment_name.as_str())
                        .map(|defined_fragment| defined_fragment.used_variables.clone())
                    {
                        visitor
                            .state()
                            .used_variables
                            .extend(defined_fragment_used_variables);
                    }
                }
                used_fragments.extend(local_used_fragments);

                // remove unused variables
                new_def.variables.retain(|var| {
                    let res = visitor.state().used_variables.contains(var.name.as_str());
                    res
                });

                new.definitions
                    .push(ast::Definition::OperationDefinition(new_def.into()));
            }
        }
    }

    for (name, defined_fragment) in visitor.state().defined_fragments.clone().into_iter() {
        if used_fragments.contains(name.as_str()) {
            new.definitions.push(ast::Definition::FragmentDefinition(
                defined_fragment.fragment.into(),
            ));
        }
    }
    Ok(new)
}

/// Holds state during the transformation to account for used fragments and variables.
pub(crate) struct TransformState {
    used_fragments: HashSet<String>,
    used_variables: HashSet<String>,
    /// keeps the list of fragments defined in the produced document (the visitor might have removed some of them)
    defined_fragments: BTreeMap<String, DefinedFragment>,
}

#[derive(Clone)]
pub(crate) struct DefinedFragment {
    pub(crate) fragment: ast::FragmentDefinition,
    /// variables used in the fragment
    pub(crate) used_variables: HashSet<String>,
    /// fragments used in the fragment
    pub(crate) used_fragments: HashSet<String>,
}

impl TransformState {
    pub(crate) fn new() -> Self {
        Self {
            used_fragments: HashSet::new(),
            used_variables: HashSet::new(),
            defined_fragments: BTreeMap::new(),
        }
    }

    fn reset(&mut self) {
        self.used_fragments.clear();
        self.used_variables.clear();
        self.defined_fragments.clear();
    }

    pub(crate) fn fragments(&self) -> &BTreeMap<String, DefinedFragment> {
        &self.defined_fragments
    }
}

pub(crate) trait Visitor: Sized {
    fn schema(&self) -> &apollo_compiler::Schema;

    /// mutable state provided by the visitor to clean up unused fragments and variables
    /// do not modify directly
    fn state(&mut self) -> &mut TransformState;

    /// Transform an operation definition.
    ///
    /// Call the [`operation`] free function for the default behavior.
    /// Return `Ok(None)` to remove this operation.
    fn operation(
        &mut self,
        root_type: &str,
        def: &ast::OperationDefinition,
    ) -> Result<Option<ast::OperationDefinition>, BoxError> {
        operation(self, root_type, def)
    }

    /// Transform a fragment definition.
    ///
    /// Call the [`fragment_definition`] free function for the default behavior.
    /// Return `Ok(None)` to remove this fragment.
    fn fragment_definition(
        &mut self,
        def: &ast::FragmentDefinition,
    ) -> Result<Option<ast::FragmentDefinition>, BoxError> {
        fragment_definition(self, def)
    }

    /// Transform a field within a selection set.
    ///
    /// Call the [`field`] free function for the default behavior.
    /// Return `Ok(None)` to remove this field.
    fn field(
        &mut self,
        _parent_type: &str,
        field_def: &ast::FieldDefinition,
        def: &ast::Field,
    ) -> Result<Option<ast::Field>, BoxError> {
        field(self, field_def, def)
    }

    /// Transform a fragment spread within a selection set.
    ///
    /// Call the [`fragment_spread`] free function for the default behavior.
    /// Return `Ok(None)` to remove this fragment spread.
    fn fragment_spread(
        &mut self,
        def: &ast::FragmentSpread,
    ) -> Result<Option<ast::FragmentSpread>, BoxError> {
        let res = fragment_spread(self, def);
        if let Ok(Some(ref fragment)) = res.as_ref() {
            self.state()
                .used_fragments
                .insert(fragment.fragment_name.as_str().to_string());
        }
        res
    }

    /// Transform a inline fragment within a selection set.
    ///
    /// Call the [`inline_fragment`] free function for the default behavior.
    /// Return `Ok(None)` to remove this inline fragment.
    fn inline_fragment(
        &mut self,
        parent_type: &str,
        def: &ast::InlineFragment,
    ) -> Result<Option<ast::InlineFragment>, BoxError> {
        inline_fragment(self, parent_type, def)
    }
}

/// The default behavior for transforming an operation.
///
/// Never returns `Ok(None)`, the `Option` is for `Visitor` impl convenience.
pub(crate) fn operation(
    visitor: &mut impl Visitor,
    root_type: &str,
    def: &ast::OperationDefinition,
) -> Result<Option<ast::OperationDefinition>, BoxError> {
    let Some(selection_set) = selection_set(visitor, root_type, &def.selection_set)? else {
        return Ok(None);
    };

    for directive in def.directives.iter() {
        for argument in directive.arguments.iter() {
            used_variables_from_value(visitor, &argument.value);
        }
    }

    Ok(Some(ast::OperationDefinition {
        name: def.name.clone(),
        operation_type: def.operation_type,
        variables: def.variables.clone(),
        directives: def.directives.clone(),
        selection_set,
    }))
}

/// The default behavior for transforming a fragment definition.
///
/// Never returns `Ok(None)`, the `Option` is for `Visitor` impl convenience.
pub(crate) fn fragment_definition(
    visitor: &mut impl Visitor,
    def: &ast::FragmentDefinition,
) -> Result<Option<ast::FragmentDefinition>, BoxError> {
    let Some(selection_set) = selection_set(visitor, &def.type_condition, &def.selection_set)?
    else {
        return Ok(None);
    };

    for directive in def.directives.iter() {
        for argument in directive.arguments.iter() {
            used_variables_from_value(visitor, &argument.value);
        }
    }

    Ok(Some(ast::FragmentDefinition {
        name: def.name.clone(),
        type_condition: def.type_condition.clone(),
        directives: def.directives.clone(),
        selection_set,
    }))
}

/// The default behavior for transforming a field within a selection set.
///
/// Returns `Ok(None)` if the field had nested selections and they’re all removed.
pub(crate) fn field(
    visitor: &mut impl Visitor,
    field_def: &ast::FieldDefinition,
    def: &ast::Field,
) -> Result<Option<ast::Field>, BoxError> {
    let Some(selection_set) =
        selection_set(visitor, field_def.ty.inner_named_type(), &def.selection_set)?
    else {
        return Ok(None);
    };

    for argument in def.arguments.iter() {
        used_variables_from_value(visitor, &argument.value);
    }

    for directive in def.directives.iter() {
        for argument in directive.arguments.iter() {
            used_variables_from_value(visitor, &argument.value);
        }
    }

    Ok(Some(ast::Field {
        alias: def.alias.clone(),
        name: def.name.clone(),
        arguments: def.arguments.clone(),
        directives: def.directives.clone(),
        selection_set,
    }))
}

/// The default behavior for transforming a fragment spread.
///
/// Never returns `Ok(None)`, the `Option` is for `Visitor` impl convenience.
pub(crate) fn fragment_spread(
    visitor: &mut impl Visitor,
    def: &ast::FragmentSpread,
) -> Result<Option<ast::FragmentSpread>, BoxError> {
    visitor
        .state()
        .used_fragments
        .insert(def.fragment_name.as_str().to_string());

    for directive in def.directives.iter() {
        for argument in directive.arguments.iter() {
            used_variables_from_value(visitor, &argument.value);
        }
    }

    Ok(Some(def.clone()))
}

/// The default behavior for transforming an inline fragment.
///
/// Returns `Ok(None)` if all selections within the fragment are removed.
pub(crate) fn inline_fragment(
    visitor: &mut impl Visitor,
    parent_type: &str,
    def: &ast::InlineFragment,
) -> Result<Option<ast::InlineFragment>, BoxError> {
    let Some(selection_set) = selection_set(visitor, parent_type, &def.selection_set)? else {
        return Ok(None);
    };

    for directive in def.directives.iter() {
        for argument in directive.arguments.iter() {
            used_variables_from_value(visitor, &argument.value);
        }
    }

    Ok(Some(ast::InlineFragment {
        type_condition: def.type_condition.clone(),
        directives: def.directives.clone(),
        selection_set,
    }))
}

pub(crate) fn selection_set(
    visitor: &mut impl Visitor,
    parent_type: &str,
    set: &[ast::Selection],
) -> Result<Option<Vec<ast::Selection>>, BoxError> {
    if set.is_empty() {
        return Ok(Some(Vec::new()));
    }
    let mut selections = Vec::new();
    for sel in set {
        match sel {
            ast::Selection::Field(def) => {
                let field_def = visitor
                    .schema()
                    .type_field(parent_type, &def.name)
                    .map_err(|e| match e {
                        FieldLookupError::NoSuchType => format!("type `{parent_type}` not defined"),
                        FieldLookupError::NoSuchField(_, _) => {
                            format!("no field `{}` in type `{parent_type}`", &def.name)
                        }
                    })?
                    .clone();
                if let Some(sel) = visitor.field(parent_type, &field_def, def)? {
                    selections.push(ast::Selection::Field(sel.into()))
                }
            }
            ast::Selection::FragmentSpread(def) => {
                if let Some(sel) = visitor.fragment_spread(def)? {
                    selections.push(ast::Selection::FragmentSpread(sel.into()))
                }
            }
            ast::Selection::InlineFragment(def) => {
                let fragment_type = def
                    .type_condition
                    .as_ref()
                    .map(|s| s.as_str())
                    .unwrap_or(parent_type);
                if let Some(sel) = visitor.inline_fragment(fragment_type, def)? {
                    selections.push(ast::Selection::InlineFragment(sel.into()))
                }
            }
        }
    }
    Ok((!selections.is_empty()).then_some(selections))
}

fn used_variables_from_value<V: Visitor>(
    visitor: &mut V,
    argument_value: &apollo_compiler::ast::Value,
) {
    match argument_value {
        apollo_compiler::ast::Value::Variable(name) => {
            visitor
                .state()
                .used_variables
                .insert(name.as_str().to_string());
        }
        apollo_compiler::ast::Value::List(values) => {
            for value in values {
                used_variables_from_value(visitor, value);
            }
        }
        apollo_compiler::ast::Value::Object(values) => {
            for (_, value) in values {
                used_variables_from_value(visitor, value);
            }
        }
        _ => {}
    }
}

/// this visitor goes through the list of fragments in the query, looking at fragment spreads
/// in their selection, and generates a list of fragments in the order they should be visited
/// by the transform visitor, to ensure a fragment has already been visited before it is
/// referenced in a fragment spread
struct FragmentOrderVisitor<'a> {
    // the resulting list of ordered fragments
    ordered_fragments: Vec<String>,
    // list of fragments in the document
    fragments: HashMap<String, &'a apollo_compiler::ast::FragmentDefinition>,

    // fragment dependencies. The key is a fragment name, the value is all the fragments that reference it
    // in a fragment spread
    dependencies: HashMap<String, Vec<String>>,
    // name of the fragment currently being visited
    current: Option<String>,

    // how many fragments are used by each fragment. This is decremented when a referenced fragment
    // is added to the final list. Once it reaches 0, the fragment is added to the final list too
    rank: HashMap<String, usize>,
}

impl<'a> FragmentOrderVisitor<'a> {
    fn new() -> Self {
        Self {
            ordered_fragments: Vec::new(),
            fragments: HashMap::new(),
            dependencies: HashMap::new(),
            current: None,
            rank: HashMap::new(),
        }
    }

    fn rerank(&mut self, name: &str) {
        if let Some(v) = self.dependencies.remove(name) {
            for dep in v {
                if let Some(rank) = self.rank.get_mut(&dep) {
                    *rank -= 1;
                    if *rank == 0 {
                        self.ordered_fragments.push(dep.clone());
                        self.rerank(&dep);
                    }
                }
            }
        }
    }

    fn ordered_fragments(self) -> Vec<&'a ast::FragmentDefinition> {
        let mut ordered_fragments = Vec::new();
        for name in self.ordered_fragments {
            if let Some(fragment) = self.fragments.get(name.as_str()) {
                ordered_fragments.push(*fragment);
            }
        }
        ordered_fragments
    }

    fn visit_document(&mut self, doc: &'a ast::Document) {
        for definition in &doc.definitions {
            if let ast::Definition::FragmentDefinition(def) = definition {
                self.visit_fragment_definition(def);
            }
        }
    }

    fn visit_fragment_definition(&mut self, def: &'a ast::FragmentDefinition) {
        let name = def.name.as_str().to_string();
        self.fragments.insert(name.clone(), def);

        self.current = Some(name.clone());
        self.rank.insert(name.clone(), 0);

        self.visit_selection_set(&def.selection_set);

        if self.rank.get(&name) == Some(&0) {
            // if the fragment does not reference any other fragments, it is ready to be added to the final list
            self.ordered_fragments.push(name.clone());
            // then we rerank all the fragments that reference this one: if any of them reaches the rank 0, they
            // are added to the final list too
            self.rerank(&name);
        }
    }

    fn visit_selection_set(&mut self, selection_set: &[apollo_compiler::ast::Selection]) {
        for selection in selection_set {
            match selection {
                ast::Selection::Field(def) => self.visit_selection_set(&def.selection_set),
                ast::Selection::InlineFragment(def) => self.visit_selection_set(&def.selection_set),
                ast::Selection::FragmentSpread(def) => {
                    let name = def.fragment_name.as_str().to_string();

                    // we have already seen this fragment, so we don't need to add it again
                    if self.rank.get(name.as_str()) == Some(&0) {
                        continue;
                    }
                    if let Some(current) = self.current.as_ref() {
                        if let Some(rank) = self.rank.get_mut(current.as_str()) {
                            *rank += 1;
                        }
                        self.dependencies
                            .entry(name)
                            .or_default()
                            .push(current.clone());
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_add_directive_to_fields() {
        struct AddDirective {
            schema: apollo_compiler::Schema,
            state: TransformState,
        }

        impl Visitor for AddDirective {
            fn field(
                &mut self,
                _parent_type: &str,
                field_def: &ast::FieldDefinition,
                def: &ast::Field,
            ) -> Result<Option<ast::Field>, BoxError> {
                Ok(field(self, field_def, def)?.map(|mut new| {
                    new.directives.push(ast::Directive {
                        name: apollo_compiler::name!("added"),
                        arguments: Vec::new(),
                    });
                    new
                }))
            }

            fn schema(&self) -> &apollo_compiler::Schema {
                &self.schema
            }

            fn state(&mut self) -> &mut TransformState {
                &mut self.state
            }
        }

        let graphql = "
        type Query {
            a(id: ID): String
            b: Int
            next: Query
        }
        directive @defer(label: String, if: Boolean! = true) on FRAGMENT_SPREAD | INLINE_FRAGMENT

        query($id: ID = null) {
            a(id: $id)
            ... @defer {
                b
            }
            ... F
        }

        fragment F on Query {
            next {
                a
            }
        }
    ";
        let ast = apollo_compiler::ast::Document::parse(graphql, "").unwrap();
        let (schema, _doc) = ast.to_mixed_validate().unwrap();
        let schema = schema.into_inner();
        let mut visitor = AddDirective {
            schema,
            state: TransformState::new(),
        };
        let expected = "query($id: ID = null) {
  a(id: $id) @added
  ... @defer {
    b @added
  }
  ...F
}

fragment F on Query {
  next @added {
    a @added
  }
}
";
        assert_eq!(document(&mut visitor, &ast).unwrap().to_string(), expected)
    }

    struct RemoveDirective {
        schema: apollo_compiler::Schema,
        state: TransformState,
    }

    impl RemoveDirective {
        fn new(schema: apollo_compiler::Schema) -> Self {
            Self {
                schema,
                state: TransformState::new(),
            }
        }
    }

    impl Visitor for RemoveDirective {
        fn field(
            &mut self,
            _parent_type: &str,
            field_def: &ast::FieldDefinition,
            def: &ast::Field,
        ) -> Result<Option<ast::Field>, BoxError> {
            if def.directives.iter().any(|d| d.name == "remove") {
                return Ok(None);
            }
            field(self, field_def, def)
        }

        fn fragment_spread(
            &mut self,
            def: &ast::FragmentSpread,
        ) -> Result<Option<ast::FragmentSpread>, BoxError> {
            if def.directives.iter().any(|d| d.name == "remove") {
                return Ok(None);
            }

            // remove the fragment spread if the fragment was removed
            if !self
                .state()
                .fragments()
                .contains_key(def.fragment_name.as_str())
            {
                return Ok(None);
            }

            fragment_spread(self, def)
        }

        fn inline_fragment(
            &mut self,
            _parent_type: &str,
            def: &ast::InlineFragment,
        ) -> Result<Option<ast::InlineFragment>, BoxError> {
            if def.directives.iter().any(|d| d.name == "remove") {
                return Ok(None);
            }
            inline_fragment(self, _parent_type, def)
        }

        fn schema(&self) -> &apollo_compiler::Schema {
            &self.schema
        }

        fn state(&mut self) -> &mut TransformState {
            &mut self.state
        }
    }

    struct TestResult<'a> {
        query: &'a str,
        result: ast::Document,
    }

    impl std::fmt::Display for TestResult<'_> {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "query:\n{}\nfiltered:\n{}", self.query, self.result,)
        }
    }

    static TRANSFORM_REMOVE_SCHEMA: &str = r#"
    schema
      @link(url: "https://specs.apollo.dev/link/v1.0")
      @link(url: "https://specs.apollo.dev/join/v0.3", for: EXECUTION)
      @link(url: "https://specs.apollo.dev/authenticated/v0.1", for: SECURITY)
    {
      query: Query
    }
    directive @link(url: String, as: String, for: link__Purpose, import: [link__Import]) repeatable on SCHEMA
    directive @remove on FIELD | INLINE_FRAGMENT | FRAGMENT_SPREAD
    directive @hasArg(arg: String!) on QUERY | FRAGMENT_DEFINITION | INLINE_FRAGMENT | FRAGMENT_SPREAD
    scalar link__Import
      enum link__Purpose {
      """
      `SECURITY` features provide metadata necessary to securely resolve fields.
      """
      SECURITY

      """
      `EXECUTION` features provide metadata necessary for operation execution.
      """
      EXECUTION
    }

    type Query  {
        a(arg: String): String
        b: Obj
        c: Int
        d(arg: [String]): String
        e(arg: Inp): String
        f(arg: [[String]]): String
        g(arg: [Inp]): String

    }

    input Inp {
        a: String
        b: String
        c: [String]
    }

    type Obj {
        a: String
    }
    "#;

    #[test]
    fn remove_directive() {
        let ast = apollo_compiler::ast::Document::parse(TRANSFORM_REMOVE_SCHEMA, "").unwrap();
        let (schema, _doc) = ast.to_mixed_validate().unwrap();
        let schema = schema.into_inner();
        let mut visitor = RemoveDirective::new(schema.clone());

        // test removed fragment
        let query = r#"
            query {
                a
               ... F @remove
            }

            fragment F on Query {
                b {
                    a
                }
            }"#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test removed field with variable
        let query = r#"
            query($a: String) {
                a(arg: $a) @remove
                c
            }"#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test removed field with variable in fragment
        let query = r#"
            query($a: String) {
                ... F
                c
            }

            fragment F on Query {
                a(arg: $a) @remove
            }"#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test field with variable in removed fragment
        let query = r#"
            query($a: String) {
                ... F @remove
                c
            }

            fragment F on Query {
                a(arg: $a)
            }"#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test field with variable in fragment nested in removed fragment
        let query = r#"
            query($a: String) {
                ... F @remove
                c
            }

            fragment F on Query {
                ... G
            }

            fragment G on Query {
                a(arg: $a)
            }
            "#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test removed field with variable in fragment nested in fragment
        let query = r#"
            query($a: String) {
                ... F
                c
            }

            fragment F on Query {
                ... G
            }

            fragment G on Query {
                a(arg: $a) @remove
            }
            "#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test removed field with variable in list argument
        let query = r#"
            query($a: String, $b: String) {
                c
                d(arg: ["a", $a, "b"]) @remove
                aliased: d(arg: [$b])
            }
            "#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test removed field with variable in input argument
        let query = r#"
            query($a: String, $b: String) {
                c
                e(arg:  {a: $a, b: "b"}) @remove
                aliased:  e(arg:  {a: "a", b: $b})
            }
            "#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test removed field with variable in directive on operation and fragment
        let query = r#"
            query Test($a: String, $b: String, $c: String) @hasArg(arg: $a) {
                ...TestFragment
                ...TestFragment2
                c
            }

            fragment TestFragment on Query @hasArg(arg: $b) {
                __typename @remove
            }

            fragment TestFragment2 on Query @hasArg(arg: $c) {
                __typename
            }
            "#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test removed field with variable in directive on fragment spread
        let query = r#"
            query Test($a: String, $b: String) {
                ...TestFragment @hasArg(arg: $a)
                ...TestFragment2 @hasArg(arg: $b)
                c
            }

            fragment TestFragment on Query {
                __typename @remove
            }

            fragment TestFragment2 on Query {
                __typename
            }
            "#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test removed field with variable in directive on inline fragment
        let query = r#"
            query Test($a: String, $b: String) {
                ... @hasArg(arg: $a) {
                  c @remove
                }
                ... @hasArg(arg: $b) {
                  test: c
                }
                c
            }
            "#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test removed field with variable in nested list argument
        let query = r#"
         query($a: String, $b: String) {
             c
             f(arg: [["a"], [$a], ["b"]]) @remove
             aliased: f(arg: [["a"], [$b]])
         }
         "#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test removed field with variable in input type in list argument
        let query = r#"
         query($a: String, $b: String) {
             c
             g(arg: [{a: $a}, {a: "a"}]) @remove
             aliased: g(arg: [{a: "a"}, {a: $b}])
         }
         "#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });

        // test removed field with variable in list in input type argument
        let query = r#"
         query($a: String, $b: String) {
             c
             e(arg: {c: [$a]}) @remove
             aliased: e(arg: {c: [$b]})
         }
         "#;
        let doc = ast::Document::parse(query, "query.graphql").unwrap();
        let result = document(&mut visitor, &doc).unwrap();
        insta::assert_snapshot!(TestResult { query, result });
    }
}