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

use apollo_compiler::executable;
use apollo_compiler::name;

use super::DirectiveList;
use super::Field;
use super::FieldSelection;
use super::InlineFragmentSelection;
use super::Selection;
use super::SelectionMap;
use super::SelectionSet;
use super::runtime_types_intersect;
use crate::error::FederationError;
use crate::schema::ValidFederationSchema;
use crate::schema::position::CompositeTypeDefinitionPosition;

#[derive(Debug, Clone, PartialEq, Eq, derive_more::From)]
pub(crate) enum SelectionOrSet {
    Selection(Selection),
    SelectionSet(SelectionSet),
}

impl Selection {
    fn flatten_unnecessary_fragments(
        &self,
        parent_type: &CompositeTypeDefinitionPosition,
        schema: &ValidFederationSchema,
    ) -> Result<Option<SelectionOrSet>, FederationError> {
        match self {
            Selection::Field(field) => field.flatten_unnecessary_fragments(parent_type, schema),
            Selection::InlineFragment(inline) => {
                inline.flatten_unnecessary_fragments(parent_type, schema)
            }
        }
    }
}

impl FieldSelection {
    fn flatten_unnecessary_fragments(
        &self,
        parent_type: &CompositeTypeDefinitionPosition,
        schema: &ValidFederationSchema,
    ) -> Result<Option<SelectionOrSet>, FederationError> {
        let field_position =
            if self.field.schema() == schema && self.field.parent_type_position() == *parent_type {
                self.field.field_position.clone()
            } else {
                parent_type.field(self.field.name().clone())?
            };

        let field_element =
            if self.field.schema() == schema && self.field.field_position == field_position {
                self.field.clone()
            } else {
                self.field
                    .with_updated_position(schema.clone(), field_position)
            };

        if let Some(selection_set) = &self.selection_set {
            let field_composite_type_position: CompositeTypeDefinitionPosition =
                field_element.output_base_type()?.try_into()?;
            let mut normalized_selection: SelectionSet = selection_set
                .flatten_unnecessary_fragments(&field_composite_type_position, schema)?;

            let mut selection = self.with_updated_element(field_element);
            if normalized_selection.is_empty() {
                // In rare cases, it's possible that everything in the sub-selection was trimmed away and so the
                // sub-selection is empty. Which suggest something may be wrong with this part of the query
                // intent, but the query was valid while keeping an empty sub-selection isn't. So in that
                // case, we just add some "non-included" __typename field just to keep the query valid.
                let directives = DirectiveList::one(executable::Directive {
                    name: name!("include"),
                    arguments: vec![(name!("if"), false).into()],
                });
                let non_included_typename = Selection::from_field(
                    Field {
                        schema: schema.clone(),
                        field_position: field_composite_type_position
                            .introspection_typename_field(),
                        alias: None,
                        arguments: Default::default(),
                        directives,
                        sibling_typename: None,
                    },
                    None,
                );
                let mut typename_selection = SelectionMap::new();
                typename_selection.insert(non_included_typename);

                normalized_selection.selections = Arc::new(typename_selection);
                selection.selection_set = Some(normalized_selection);
            } else {
                selection.selection_set = Some(normalized_selection);
            }
            Ok(Some(SelectionOrSet::Selection(Selection::from(selection))))
        } else {
            Ok(Some(SelectionOrSet::Selection(Selection::from(
                self.with_updated_element(field_element),
            ))))
        }
    }
}

