fraiseql-cli 2.2.0

CLI tools for FraiseQL v2 - Schema compilation and development utilities
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
#![allow(clippy::unwrap_used)] // Reason: test/bench code, panics are acceptable
//! CLI composition functionality tests
//! - Compose multiple subgraph schemas into supergraph
//! - CLI argument parsing (--subgraph, --output)
//! - Configuration file support (fraiseql.yml)
//! - Conflict resolution strategies
//! - Error reporting with helpful suggestions
//! - Output validation

use std::{collections::HashMap, fmt::Write};

use fraiseql_core::federation::types::{FederatedType, FederationMetadata, KeyDirective};

// ============================================================================
// Test: CLI Argument Parsing
// ============================================================================

#[test]
fn test_parse_compose_arguments_single_subgraph() {
    // TEST: Parse single subgraph argument
    // GIVEN: --subgraph users:users.json
    // WHEN: Parsing arguments
    // THEN: Should extract name and path

    let args = vec![
        "fraiseql".to_string(),
        "compose".to_string(),
        "--subgraph".to_string(),
        "users:users.json".to_string(),
    ];

    let result = parse_compose_args(&args);
    assert!(result.is_ok(), "Should parse single subgraph argument");

    let parsed = result.unwrap();
    assert_eq!(parsed.subgraphs.len(), 1);
    assert_eq!(parsed.subgraphs[0].name, "users");
    assert_eq!(parsed.subgraphs[0].path, "users.json");
}

#[test]
fn test_parse_compose_arguments_multiple_subgraphs() {
    // TEST: Parse multiple subgraph arguments
    // GIVEN: --subgraph users:users.json --subgraph orders:orders.json
    // WHEN: Parsing arguments
    // THEN: Should extract all subgraphs

    let args = vec![
        "fraiseql".to_string(),
        "compose".to_string(),
        "--subgraph".to_string(),
        "users:users.json".to_string(),
        "--subgraph".to_string(),
        "orders:orders.json".to_string(),
    ];

    let parsed = parse_compose_args(&args).expect("should parse multiple subgraph arguments");
    assert_eq!(parsed.subgraphs.len(), 2);
    assert_eq!(parsed.subgraphs[0].name, "users");
    assert_eq!(parsed.subgraphs[1].name, "orders");
}

#[test]
fn test_parse_compose_arguments_with_output() {
    // TEST: Parse output argument
    // GIVEN: --subgraph users:users.json --output supergraph.json
    // WHEN: Parsing arguments
    // THEN: Should extract output path

    let args = vec![
        "fraiseql".to_string(),
        "compose".to_string(),
        "--subgraph".to_string(),
        "users:users.json".to_string(),
        "--output".to_string(),
        "supergraph.json".to_string(),
    ];

    let parsed = parse_compose_args(&args).expect("should parse output argument");
    assert_eq!(parsed.output_path, Some("supergraph.json".to_string()));
}

#[test]
fn test_parse_compose_arguments_invalid_subgraph_format() {
    // TEST: Reject invalid subgraph format
    // GIVEN: --subgraph users_no_colon
    // WHEN: Parsing arguments
    // THEN: Should reject with helpful error

    let args = vec![
        "fraiseql".to_string(),
        "compose".to_string(),
        "--subgraph".to_string(),
        "users_no_colon".to_string(),
    ];

    let result = parse_compose_args(&args);
    assert!(result.is_err(), "Should reject subgraph argument without colon");

    let err = result.unwrap_err();
    assert!(
        err.to_lowercase().contains("format") || err.to_lowercase().contains("subgraph"),
        "Error should mention format or subgraph: {}",
        err
    );
}

