apollo-federation 2.13.1

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
//! Expansion Visitors
//!
//! This module contains various helper visitors for traversing nested structures,
//! adding needed types to a mutable schema.

pub(crate) mod input;
pub(crate) mod selection;

use std::collections::VecDeque;

use apollo_compiler::Name;
use apollo_compiler::ast::Directive;
use indexmap::IndexSet;

use crate::schema::FederationSchema;
use crate::schema::ValidFederationSchema;

/// Filter out directives from a directive list
pub(crate) fn filter_directives<'a, D, I, O>(deny_list: &IndexSet<Name>, directives: D) -> O
where
    D: IntoIterator<Item = &'a I>,
    I: 'a + AsRef<Directive> + Clone,
    O: FromIterator<I>,
{
    directives
        .into_iter()
        .filter(|d| !deny_list.contains(&d.as_ref().name))
        .cloned()
        .collect()
}

/// Try to pre-insert into a schema, ignoring the operation if the type already exists
/// and matches the existing type. Idempotent for v0.4+ code paths where types may be
/// visited multiple times during shape-driven expansion.
macro_rules! try_pre_insert {
    ($schema:expr, $pos:expr) => {{
        if let Some(old_pos) = $schema.try_get_type($pos.type_name.clone()) {
            // Verify that the types match
            let pos = $crate::schema::position::TypeDefinitionPosition::from($pos.clone());
            if old_pos != pos {
                Err($crate::FederationError::internal(format!(
                    "found different type when upserting: expected {:?} found {:?}",
                    pos, old_pos
                )))
            } else {
                Ok(())
            }
        } else if $schema.referencers().contains_type_name(&$pos.type_name) {
            // Already pre-inserted but not yet inserted - idempotent
            Ok(())
        } else {
            $pos.pre_insert($schema)
        }
    }};
}

/// Try to insert into a schema, ignoring the operation if the type already exists
/// and matches the existing type
macro_rules! try_insert {
    ($schema:expr, $pos:expr, $def:expr) => {{
        if let Some(old_pos) = $schema.try_get_type($pos.type_name.clone()) {
            // Verify that the types match
            let pos = $crate::schema::position::TypeDefinitionPosition::from($pos.clone());
            if old_pos != pos {
                Err($crate::FederationError::internal(format!(
                    "found different type when upserting: expected {:?} found {:?}",
                    pos, old_pos
                )))
            } else {
                Ok(())
            }
        } else {
            $pos.insert($schema, $def)
        }
    }};
}
pub(crate) use try_insert;
pub(crate) use try_pre_insert;

/// Visitor for arbitrary field types.
///
/// Any type of interest that should be viewed when traversing the tree-like structure
/// defined by [GroupVisitor] should implement this trait.
pub(crate) trait FieldVisitor<Field>: Sized {
    type Error;

    /// Visit a field
    fn visit(&mut self, field: Field) -> Result<(), Self::Error>;
}