impl InlineFragmentSelection {
    fn flatten_unnecessary_fragments(
        self: &Arc<Self>,
        parent_type: &CompositeTypeDefinitionPosition,
        schema: &ValidFederationSchema,
    ) -> Result<Option<SelectionOrSet>, FederationError> {
        let this_condition = self.inline_fragment.type_condition_position.as_ref();
        // This method assumes by contract that `parent_type` runtimes intersects `self.inline_fragment.parent_type_position`'s,
        // but `parent_type` runtimes may be a subset. So first check if the selection should not be discarded on that account (that
        // is, we should not keep the selection if its condition runtimes don't intersect at all with those of
        // `parent_type` as that would ultimately make an invalid selection set).
        if let Some(type_condition) = this_condition
            && (self.inline_fragment.schema != *schema
                || self.inline_fragment.parent_type_position != *parent_type)
            && !runtime_types_intersect(type_condition, parent_type, schema)
        {
            return Ok(None);
        }

        // We know the condition is "valid", but it may not be useful. That said, if the condition has directives,
        // we preserve the fragment no matter what.
        if self.inline_fragment.directives.is_empty() {
            // There is a number of cases where a fragment is not useful:
            // 1. if there is no type condition (remember it also has no directives).
            // 2. if it's the same type as the current type: it's not restricting types further.
            // 3. if the current type is an object more generally: because in that case the condition
            //   cannot be restricting things further (it's typically a less precise interface/union).
            let useless_fragment = this_condition.is_none_or(|type_condition| {
                self.inline_fragment.schema == *schema && type_condition == parent_type
            });
            if useless_fragment || parent_type.is_object_type() {
                // Try to skip this fragment and flatten_unnecessary_fragments self.selection_set with `parent_type`,
                // instead of its original type.
                let selection_set = self
                    .selection_set
                    .flatten_unnecessary_fragments(parent_type, schema)?;
                return if selection_set.is_empty() {
                    Ok(None)
                } else {
                    // We need to rebase since the parent type for the selection set could be
                    // changed.
                    // Note: Rebasing after flattening, since rebasing before that can error out.
                    //       Or, `flatten_unnecessary_fragments` could `rebase` at the same time.
                    let selection_set = if useless_fragment {
                        selection_set
                    } else {
                        selection_set.rebase_on(parent_type, schema)?
                    };
                    Ok(Some(SelectionOrSet::SelectionSet(selection_set)))
                };
            }
        }

        // Note: This selection_set is not rebased here yet. It will be rebased later as necessary.
        let selection_set = self.selection_set.flatten_unnecessary_fragments(
            &self.selection_set.type_position,
            &self.selection_set.schema,
        )?;
        // It could be that nothing was satisfiable.
        if selection_set.is_empty() {
            if self.inline_fragment.directives.is_empty() {
                return Ok(None);
            } else {
                let rebased_fragment = self.inline_fragment.rebase_on(parent_type, schema)?;
                // We should be able to rebase, or there is a bug, so error if that is the case.
                // If we rebased successfully then we add "non-included" __typename field selection
                // just to keep the query valid.
                let directives = DirectiveList::one(executable::Directive {
                    name: name!("include"),
                    arguments: vec![(name!("if"), false).into()],
                });
                let parent_typename_field = if let Some(condition) = this_condition {
                    condition.introspection_typename_field()
                } else {
                    parent_type.introspection_typename_field()
                };
                let typename_field_selection = Selection::from_field(
                    Field {
                        schema: schema.clone(),
                        field_position: parent_typename_field,
                        alias: None,
                        arguments: Default::default(),
                        directives,
                        sibling_typename: None,
                    },
                    None,
                );

                // Return `... [on <rebased condition>] { __typename @include(if: false) }`
                let rebased_casted_type = rebased_fragment.casted_type();
                return Ok(Some(SelectionOrSet::Selection(
                    InlineFragmentSelection::new(
                        rebased_fragment,
                        SelectionSet::from_selection(rebased_casted_type, typename_field_selection),
                    )
                    .into(),
                )));
            }
        }

        // Second, we check if some of the sub-selection fragments can be "lifted" outside of this fragment. This can happen if:
        // 1. the current fragment is an abstract type,
        // 2. the sub-fragment is an object type,
        // 3. the sub-fragment type is a valid runtime of the current type.
        if self.inline_fragment.directives.is_empty()
            && this_condition.is_some_and(|c| c.is_abstract_type())
        {
            let mut liftable_selections = SelectionMap::new();
            for selection in selection_set.selections.values() {
                match selection {
                    Selection::InlineFragment(inline_fragment_selection) => {
                        if let Some(type_condition) = &inline_fragment_selection
                            .inline_fragment
                            .type_condition_position
                            && type_condition.is_object_type()
                            && runtime_types_intersect(parent_type, type_condition, schema)
                        {
                            liftable_selections.insert(selection.clone());
                        };
                    }
                    _ => continue,
                }
            }

            // If we can lift all selections, then that just mean we can get rid of the current fragment altogether
            if liftable_selections.len() == selection_set.selections.len() {
                // Rebasing is necessary since this normalized sub-selection set changed its parent.
                let rebased_selection_set = selection_set.rebase_on(parent_type, schema)?;
                return Ok(Some(SelectionOrSet::SelectionSet(rebased_selection_set)));
            }

            // Otherwise, if there are "liftable" selections, we must return a set comprised of those lifted selection,
            // and the current fragment _without_ those lifted selections.
            if !liftable_selections.is_empty() {
                // Converting `... [on T] { <liftable_selections> <non-liftable_selections> }` into
                // `{ ... [on T] { <non-liftable_selections> } <liftable_selections> }`.
                // PORT_NOTE: It appears that this lifting could be repeatable (meaning lifted
                // selection could be broken down further and lifted again), but
                // flatten_unnecessary_fragments is not
                // applied recursively. This could be worth investigating.
                let rebased_inline_fragment =
                    self.inline_fragment.rebase_on(parent_type, schema)?;

                let mut nonliftable_selections = selection_set.selections;
                Arc::make_mut(&mut nonliftable_selections)
                    .retain(|k, _| !liftable_selections.contains_key(k));

                let rebased_casted_type = rebased_inline_fragment.casted_type();
                let final_inline_fragment: Selection = InlineFragmentSelection::new(
                    rebased_inline_fragment,
                    SelectionSet {
                        schema: schema.clone(),
                        type_position: rebased_casted_type,
                        selections: nonliftable_selections,
                    },
                )
                .into();

                // Since liftable_selections are changing their parent, we need to rebase them.
                liftable_selections = liftable_selections
                    .into_values()
                    .map(|sel| sel.rebase_on(parent_type, schema))
                    .collect::<Result<_, _>>()?;

                let mut final_selection_map = SelectionMap::new();
                final_selection_map.extend(liftable_selections);
                final_selection_map.insert(final_inline_fragment);
                let final_selections = SelectionSet {
                    schema: schema.clone(),
                    type_position: parent_type.clone(),
                    selections: final_selection_map.into(),
                };
                return Ok(Some(SelectionOrSet::SelectionSet(final_selections)));
            }
        }

        if self.inline_fragment.schema == *schema
            && self.inline_fragment.parent_type_position == *parent_type
            && self.selection_set == selection_set
        {
            // flattening did not change the fragment
            Ok(Some(Selection::InlineFragment(Arc::clone(self)).into()))
        } else {
            let rebased_inline_fragment = self.inline_fragment.rebase_on(parent_type, schema)?;
            let rebased_casted_type = rebased_inline_fragment.casted_type();
            // Re-flatten with the rebased casted type, which could further flatten away.
            let selection_set =
                selection_set.flatten_unnecessary_fragments(&rebased_casted_type, schema)?;
            if selection_set.is_empty() {
                Ok(None)
            } else {
                // We need to rebase since the parent type for the selection set could be
                // changed.
                // Note: Rebasing after flattening, since rebasing before that can error out.
                //       Or, `flatten_unnecessary_fragments` could `rebase` at the same time.
                let rebased_selection_set =
                    selection_set.rebase_on(&rebased_casted_type, schema)?;
                Ok(Some(
                    Selection::InlineFragment(Arc::new(InlineFragmentSelection::new(
                        rebased_inline_fragment,
                        rebased_selection_set,
                    )))
                    .into(),
                ))
            }
        }
    }
}