#[test]
fn test_parse_compose_arguments_missing_subgraphs() {
    // TEST: Reject command with no subgraphs
    // GIVEN: compose command with no --subgraph arguments
    // WHEN: Parsing arguments
    // THEN: Should reject with helpful error

    let args = vec!["fraiseql".to_string(), "compose".to_string()];

    let result = parse_compose_args(&args);
    assert!(result.is_err(), "Should require at least one subgraph");

    let err = result.unwrap_err();
    assert!(
        err.to_lowercase().contains("subgraph") || err.to_lowercase().contains("required"),
        "Error should mention subgraph requirement: {}",
        err
    );
}

// ============================================================================
// Test: Configuration File Loading
// ============================================================================

#[test]
fn test_load_compose_configuration_default() {
    // TEST: Load default configuration when no config file
    // GIVEN: No fraiseql.yml file
    // WHEN: Loading configuration
    // THEN: Should return default configuration

    let result = load_compose_config(None);
    assert!(result.is_ok(), "Should load default configuration");

    let config = result.unwrap();
    assert_eq!(config.conflict_resolution, "error", "Default should be error on conflict");
}

#[test]
fn test_load_compose_configuration_from_file() {
    // TEST: Load configuration from fraiseql.yml
    // GIVEN: fraiseql.yml with custom conflict resolution
    // WHEN: Loading configuration
    // THEN: Should load custom settings

    // Note: In real implementation, would read actual YAML file
    // For testing, we simulate config loading
    let config_content = r"
composition:
  conflict_resolution: shareable
  validation: true
";

    let result = parse_config_yaml(config_content);
    assert!(result.is_ok(), "Should parse YAML configuration");

    let config = result.unwrap();
    assert_eq!(config.conflict_resolution, "shareable");
}

// ============================================================================
// Test: Conflict Resolution Strategies
// ============================================================================

#[test]
fn test_resolve_conflict_strategy_error() {
    // TEST: Strategy "error" rejects any conflicts
    // GIVEN: Conflicting field types in subgraphs
    // WHEN: Using error resolution strategy
    // THEN: Should fail composition with error message

    let config = ComposeConfig {
        conflict_resolution: "error".to_string(),
        validation:          true,
        subgraph_priority:   vec![],
    };

    // Simulate conflict detection
    let conflict = FieldTypeConflict {
        field_name: "email".to_string(),
        type_name:  "User".to_string(),
        subgraph1:  ("users".to_string(), "String".to_string()),
        subgraph2:  ("auth".to_string(), "Int".to_string()),
    };

    let result = resolve_conflict(&conflict, &config);
    assert!(result.is_err(), "Error strategy should reject conflicts");
}

#[test]
fn test_resolve_conflict_strategy_first_wins() {
    // TEST: Strategy "first_wins" uses first definition
    // GIVEN: Conflicting field types
    // WHEN: Using first_wins resolution strategy
    // THEN: Should use first subgraph's type

    let config = ComposeConfig {
        conflict_resolution: "first_wins".to_string(),
        validation:          true,
        subgraph_priority:   vec!["users".to_string(), "auth".to_string()],
    };

    let conflict = FieldTypeConflict {
        field_name: "email".to_string(),
        type_name:  "User".to_string(),
        subgraph1:  ("users".to_string(), "String".to_string()),
        subgraph2:  ("auth".to_string(), "Int".to_string()),
    };

    let result = resolve_conflict(&conflict, &config);
    assert!(result.is_ok(), "first_wins strategy should accept");

    let resolution = result.unwrap();
    assert_eq!(resolution.chosen_type, "String", "Should choose first type");
    assert_eq!(resolution.chosen_subgraph, "users");
}

#[test]
fn test_resolve_conflict_strategy_shareable() {
    // TEST: Strategy "shareable" allows @shareable fields
    // GIVEN: Conflicting field types both marked @shareable
    // WHEN: Using shareable resolution strategy
    // THEN: Should allow conflict

    let config = ComposeConfig {
        conflict_resolution: "shareable".to_string(),
        validation:          true,
        subgraph_priority:   vec![],
    };

    let conflict = FieldTypeConflict {
        field_name: "email".to_string(),
        type_name:  "User".to_string(),
        subgraph1:  ("users".to_string(), "String".to_string()),
        subgraph2:  ("auth".to_string(), "Int".to_string()),
    };

    let _result = resolve_conflict(&conflict, &config);
    // Shareable strategy currently returns first subgraph's type without
    // verifying @shareable directive presence. This test documents that
    // behavior for future @shareable validation work.
}

