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
mod best_path;
pub(crate) mod error;
mod excluded;
pub(crate) mod path;
pub(crate) mod pathfinder;
pub(crate) mod utils;

use std::collections::{HashSet, VecDeque};

use crate::query_planner::{
    ast::{
        merge_path::Condition,
        operation::OperationDefinition,
        selection_item::SelectionItem,
        selection_set::{FieldSelection, InlineFragmentSelection, SelectionSet},
    },
    graph::{
        edge::{Edge, PlannerOverrideContext},
        node::Node,
        Graph,
    },
    planner::walker::pathfinder::{find_self_referencing_direct_path, NavigationTarget},
    state::supergraph_state::{OperationKind, SupergraphState},
    utils::cancellation::CancellationToken,
};
use best_path::{find_best_paths, BestPathTracker};
use error::WalkOperationError;
use excluded::ExcludedFromLookup;
use path::OperationPath;
use pathfinder::{find_direct_paths, find_indirect_paths};
use tracing::{instrument, span, trace, Level};
use utils::get_entrypoints;

pub struct ResolvedOperation<'graph> {
    pub operation_kind: OperationKind,
    pub root_field_groups: Vec<BestPathsPerLeaf<'graph>>,
}

// TODO: Make a better struct
pub type BestPathsPerLeaf<'graph> = Vec<Vec<OperationPath<'graph>>>;

// TODO: Consider to use VecDeque(fixed_size) if we can predict it?
// TODO: Consider to drop this IR layer and just go with QTP directly.

type WorkItem<'graph, 'op> = (&'op SelectionItem, Vec<OperationPath<'graph>>);
type ResolutionStack<'graph, 'op> = Vec<WorkItem<'graph, 'op>>;

#[instrument(level = "trace", skip_all)]
pub fn walk_operation<'graph, 'op: 'graph>(
    graph: &'graph Graph,
    supergraph: &'graph SupergraphState,
    override_context: &'graph PlannerOverrideContext,
    operation: &'op OperationDefinition,
    cancellation_token: &'graph CancellationToken,
) -> Result<ResolvedOperation<'graph>, WalkOperationError> {
    let operation_kind = operation
        .operation_kind
        .clone()
        .unwrap_or(OperationKind::Query);
    let (op_type, selection_set) = operation.parts();
    trace!("operation is of type {:?}", op_type);

    let root_entrypoints = get_entrypoints(graph, op_type)?;
    let initial_paths: Vec<OperationPath<'graph>> = root_entrypoints
        .iter()
        .map(|edge| OperationPath::new_entrypoint(edge))
        .collect();

    let mut paths_grouped_by_root_field: Vec<BestPathsPerLeaf> =
        Vec::with_capacity(operation.selection_set.items.len());

    // It's critical to iterate over root fiels and preserve their original order
    for selection_item in selection_set.items.iter() {
        let mut stack_to_resolve: VecDeque<WorkItem> = VecDeque::new();

        stack_to_resolve.push_back((selection_item, initial_paths.to_vec()));

        let mut paths_per_leaf: Vec<Vec<OperationPath<'graph>>> = vec![];

        while let Some((selection_item, paths)) = stack_to_resolve.pop_front() {
            cancellation_token.bail_if_cancelled()?;
            let (next_stack_to_resolve, new_paths_per_leaf) = process_selection(
                graph,
                supergraph,
                override_context,
                selection_item,
                &paths,
                &[],
                cancellation_token,
            )?;

            paths_per_leaf.extend(new_paths_per_leaf);
            for item in next_stack_to_resolve.into_iter().rev() {
                stack_to_resolve.push_front(item);
            }
        }

        paths_grouped_by_root_field.push(paths_per_leaf);
    }

    Ok(ResolvedOperation {
        operation_kind,
        root_field_groups: paths_grouped_by_root_field,
    })
}