impl SelectionSet {
    /// Simplify this selection set in the context of the provided `parent_type`.
    ///
    /// This removes unnecessary/redundant inline fragments, so that for instance, with a schema:
    /// ```graphql
    /// type Query {
    ///   t1: T1
    ///   i: I
    /// }
    ///
    /// interface I {
    ///   id: ID!
    /// }
    ///
    /// type T1 implements I {
    ///   id: ID!
    ///   v1: Int
    /// }
    ///
    /// type T2 implements I {
    ///   id: ID!
    ///   v2: Int
    /// }
    /// ```
    /// We can perform following simplification:
    /// ```graphql
    /// flatten_unnecessary_fragments({
    ///   t1 {
    ///     ... on I {
    ///       id
    ///     }
    ///   }
    ///   i {
    ///     ... on T1 {
    ///       ... on I {
    ///         ... on T1 {
    ///           v1
    ///         }
    ///         ... on T2 {
    ///           v2
    ///         }
    ///       }
    ///     }
    ///     ... on T2 {
    ///       ... on I {
    ///         id
    ///       }
    ///     }
    ///   }
    /// }) === {
    ///   t1 {
    ///     id
    ///   }
    ///   i {
    ///     ... on T1 {
    ///       v1
    ///     }
    ///     ... on T2 {
    ///       id
    ///     }
    ///   }
    /// }
    /// ```
    ///
    /// For this operation to be valid (to not throw), `parent_type` must be such that every field selection in
    /// this selection set is such that its type position intersects with passed `parent_type` (there is no limitation
    /// on the fragment selections, though any fragment selections whose condition do not intersects `parent_type`
    /// will be discarded). Note that `self.flatten_unnecessary_fragments(self.type_condition)` is always valid and useful, but it is
    /// also possible to pass a `parent_type` that is more "restrictive" than the selection current type position
    /// (as long as the top-level fields of this selection set can be rebased on that type).
    ///
    // PORT_NOTE: this is now module-private, because it looks like it *can* be. If some place
    // outside this module *does* need it, feel free to mark it pub(crate).
    // PORT_NOTE: in JS, this was called "normalize".
    // PORT_NOTE: in JS, this had a `recursive: false` flag, which would only apply the
    // simplification at the top level. This appears to be unused.
    pub(super) fn flatten_unnecessary_fragments(
        &self,
        parent_type: &CompositeTypeDefinitionPosition,
        schema: &ValidFederationSchema,
    ) -> Result<SelectionSet, FederationError> {
        let mut normalized_selections = Self {
            schema: schema.clone(),
            type_position: parent_type.clone(),
            selections: Default::default(), // start empty
        };
        for selection in self.selections.values() {
            if let Some(selection_or_set) =
                selection.flatten_unnecessary_fragments(parent_type, schema)?
            {
                match selection_or_set {
                    SelectionOrSet::Selection(normalized_selection) => {
                        normalized_selections.add_local_selection(&normalized_selection)?;
                    }
                    SelectionOrSet::SelectionSet(normalized_set) => {
                        // Since the `selection` has been expanded/lifted, we use
                        // `add_selection_set` to make sure it's rebased.
                        normalized_selections.add_selection_set(&normalized_set)?;
                    }
                }
            }
        }
        Ok(normalized_selections)
    }
}