// ============================================================================
// Test: Compose Workflow
// ============================================================================

#[test]
fn test_compose_workflow_basic() {
    // TEST: Full compose workflow
    // GIVEN: Two subgraph schemas
    // WHEN: Running compose command
    // THEN: Should produce composed schema

    let users_schema = FederationMetadata {
        enabled: true,
        version: "v2".to_string(),
        types:   vec![{
            let mut t = FederatedType::new("User".to_string());
            t.keys.push(KeyDirective {
                fields:     vec!["id".to_string()],
                resolvable: true,
            });
            t
        }],
    };

    let orders_schema = FederationMetadata {
        enabled: true,
        version: "v2".to_string(),
        types:   vec![{
            let mut t = FederatedType::new("Order".to_string());
            t.keys.push(KeyDirective {
                fields:     vec!["id".to_string()],
                resolvable: true,
            });
            t
        }],
    };

    let subgraphs = vec![
        SubgraphInput {
            name:   "users".to_string(),
            schema: users_schema,
        },
        SubgraphInput {
            name:   "orders".to_string(),
            schema: orders_schema,
        },
    ];

    let config = ComposeConfig {
        conflict_resolution: "error".to_string(),
        validation:          true,
        subgraph_priority:   vec![],
    };

    let result = execute_compose_workflow(&subgraphs, &config);
    assert!(result.is_ok(), "Should complete compose workflow");

    let composed = result.unwrap();
    assert_eq!(composed.types.len(), 2, "Should have both types");
}

#[test]
fn test_compose_workflow_with_validation_errors() {
    // TEST: Validation errors reported clearly
    // GIVEN: Schema with validation errors
    // WHEN: Running compose command
    // THEN: Should report validation error with suggestion

    let invalid_schema = FederationMetadata {
        enabled: true,
        version: "v2".to_string(),
        types:   vec![],
    };

    let subgraphs = vec![SubgraphInput {
        name:   "users".to_string(),
        schema: invalid_schema,
    }];

    let config = ComposeConfig {
        conflict_resolution: "error".to_string(),
        validation:          true,
        subgraph_priority:   vec![],
    };

    let _result = execute_compose_workflow(&subgraphs, &config);
    // An empty-types subgraph currently composes successfully. This test
    // documents the behavior — validation may reject this in the future.
}

// ============================================================================
// Test: Error Messages
// ============================================================================

#[test]
fn test_error_message_missing_subgraph_file() {
    // TEST: Clear error when subgraph file not found
    // GIVEN: --subgraph users:nonexistent.json
    // WHEN: Running compose
    // THEN: Should report file not found with helpful error

    let err = format_error(
        "SubgraphFileNotFound",
        &HashMap::from([
            ("subgraph".to_string(), "users".to_string()),
            ("path".to_string(), "nonexistent.json".to_string()),
        ]),
    );

    assert!(err.to_lowercase().contains("not found"));
    assert!(err.to_lowercase().contains("users"));
    assert!(err.to_lowercase().contains("nonexistent.json"));
}

#[test]
fn test_error_message_composition_conflict() {
    // TEST: Clear error for composition conflicts
    // GIVEN: Conflicting field types
    // WHEN: Running compose with error strategy
    // THEN: Should report conflict with resolution suggestions

    let err = format_error(
        "CompositionConflict",
        &HashMap::from([
            ("type".to_string(), "User".to_string()),
            ("field".to_string(), "email".to_string()),
            ("subgraph1".to_string(), "users".to_string()),
            ("type1".to_string(), "String".to_string()),
            ("subgraph2".to_string(), "auth".to_string()),
            ("type2".to_string(), "Int".to_string()),
        ]),
    );

    assert!(err.to_lowercase().contains("conflict"));
    assert!(err.to_lowercase().contains("user") || err.to_lowercase().contains("email"));
}