/// Visitor for arbitrary tree-like structures where nodes can also have children
///
/// This trait treats all nodes in the graph as Fields, checking if a Field is also
/// a group for handling children. Visiting order is depth-first.
pub(crate) trait GroupVisitor<Group, Field>
where
    Self: FieldVisitor<Field>,
    Field: Clone,
{
    /// Try to get a group from a field, returning None if the field is not a group
    fn try_get_group_for_field(
        &self,
        field: &Field,
    ) -> Result<Option<Group>, <Self as FieldVisitor<Field>>::Error>;

    /// Enter a subselection group
    /// Note: You can assume that the field corresponding to this
    /// group will be visited first.
    fn enter_group(
        &mut self,
        group: &Group,
    ) -> Result<Vec<Field>, <Self as FieldVisitor<Field>>::Error>;

    /// Exit a subselection group
    /// Note: You can assume that the named selection corresponding to this
    /// group will be visited and entered first.
    fn exit_group(&mut self) -> Result<(), <Self as FieldVisitor<Field>>::Error>;

    /// Walk through the `Group`, visiting each output key. If at any point, one of the
    /// visitor methods returns an error, then the walk will be stopped and the error will be
    /// returned.
    fn walk(mut self, entry: Group) -> Result<Self, <Self as FieldVisitor<Field>>::Error> {
        // Start visiting each of the fields
        let mut to_visit =
            VecDeque::from_iter(self.enter_group(&entry)?.into_iter().map(|n| (0i32, n)));
        let mut current_depth = 0;
        while let Some((depth, next)) = to_visit.pop_front() {
            for _ in depth..current_depth {
                self.exit_group()?;
            }
            current_depth = depth;

            self.visit(next.clone())?;

            // If we have a named selection that has a subselection, then we want to
            // make sure that we visit the children before all other siblings.
            //
            // Note: We reverse here since we always push to the front.
            if let Some(group) = self.try_get_group_for_field(&next)? {
                current_depth += 1;

                let fields = self.enter_group(&group)?;
                fields
                    .into_iter()
                    .rev()
                    .for_each(|s| to_visit.push_front((current_depth, s)));
            }
        }

        // Make sure that we exit until we are no longer nested
        for _ in 0..=current_depth {
            self.exit_group()?;
        }

        Ok(self)
    }
}

/// A visitor for schema building.
///
/// This implementation of the JSONSelection visitor walks a JSONSelection,
/// copying over all output types (and respective fields / sub types) as it goes
/// from a reference schema.
pub(crate) struct SchemaVisitor<'a, Group, GroupType> {
    /// List of directives to not copy over into the target schema.
    directive_deny_list: &'a IndexSet<Name>,

    /// The original schema used for sourcing all types / fields / directives / etc.
    original_schema: &'a ValidFederationSchema,

    /// The target schema for adding all types.
    to_schema: &'a mut FederationSchema,

    /// A stack of parent types used for fetching subtypes
    ///
    /// Each entry corresponds to a nested subselect in the JSONSelection.
    type_stack: Vec<(Group, GroupType)>,
}