fn process_selection<'graph, 'op: 'graph>(
    graph: &'graph Graph,
    supergraph: &'graph SupergraphState,
    override_context: &'graph PlannerOverrideContext,
    selection_item: &'op SelectionItem,
    paths: &[OperationPath<'graph>],
    fields_to_resolve_locally: &[String],
    cancellation_token: &'graph CancellationToken,
) -> Result<
    (
        ResolutionStack<'graph, 'op>,
        Vec<Vec<OperationPath<'graph>>>,
    ),
    WalkOperationError,
> {
    let mut stack_to_resolve: ResolutionStack = vec![];
    let mut paths_per_leaf: Vec<Vec<OperationPath<'graph>>> = vec![];

    match selection_item {
        SelectionItem::InlineFragment(fragment) => {
            let (next_selection_items, new_paths_per_leaf) = process_inline_fragment(
                graph,
                supergraph,
                override_context,
                fragment,
                paths,
                fields_to_resolve_locally,
                cancellation_token,
            )?;
            paths_per_leaf.extend(new_paths_per_leaf);
            stack_to_resolve.extend(next_selection_items);
        }
        SelectionItem::Field(field) => {
            let (next_selection_items, new_paths_per_leaf) = process_field(
                graph,
                supergraph,
                override_context,
                field,
                paths,
                fields_to_resolve_locally,
                cancellation_token,
            )?;
            paths_per_leaf.extend(new_paths_per_leaf);
            stack_to_resolve.extend(next_selection_items);
        }
        SelectionItem::FragmentSpread(_) => {
            // No processing needed for FragmentSpread
        }
    }

    Ok((stack_to_resolve, paths_per_leaf))
}

#[instrument(level = "trace", skip_all)]
fn process_selection_set<'graph, 'op: 'graph>(
    graph: &'graph Graph,
    supergraph: &'graph SupergraphState,
    override_context: &'graph PlannerOverrideContext,
    selection_set: &'op SelectionSet,
    paths: &[OperationPath<'graph>],
    fields_to_resolve_locally: &[String],
    cancellation_token: &'graph CancellationToken,
) -> Result<
    (
        ResolutionStack<'graph, 'op>,
        Vec<Vec<OperationPath<'graph>>>,
    ),
    WalkOperationError,
> {
    let mut stack_to_resolve: ResolutionStack = vec![];
    let mut paths_per_leaf: Vec<Vec<OperationPath<'graph>>> = vec![];

    for item in selection_set.items.iter() {
        let (next_stack_to_resolve, new_paths_per_leaf) = process_selection(
            graph,
            supergraph,
            override_context,
            item,
            paths,
            fields_to_resolve_locally,
            cancellation_token,
        )?;
        paths_per_leaf.extend(new_paths_per_leaf);
        stack_to_resolve.extend(next_stack_to_resolve);
    }

    Ok((stack_to_resolve, paths_per_leaf))
}

#[instrument(level = "trace", skip_all, fields(
  type_condition = fragment.type_condition,
))]
fn process_inline_fragment<'graph, 'op: 'graph>(
    graph: &'graph Graph,
    supergraph: &'graph SupergraphState,
    override_context: &'graph PlannerOverrideContext,
    fragment: &'op InlineFragmentSelection,
    paths: &[OperationPath<'graph>],
    fields_to_resolve_locally: &[String],
    cancellation_token: &'graph CancellationToken,
) -> Result<
    (
        ResolutionStack<'graph, 'op>,
        Vec<Vec<OperationPath<'graph>>>,
    ),
    WalkOperationError,