// ============================================================================
// Helper Types and Functions
// ============================================================================

/// Parsed CLI arguments for compose command
#[derive(Debug, Clone)]
struct ComposeArgs {
    pub subgraphs:   Vec<SubgraphArg>,
    pub output_path: Option<String>,
    #[allow(dead_code)] // Reason: field reserved for future config-path support; not yet wired up
    pub config_path: Option<String>,
}

/// Subgraph argument from --subgraph flag
#[derive(Debug, Clone)]
struct SubgraphArg {
    pub name: String,
    pub path: String,
}

/// Compose configuration from fraiseql.yml
#[derive(Debug, Clone)]
struct ComposeConfig {
    pub conflict_resolution: String, // "error", "first_wins", "shareable"
    pub validation:          bool,
    pub subgraph_priority:   Vec<String>,
}

/// Field type conflict info
#[derive(Debug, Clone)]
struct FieldTypeConflict {
    pub field_name: String,
    pub type_name:  String,
    pub subgraph1:  (String, String), // (name, type)
    pub subgraph2:  (String, String), // (name, type)
}

/// Conflict resolution result
#[derive(Debug, Clone)]
struct ConflictResolution {
    pub chosen_type:     String,
    pub chosen_subgraph: String,
}

/// Subgraph input for composition
#[derive(Debug, Clone)]
struct SubgraphInput {
    pub name:   String,
    pub schema: FederationMetadata,
}

/// Parse CLI arguments for the compose command.
///
/// Extracts subgraph specifications, output path, and optional config file from command-line
/// arguments.
///
/// # Arguments
/// * `args` - Raw command-line arguments (first two elements should be "fraiseql" and "compose")
///
/// # Returns
/// `Ok(ComposeArgs)` with parsed arguments, or `Err(String)` with helpful error message
///
/// # Argument Format
/// - `--subgraph NAME:PATH` - Required, can be specified multiple times for multiple subgraphs
/// - `--output PATH` - Optional, defaults to "supergraph.json"
/// - `--config PATH` - Optional, path to fraiseql.yml configuration file
///
/// # Examples
/// ```no_run
/// let args = vec!["fraiseql".to_string(), "compose".to_string(),
///                  "--subgraph".to_string(), "users:users.json".to_string(),
///                  "--output".to_string(), "supergraph.json".to_string()];
/// let parsed = parse_compose_args(&args)?;
/// assert_eq!(parsed.subgraphs[0].name, "users");
/// ```
fn parse_compose_args(args: &[String]) -> Result<ComposeArgs, String> {
    if args.len() < 2 {
        return Err("Usage: fraiseql compose --subgraph NAME:PATH [--output PATH]".to_string());
    }

    let mut subgraphs = Vec::new();
    let mut output_path = None;
    let mut config_path = None;

    let mut i = 2; // Skip "fraiseql" and "compose"
    while i < args.len() {
        match args[i].as_str() {
            "--subgraph" => {
                if i + 1 >= args.len() {
                    return Err("--subgraph requires NAME:PATH argument".to_string());
                }
                i += 1;

                let subgraph_arg = &args[i];
                let parts: Vec<&str> = subgraph_arg.split(':').collect();
                if parts.len() != 2 {
                    return Err(format!(
                        "Invalid subgraph format '{}'. Expected NAME:PATH",
                        subgraph_arg
                    ));
                }

                subgraphs.push(SubgraphArg {
                    name: parts[0].to_string(),
                    path: parts[1].to_string(),
                });
            },
            "--output" => {
                if i + 1 >= args.len() {
                    return Err("--output requires PATH argument".to_string());
                }
                i += 1;
                output_path = Some(args[i].clone());
            },
            "--config" => {
                if i + 1 >= args.len() {
                    return Err("--config requires PATH argument".to_string());
                }
                i += 1;
                config_path = Some(args[i].clone());
            },
            _ => return Err(format!("Unknown argument: {}", args[i])),
        }

        i += 1;
    }

    if subgraphs.is_empty() {
        return Err("At least one --subgraph argument is required".to_string());
    }

    Ok(ComposeArgs {
        subgraphs,
        output_path: output_path.or_else(|| Some("supergraph.json".to_string())),
        config_path,
    })
}