#[cfg(test)]
mod tests {
    use apollo_compiler::Schema;

    use super::*;
    use crate::operation::Operation;

    #[test]
    fn does_not_duplicate_fragments_regression_router782() {
        let schema = Schema::parse_and_validate(
            r#"
interface Node {
  id: ID!
}
interface HasNodes {
  nodes: [Node!]!
}
type MySpecialNode implements Node & HasNodes {
  id: ID!
  value: String
  nodes: [Node]
}
type Query {
  nodes: [Node]
}
        "#,
            "apischema.graphql",
        )
        .unwrap();
        let schema = ValidFederationSchema::new(schema).unwrap();

        // Below, the second `... on MySpecialNode` is redundant,
        // the same selection is already guaranteed by the first.
        let operation = Operation::parse(
            schema.clone(),
            r#"
{
  nodes {
    __typename
    ... on MySpecialNode {
      value
    }
    ... on HasNodes {
      ... on Node {
        __typename
        ... on MySpecialNode {
          value
        }
      }
    }
  }
}
        "#,
            "query.graphql",
        )
        .unwrap();

        let expanded_and_flattened = operation
            .selection_set
            .flatten_unnecessary_fragments(&operation.selection_set.type_position, &schema)
            .unwrap();

        // Use apollo-compiler's selection set printer directly instead of the minimized
        // apollo-federation printer
        let compiler_set = executable::SelectionSet::try_from(&expanded_and_flattened).unwrap();

        insta::assert_snapshot!(compiler_set, @r#"
            {
              nodes {
                __typename
                ... on MySpecialNode {
                  value
                }
                ... on HasNodes {
                  ... on Node {
                    __typename
                  }
                }
              }
            }
        "#);
    }
}