> {
    trace!(
        "Processing inline fragment '{}' on type '{}' (skip: {:?}, include: {:?}) through {} possible paths",
        fragment.selections,
        fragment.type_condition,
        fragment.include_if,
        fragment.skip_if,
        paths.len()
    );

    cancellation_token.bail_if_cancelled()?;

    // if the current type is an object type we ignore an abstract move
    // but if it's a union, we need to find an abstract move to the target type
    let tail_index = graph.get_edge_tail(
        &paths
            .first()
            .unwrap()
            .last_segment
            .as_ref()
            .unwrap()
            .edge_index,
    )?;

    // if type condition if matching the tail's type name,
    // ignore the fragment and do not check if `... on X` is possible.
    // In case of
    //  - interfaces - `... on Interface` - we look for interface's fields.
    //  - object types - `... on Object`-  we look for object's fields.
    //  - union types - `... on Union` will cause a graphql validation error.
    // We don't need to worry about correctness here as it's handled by graphql validations.
    let tail = graph.node(tail_index)?;
    let tail_type_name = match tail {
        Node::SubgraphType(t) => &t.name,
        _ => panic!("Expected a subgraph type when resolving fragments"),
    };

    if tail_type_name == &fragment.type_condition {
        // It's the same type and no conditions are applied, we can skip the fragment processing
        // and go directly to its selections.
        if fragment.include_if.is_none() && fragment.skip_if.is_none() {
            return process_selection_set(
                graph,
                supergraph,
                override_context,
                &fragment.selections,
                paths,
                fields_to_resolve_locally,
                cancellation_token,
            );
        }

        // Looks like the fragment has conditions, we need to process them differently.
        // We aim to preserve the inline fragment due to conditions, instead of eliminating it,
        // and jumping straight to its selections.
        let condition: Option<Condition> = fragment.into();

        let mut next_paths: Vec<OperationPath<'graph>> = Vec::with_capacity(paths.len());
        for path in paths {
            let path_span = span!(
                Level::TRACE,
                "explore_path",
                path = path.pretty_print(graph)
            );
            let _enter = path_span.enter();

            // Find a direct path that references the same type as the current tail,
            let direct_path = find_self_referencing_direct_path(
                graph,
                supergraph,
                override_context,
                path,
                &fragment.type_condition,
                condition.as_ref().expect("Condition should be present"),
                cancellation_token,
            )?;

            trace!("advanced: {}", path.pretty_print(graph));

            next_paths.push(direct_path);
        }

        // Now process the selections under the fragment using the advanced paths
        return process_selection_set(
            graph,
            supergraph,
            override_context,
            &fragment.selections,
            &next_paths,
            fields_to_resolve_locally,
            cancellation_token,
        );
    }

    trace!(
        "Trying to advance to: ... on {}, through {} possible paths",
        fragment.type_condition,
        paths.len()
    );

    let mut next_paths: Vec<OperationPath<'graph>> = Vec::with_capacity(paths.len());
    for path in paths {
        let path_span = span!(
            Level::TRACE,
            "explore_path",
            path = path.pretty_print(graph)
        );
        let _enter = path_span.enter();

        let mut direct_paths = find_direct_paths(
            graph,
            supergraph,
            override_context,
            path,
            &NavigationTarget::ConcreteType(&fragment.type_condition, fragment.into()),
            cancellation_token,
        )?;

        trace!("Direct paths found: {}", direct_paths.len());
        if !direct_paths.is_empty() {
            trace!("advanced: {}", path.pretty_print(graph));
            next_paths.push(direct_paths.remove(0));
        }

        if fields_to_resolve_locally.is_empty() {
            let mut indirect_paths = find_indirect_paths(
                graph,
                supergraph,
                override_context,
                path,
                &NavigationTarget::ConcreteType(&fragment.type_condition, fragment.into()),
                &ExcludedFromLookup::new(),
                cancellation_token,
            )?;

            if !indirect_paths.is_empty() {
                trace!("advanced: {}", path.pretty_print(graph));
                next_paths.push(indirect_paths.remove(0));
            }

            if indirect_paths.is_empty() && direct_paths.is_empty() {
                // Looks like a union member or an interface implementation is not resolvable.
                // The fact the fragment for that object type passed GraphQL validations,
                // means that it's a child of the abstract type,
                // and it was probably eliminated from the Graph because of intersection.
                trace!(
                    "Object type '{}' is not resolvable by '{}', resolve only the __typename",
                    fragment.type_condition,
                    tail_type_name
                );
            }
        }
    }

    if next_paths.is_empty() {
        let mut tracker = BestPathTracker::new(graph);

        for path in paths {
            let path_span = span!(
                Level::TRACE,
                "explore_path",
                path = path.pretty_print(graph)
            );
            let _enter = path_span.enter();
            let direct_paths = find_direct_paths(
                graph,
                supergraph,
                override_context,
                path,
                &NavigationTarget::Field {
                    field: &FieldSelection::new_typename(),
                    target_subgraph_ids: None,
                },
                cancellation_token,
            )?;

            trace!("Direct paths found: {}", direct_paths.len());

            if !direct_paths.is_empty() {
                for p in direct_paths {
                    tracker.add(&p)?;
                }
            } else {
                return Err(WalkOperationError::NoPathsFound("__typename".to_string()));
            }
        }

        let next_paths = tracker.get_best_paths();

        if next_paths.is_empty() {
            return Err(WalkOperationError::NoPathsFound("__typename".to_string()));
        }

        return Ok((vec![], vec![find_best_paths(next_paths)]));
    }

    process_selection_set(
        graph,
        supergraph,
        override_context,
        &fragment.selections,
        &next_paths,
        fields_to_resolve_locally,
        cancellation_token,
    )
}