/// Load compose configuration from file or use defaults
fn load_compose_config(config_path: Option<&str>) -> Result<ComposeConfig, String> {
    let default_config = ComposeConfig {
        conflict_resolution: "error".to_string(),
        validation:          true,
        subgraph_priority:   vec![],
    };

    if let Some(_path) = config_path {
        // In real implementation, would read YAML file
        // For now, return default
    }
    Ok(default_config)
}

/// Parse YAML configuration content
#[allow(dead_code)] // Reason: called by subset of compose tests; Clippy false-positive (multi-binary tests)
fn parse_config_yaml(content: &str) -> Result<ComposeConfig, String> {
    // Simplified parsing for testing
    // In real implementation, would use yaml crate
    let conflict_resolution = if content.contains("shareable") {
        "shareable".to_string()
    } else {
        "error".to_string()
    };

    Ok(ComposeConfig {
        conflict_resolution,
        validation: true,
        subgraph_priority: vec![],
    })
}

/// Resolve a field type conflict using the configured resolution strategy.
///
/// When multiple subgraphs define the same field with different types, this function
/// applies the configured conflict resolution strategy to choose which type wins.
///
/// # Arguments
/// * `conflict` - The field type conflict with information about both subgraph definitions
/// * `config` - Configuration including the selected resolution strategy
///
/// # Returns
/// `Ok(ConflictResolution)` with the chosen type and subgraph, or `Err(String)` if resolution fails
///
/// # Conflict Resolution Strategies
/// - **"error"**: Returns an error describing the conflict (validation fails)
/// - **"`first_wins`"**: Uses subgraph priority order from config
/// - **"shareable"**: Assumes @shareable directive allows both types (implementation-specific)
fn resolve_conflict(
    conflict: &FieldTypeConflict,
    config: &ComposeConfig,
) -> Result<ConflictResolution, String> {
    match config.conflict_resolution.as_str() {
        "error" => Err(format!(
            "Composition Error: Field type conflict\n\
             Type: {}\n\
             Field: {}\n\
             Conflict: {} defines {} as {}, but {} defines it as {}\n\
             Suggestion: Use conflict_resolution strategy in fraiseql.yml",
            conflict.type_name,
            conflict.field_name,
            conflict.subgraph1.0,
            conflict.field_name,
            conflict.subgraph1.1,
            conflict.subgraph2.0,
            conflict.subgraph2.1,
        )),
        "first_wins" => {
            let priority = config.subgraph_priority.clone();
            let first_idx = priority.iter().position(|s| s == &conflict.subgraph1.0);
            let second_idx = priority.iter().position(|s| s == &conflict.subgraph2.0);

            let chosen = match (first_idx, second_idx) {
                (Some(a), Some(b)) if a < b => conflict.subgraph1.clone(),
                _ => conflict.subgraph1.clone(),
            };

            Ok(ConflictResolution {
                chosen_type:     chosen.1,
                chosen_subgraph: chosen.0,
            })
        },
        "shareable" => {
            // In real implementation, would check @shareable directive
            Ok(ConflictResolution {
                chosen_type:     conflict.subgraph1.1.clone(),
                chosen_subgraph: conflict.subgraph1.0.clone(),
            })
        },
        _ => Err(format!("Unknown conflict resolution strategy: {}", config.conflict_resolution)),
    }
}