impl<'a, Group, GroupType> SchemaVisitor<'a, Group, GroupType> {
    pub(crate) fn new(
        original_schema: &'a ValidFederationSchema,
        to_schema: &'a mut FederationSchema,
        directive_deny_list: &'a IndexSet<Name>,
    ) -> Self {
        SchemaVisitor {
            directive_deny_list,
            original_schema,
            to_schema,
            type_stack: Vec::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use insta::assert_snapshot;
    use itertools::Itertools;

    use crate::connectors::JSONSelection;
    use crate::connectors::SubSelection;
    use crate::connectors::expand::visitors::FieldVisitor;
    use crate::connectors::expand::visitors::GroupVisitor;
    use crate::connectors::json_selection::NamedSelection;
    use crate::error::FederationError;

    /// Visitor for tests.
    ///
    /// Each node visited is added, along with its depth. This is later printed
    /// such that groups are indented based on depth.
    struct TestVisitor<'a> {
        depth_stack: Vec<usize>,
        visited: &'a mut Vec<(usize, String)>,
    }

    impl<'a> TestVisitor<'a> {
        fn new(visited: &'a mut Vec<(usize, String)>) -> Self {
            Self {
                depth_stack: Vec::new(),
                visited,
            }
        }

        fn last_depth(&self) -> Option<usize> {
            self.depth_stack.last().copied()
        }
    }

    fn print_visited(visited: Vec<(usize, String)>) -> String {
        let mut result = String::new();
        for (depth, visited) in visited {
            result.push_str(&format!("{}{visited}\n", "|  ".repeat(depth)));
        }

        result
    }

    impl FieldVisitor<NamedSelection> for TestVisitor<'_> {
        type Error = FederationError;

        fn visit<'a>(&mut self, field: NamedSelection) -> Result<(), Self::Error> {
            for name in field.names() {
                self.visited
                    .push((self.last_depth().unwrap_or_default(), name.to_string()));
            }

            Ok(())
        }
    }

    impl GroupVisitor<SubSelection, NamedSelection> for TestVisitor<'_> {
        fn try_get_group_for_field(
            &self,
            field: &NamedSelection,
        ) -> Result<Option<SubSelection>, FederationError> {
            Ok(field.next_subselection().cloned())
        }

        fn enter_group(
            &mut self,
            group: &SubSelection,
        ) -> Result<Vec<NamedSelection>, FederationError> {
            let next_depth = self.last_depth().map(|d| d + 1).unwrap_or(0);
            self.depth_stack.push(next_depth);
            Ok(group
                .selections_iter()
                .sorted_by_key(|s| s.names())
                .cloned()
                .collect())
        }

        fn exit_group(&mut self) -> Result<(), FederationError> {
            self.depth_stack.pop().unwrap();
            Ok(())
        }
    }

    #[test]
    fn it_iterates_over_empty_path() {
        let mut visited = Vec::new();
        let visitor = TestVisitor::new(&mut visited);
        let selection = JSONSelection::parse("").unwrap();

        visitor
            .walk(selection.next_subselection().cloned().unwrap())
            .unwrap();
        assert_snapshot!(print_visited(visited), @"");
    }

    #[test]
    fn it_iterates_over_simple_selection() {
        let mut visited = Vec::new();
        let visitor = TestVisitor::new(&mut visited);
        let selection = JSONSelection::parse("a b c d").unwrap();

        visitor
            .walk(selection.next_subselection().cloned().unwrap())
            .unwrap();
        assert_snapshot!(print_visited(visited), @r###"
        a
        b
        c
        d
        "###);
    }

    #[test]
    fn it_iterates_over_aliased_selection() {
        let mut visited = Vec::new();
        let visitor = TestVisitor::new(&mut visited);
        let selection = JSONSelection::parse("a: one b: two c: three d: four").unwrap();

        visitor
            .walk(selection.next_subselection().cloned().unwrap())
            .unwrap();
        assert_snapshot!(print_visited(visited), @r###"
        a
        b
        c
        d
        "###);
    }

    #[test]
    fn it_iterates_over_nested_selection() {
        let mut visited = Vec::new();
        let visitor = TestVisitor::new(&mut visited);
        let selection = JSONSelection::parse("a { b { c { d { e } } } } f").unwrap();

        visitor
            .walk(selection.next_subselection().cloned().unwrap())
            .unwrap();
        assert_snapshot!(print_visited(visited), @r###"
        a
        |  b
        |  |  c
        |  |  |  d
        |  |  |  |  e
        f
        "###);
    }

    #[test]
    fn it_iterates_over_paths() {
        let mut visited = Vec::new();
        let visitor = TestVisitor::new(&mut visited);
        let selection = JSONSelection::parse(
            "a
            $.b {
                c
                $.d {
                    e
                    f: g.h { i }
                }
            }
            j",
        )
        .unwrap();

        visitor
            .walk(selection.next_subselection().cloned().unwrap())
            .unwrap();
        assert_snapshot!(print_visited(visited), @r###"
        a
        c
        e
        f
        |  i
        j
        "###);
    }

    #[test]
    fn it_iterates_over_complex_selection() {
        let mut visited = Vec::new();
        let visitor = TestVisitor::new(&mut visited);
        let selection = JSONSelection::parse(
            "id
            name
            username
            email
            address {
              street
              suite
              city
              zipcode
              geo {
                lat
                lng
              }
            }
            phone
            website
            company {
              name
              catchPhrase
              bs
            }",
        )
        .unwrap();

        visitor
            .walk(selection.next_subselection().cloned().unwrap())
            .unwrap();
        assert_snapshot!(print_visited(visited), @r###"
        address
        |  city
        |  geo
        |  |  lat
        |  |  lng
        |  street
        |  suite
        |  zipcode
        company
        |  bs
        |  catchPhrase
        |  name
        email
        id
        name
        phone
        username
        website
        "###);
        // let iter = selection.iter();
        // assert_debug_snapshot!(iter.collect_vec());
    }
}