/// The same union may have different members in different subgraphs.
/// For example:
///
/// - graph A: Action = Common | OnlyA
/// - graph B: Action = Common | OnlyB
///
/// If the planner still has valid candidate paths through both A and B,
/// the response shape must not depend on which path wins later.
/// In that case we keep only members that are available in every candidate graph.
///
/// In the example above, only `Common` is safe to keep.
fn narrow_partial_union_paths<'graph>(paths: &mut Vec<OperationPath<'graph>>) {
    if paths.len() <= 1 {
        return;
    }

    let Some(union_context) = paths.first().and_then(|path| path.union_context.as_ref()) else {
        return;
    };

    let all_same_field = paths.iter().all(|path| {
        path.union_context
            .as_ref()
            .is_some_and(|ctx| ctx.eq_field(union_context))
    });

    if !all_same_field {
        return;
    }

    // Collect the union members that each subgraph can return.
    //
    // All paths through the same graph share the same `possible_members` because they originate
    // from the same `UnionMembersData` (one per graph). We only need to record each graph's member
    // set once — no accumulation is required.
    type GraphId<'graph> = &'graph str;
    type MembersPerGraph<'graph> = Vec<(GraphId<'graph>, Vec<&'graph str>)>;
    let mut members_per_graph: MembersPerGraph<'graph> = Vec::new();

    for path in paths.iter() {
        let context = path
            .union_context
            .as_ref()
            // It's safe to unwrap as `all_same_field` checked that all paths have the same context
            .expect("union member context should exist at this point");

        if !members_per_graph
            .iter()
            .any(|(graph_id, _)| graph_id == &context.graph_id)
        {
            members_per_graph.push((context.graph_id, context.possible_members.clone()));
        }
    }

    if members_per_graph.len() <= 1 {
        return;
    }

    let (_, least_members_set) = members_per_graph
        .iter()
        .min_by_key(|(_, members)| members.len())
        // It's safe as we checked that `members_per_graph` has at least two entries above
        .expect("members_per_graph has at least two entries");

    // Start from the smallest member list.
    // Then shrink it with each graph until only shared members remain.
    let mut shared_members: Vec<&'graph str> = least_members_set.clone();

    for (_, members) in &members_per_graph {
        shared_members.retain(|member| members.contains(member));

        if shared_members.is_empty() {
            // No union member exists in every candidate subgraph.
            // None of these paths is safe to keep.
            paths.clear();
            return;
        }
    }

    paths.retain_mut(|path| {
        // It's safe to unwrap as we checked that `union_context` exists already
        let context = path
            .union_context
            .as_mut()
            .expect("union context should exist at this point");

        // Keep only paths whose current member is shared
        if !shared_members.contains(&context.member_name) {
            return false;
        }

        // Update the possible members to the shared members set.
        context.possible_members = shared_members.clone();
        true
    });
}