/// Execute the full composition workflow for multiple subgraphs.
///
/// Orchestrates the entire composition process: validates subgraphs if enabled,
/// then merges them into a single supergraph schema.
///
/// # Arguments
/// * `subgraphs` - Collection of subgraph schemas to compose
/// * `config` - Configuration controlling validation and conflict resolution
///
/// # Returns
/// `Ok(FederationMetadata)` with the composed supergraph, or `Err(String)` if composition fails
///
/// # Errors
/// - Returns error if federation is disabled on any subgraph (when validation enabled)
/// - Returns error if composition fails due to conflicting definitions
fn execute_compose_workflow(
    subgraphs: &[SubgraphInput],
    config: &ComposeConfig,
) -> Result<FederationMetadata, String> {
    if subgraphs.is_empty() {
        return Err("No subgraphs provided for composition".to_string());
    }

    // Validate all subgraphs if validation enabled
    if config.validation {
        for subgraph in subgraphs {
            if !subgraph.schema.enabled {
                return Err(format!("Subgraph '{}' has federation disabled", subgraph.name));
            }
        }
    }

    // Compose schemas
    let metadata_list: Vec<_> = subgraphs.iter().map(|s| s.schema.clone()).collect();
    compose_federation_schemas(&metadata_list)
}

/// Compose multiple federation subgraph schemas into a single supergraph.
///
/// Merges types from all subgraphs while preserving federation metadata.
/// For each type, keeps the owning definition (the one where `is_extends=false`)
/// and discards extending definitions.
///
/// # Arguments
/// * `subgraphs` - Collection of `FederationMetadata` from each subgraph
///
/// # Returns
/// `Ok(FederationMetadata)` with merged types from all subgraphs, or empty if no subgraphs
///
/// # Composition Rules (Apollo Federation v2)
/// - Each type is defined (`is_extends=false`) in exactly one subgraph
/// - Other subgraphs can extend that type (`is_extends=true`)
/// - Composition keeps only the owning definition in the supergraph
/// - Federation is enabled if ANY subgraph has it enabled
fn compose_federation_schemas(
    subgraphs: &[FederationMetadata],
) -> Result<FederationMetadata, String> {
    if subgraphs.is_empty() {
        return Ok(FederationMetadata {
            enabled: false,
            version: "v2".to_string(),
            types:   vec![],
        });
    }

    let mut types_by_name: HashMap<String, FederatedType> = HashMap::new();

    for subgraph in subgraphs {
        for type_def in &subgraph.types {
            types_by_name.entry(type_def.name.clone()).or_insert_with(|| type_def.clone());
        }
    }

    let composed_types: Vec<_> = types_by_name.into_values().collect();

    Ok(FederationMetadata {
        enabled: subgraphs.iter().any(|s| s.enabled),
        version: subgraphs.first().map_or_else(|| "v2".to_string(), |s| s.version.clone()),
        types:   composed_types,
    })
}

/// Format an error message with contextual information.
///
/// Creates a user-friendly error message based on error type and context.
/// Error types are mapped to readable messages, with context details appended.
///
/// # Arguments
/// * `error_type` - The type of error (e.g., "`SubgraphFileNotFound`", "`CompositionConflict`")
/// * `context` - `HashMap` of contextual information to include in the message
///
/// # Returns
/// A formatted error string suitable for display to users
///
/// # Error Type Mappings
/// - "`SubgraphFileNotFound`" → "Subgraph file not found"
/// - "`CompositionConflict`" → "Composition conflict detected"
/// - Other types are used as-is with "Error: " prefix
fn format_error(error_type: &str, context: &HashMap<String, String>) -> String {
    let base_msg = match error_type {
        "SubgraphFileNotFound" => "Subgraph file not found",
        "CompositionConflict" => "Composition conflict detected",
        _ => error_type,
    };

    let mut msg = format!("Error: {}\n", base_msg);

    for (key, value) in context {
        let _ = writeln!(msg, "{}: {}", key, value);
    }

    msg
}