#[instrument(level = "trace", skip_all, fields(
  field_name = &field.name,
  leaf = field.is_leaf()
))]
fn process_field<'graph, 'op: 'graph>(
    graph: &'graph Graph,
    supergraph: &'graph SupergraphState,
    override_context: &'graph PlannerOverrideContext,
    field: &'op FieldSelection,
    paths: &[OperationPath<'graph>],
    fields_to_resolve_locally: &[String],
    cancellation_token: &'graph CancellationToken,
) -> Result<
    (
        ResolutionStack<'graph, 'op>,
        Vec<Vec<OperationPath<'graph>>>,
    ),
    WalkOperationError,
> {
    let mut next_stack_to_resolve: ResolutionStack = vec![];
    let mut paths_per_leaf: Vec<Vec<OperationPath<'graph>>> = vec![];
    let mut tracker = BestPathTracker::new(graph);

    trace!(
        "Trying to advance to: {} through {} possible paths",
        field,
        paths.len()
    );

    cancellation_token.bail_if_cancelled()?;

    let target_subgraph_ids =
        if !paths.is_empty() && !fields_to_resolve_locally.contains(&field.name) {
            field_target_subgraph_ids(supergraph, field, paths, graph)?
        } else {
            None
        };

    for path in paths {
        let path_span = span!(
            Level::TRACE,
            "explore_path",
            path = path.pretty_print(graph)
        );
        let _enter = path_span.enter();

        let mut advanced = false;

        let excluded = ExcludedFromLookup::new();
        let direct_paths = find_direct_paths(
            graph,
            supergraph,
            override_context,
            path,
            &NavigationTarget::Field {
                field,
                target_subgraph_ids: None,
            },
            cancellation_token,
        )?;
        trace!("Direct paths found: {}", direct_paths.len());

        let found_direct_paths_to_leaf = !direct_paths.is_empty() && field.is_leaf();

        if !direct_paths.is_empty() {
            advanced = true;
            for direct_path in &direct_paths {
                tracker.add(direct_path)?;
            }
        }

        if !fields_to_resolve_locally.contains(&field.name) && !found_direct_paths_to_leaf {
            let indirect_paths = find_indirect_paths(
                graph,
                supergraph,
                override_context,
                path,
                &NavigationTarget::Field {
                    field,
                    target_subgraph_ids: target_subgraph_ids.as_ref(),
                },
                &excluded,
                cancellation_token,
            )?;
            trace!("Indirect paths found: {}", indirect_paths.len());

            if !indirect_paths.is_empty() {
                advanced = true;
                for indirect_path in indirect_paths {
                    tracker.add(&indirect_path)?;
                }
            }
        }

        trace!(
            "{}: {}",
            if advanced {
                "advanced"
            } else {
                "failed to advance"
            },
            path.pretty_print(graph)
        );
    }

    let mut next_paths = tracker.get_best_paths();
    narrow_partial_union_paths(&mut next_paths);
    if next_paths.is_empty() {
        return Err(WalkOperationError::NoPathsFound(field.name.to_string()));
    }

    let mut fields_to_resolve_locally: Vec<String> = Vec::new();
    if !field.is_leaf() {
        let field_move_paths: Vec<_> = next_paths
            .iter()
            .filter(|path| {
                path.last_segment.as_ref().is_some_and(|seg| {
                    matches!(graph.edge(seg.edge_index).unwrap(), Edge::FieldMove(_))
                })
            })
            .collect();

        if !field_move_paths.is_empty() {
            let edge_index = field_move_paths[0]
                .last_segment
                .as_ref()
                .unwrap()
                .edge_index;

            let head_index = graph.get_edge_head(&edge_index)?;
            let tail_index = graph.get_edge_tail(&edge_index)?;

            let head = graph.node(head_index)?;
            let tail = graph.node(tail_index)?;

            let parent_type_name = head.name_str();
            let parent_def = supergraph
                .definitions
                .get(parent_type_name)
                .ok_or_else(|| WalkOperationError::TypeNotFound(parent_type_name.to_string()))?;

            let field_def = parent_def.fields().get(&field.name).ok_or_else(|| {
                WalkOperationError::FieldNotFound(
                    field.name.to_string(),
                    parent_type_name.to_string(),
                )
            })?;

            let output_type = supergraph
                .definitions
                .get(tail.name_str())
                .ok_or_else(|| WalkOperationError::TypeNotFound(tail.name_str().to_string()))?;

            if output_type.is_interface_type()
                && field_def.resolvable_in_graphs(parent_def).len() > 1
                // if there's one fragment, the query planner can decide which subgraph to use, based on the fragment's type condition
                && field
                    .selections
                    .items
                    .iter()
                    .filter(|item| matches!(item, SelectionItem::InlineFragment(_)))
                    .count()
                    > 1
            {
                fields_to_resolve_locally = output_type
                    .fields()
                    .keys()
                    .map(|name| name.to_string())
                    .collect();
            }
        }
    }

    if !fields_to_resolve_locally.is_empty() {
        let path_span = span!(
            Level::TRACE,
            "Shareable interface detected. Validating that sub-selections can be resolved from a single path."
        );
        let _enter = path_span.enter();
        let mut valid_paths_for_children: Vec<OperationPath<'graph>> =
            Vec::with_capacity(next_paths.len());
        for candidate_path in &next_paths {
            let mut all_children_resolvable = true;
            // We don't need the results of the sub-walk here, only whether it was successful
            for child_selection in &field.selections.items {
                let finding = process_selection(
                    graph,
                    supergraph,
                    override_context,
                    child_selection,
                    std::slice::from_ref(candidate_path),
                    &fields_to_resolve_locally,
                    cancellation_token,
                );

                match finding {
                    Ok((child_stack, child_leaves)) => {
                        if child_leaves.is_empty() && child_stack.is_empty() {
                            all_children_resolvable = false;
                            trace!(
                                "Path {} failed to resolve child '{}' locally.",
                                candidate_path.pretty_print(graph),
                                child_selection
                            );
                            break; // This candidate_path is invalid.
                        }
                    }
                    Err(_) => {
                        all_children_resolvable = false;
                        break; // This candidate_path is invalid.
                    }
                }
            }

            if all_children_resolvable {
                trace!(
                    "Path {} can resolve all children locally and is valid.",
                    candidate_path.pretty_print(graph)
                );
                valid_paths_for_children.push(candidate_path.clone());
            }
        }
        next_paths = valid_paths_for_children;
        if !field.is_leaf() && next_paths.is_empty() {
            // If no single path could satisfy all children, we have no valid way forward.
            return Err(WalkOperationError::NoPathsFound(field.name.to_string()));
        }
    }

    if field.is_leaf() {
        paths_per_leaf.push(find_best_paths(next_paths));
    } else {
        trace!("Found {} paths", next_paths.len());
        for next_selection_items in &field.selections.items {
            next_stack_to_resolve.push((next_selection_items, next_paths.clone()));
        }
    }

    Ok((next_stack_to_resolve, paths_per_leaf))
}

fn field_target_subgraph_ids<'graph>(
    supergraph: &SupergraphState,
    field: &FieldSelection,
    paths: &[OperationPath<'graph>],
    graph: &'graph Graph,
) -> Result<Option<HashSet<String>>, WalkOperationError> {
    let mut parent_type_names = HashSet::new();

    for path in paths {
        let parent_node = graph.node(path.tail())?;
        parent_type_names.insert(parent_node.name_str());
    }

    let mut target_subgraph_ids = HashSet::new();

    for parent_type_name in parent_type_names {
        let Some(parent_def) = supergraph.definitions.get(parent_type_name) else {
            continue;
        };
        let Some(field_def) = parent_def.fields().get(&field.name) else {
            continue;
        };

        for graph_id in field_def.resolvable_in_graphs(parent_def) {
            if let Ok(subgraph_id) = supergraph.resolve_graph_id(&graph_id) {
                target_subgraph_ids.insert(subgraph_id.0.to_string());
            }
        }
    }

    if target_subgraph_ids.is_empty() {
        Ok(None)
    } else {
        Ok(Some(target_subgraph_ids))
    }
}