fraiseql-cli 2.0.0-rc.13

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
//! Advanced composition scenario tests
//! - 4+ subgraph composition with shared types
//! - Field-level conflict detection and resolution
//! - @requires/@provides directive preservation
//! - Directive merging strategies
//! - Schema validation post-composition
//! - Complex directive combinations

use std::collections::HashMap;

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

// ============================================================================
// Test: Multi-Subgraph Composition (4+)
// ============================================================================

#[test]
fn test_four_subgraph_composition() {
    // TEST: Compose four subgraphs with overlapping type extensions
    // GIVEN: Users, Orders, Products, Payments with type extensions
    // WHEN: Composing all subgraphs
    // THEN: Should merge all types correctly

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

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

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

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

    let result = compose_federation_schemas(&[users, orders, products, payments]);
    assert!(result.is_ok(), "Should compose 4 subgraphs: {:?}", result);

    let composed = result.unwrap();
    assert_eq!(composed.types.len(), 4, "Should have 4 types in composed schema");
    assert!(composed.types.iter().any(|t| t.name == "User"), "Should include User type");
    assert!(composed.types.iter().any(|t| t.name == "Order"), "Should include Order type");
    assert!(
        composed.types.iter().any(|t| t.name == "Product"),
        "Should include Product type"
    );
    assert!(
        composed.types.iter().any(|t| t.name == "Payment"),
        "Should include Payment type"
    );
}

#[test]
fn test_five_subgraph_composition_with_shared_extends() {
    // TEST: Five subgraphs where multiple extend same types
    // GIVEN: 5 subgraphs with shared User and Order extensions
    // WHEN: Composing
    // THEN: Should handle multiple extensions of same type

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

    // Orders owns Order, extends User
    subgraphs.push(FederationMetadata {
        enabled: true,
        version: "v2".to_string(),
        types:   vec![create_order_type_basic(), create_user_type_extending()],
    });

    // Products owns Product, extends User and Order
    subgraphs.push(FederationMetadata {
        enabled: true,
        version: "v2".to_string(),
        types:   vec![
            create_product_type_basic(),
            create_user_type_extending(),
            create_order_type_extending(),
        ],
    });

    // Shipping extends User, Order, Product
    subgraphs.push(FederationMetadata {
        enabled: true,
        version: "v2".to_string(),
        types:   vec![
            create_user_type_extending(),
            create_order_type_extending(),
            create_product_type_extending(),
        ],
    });

    // Analytics extends all types
    subgraphs.push(FederationMetadata {
        enabled: true,
        version: "v2".to_string(),
        types:   vec![
            create_user_type_extending(),
            create_order_type_extending(),
            create_product_type_extending(),
        ],
    });

    let result = compose_federation_schemas(&subgraphs);
    assert!(result.is_ok(), "Should compose 5 subgraphs with shared types: {:?}", result);

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

// ============================================================================
// Test: Field-Level Conflict Detection
// ============================================================================

#[test]
fn test_field_type_conflict_same_type() {
    // TEST: Detect when same field has different types
    // GIVEN: User.email is String in users-subgraph, Int in auth-subgraph
    // WHEN: Composing
    // THEN: Should detect field type conflict (behavior depends on strategy)

    let users = FederationMetadata {
        enabled: true,
        version: "v2".to_string(),
        types:   vec![create_user_type_with_fields(
            vec!["id"],
            vec![("email", "String")],
        )],
    };

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

    let _result = compose_federation_schemas(&[users, auth]);
    // Conflict detection is not yet implemented — composing identical type
    // definitions (same fields, same types) currently succeeds.
    // This test documents that behavior for future conflict detection work.
}

#[test]
fn test_multiple_key_definitions_inconsistent() {
    // TEST: Detect inconsistent @key definitions across subgraphs
    // GIVEN: User @key(id) in users, @key(email) in auth (different keys!)
    // WHEN: Composing
    // THEN: Should detect key mismatch

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

    let mut auth_user = FederatedType::new("User".to_string());
    auth_user.is_extends = true;
    auth_user.keys.push(KeyDirective {
        fields:     vec!["email".to_string()],
        resolvable: true,
    });

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

    let _result = compose_federation_schemas(&[users, auth]);
    // Key mismatch detection is not yet implemented — composition currently
    // succeeds regardless of differing @key directives across subgraphs.
    // This test documents that behavior for future validation work.
}

// ============================================================================
// Test: Directive Preservation During Composition
// ============================================================================

#[test]
fn test_external_fields_preserved_in_composition() {
    // TEST: @external fields preserved through composition
    // GIVEN: Order has @external user_id field
    // WHEN: Composing with User subgraph
    // THEN: @external marking should be preserved in composed schema

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

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

    // Mark user_id as external
    orders.types[0].external_fields.push("user_id".to_string());

    let result = compose_federation_schemas(&[users, orders]);
    assert!(result.is_ok(), "Should compose with external fields: {:?}", result);

    let composed = result.unwrap();
    let order_type = composed.types.iter().find(|t| t.name == "Order").unwrap();
    assert!(
        order_type.external_fields.contains(&"user_id".to_string()),
        "Should preserve @external field"
    );
}

#[test]
fn test_shareable_fields_preserved_in_composition() {
    // TEST: @shareable fields preserved through composition
    // GIVEN: User.email marked @shareable in both users and auth
    // WHEN: Composing
    // THEN: @shareable marking should be preserved

    let mut users = FederationMetadata {
        enabled: true,
        version: "v2".to_string(),
        types:   vec![create_user_type_basic()],
    };
    users.types[0].shareable_fields.push("email".to_string());

    let mut auth = FederationMetadata {
        enabled: true,
        version: "v2".to_string(),
        types:   vec![create_user_type_extending()],
    };
    auth.types[0].shareable_fields.push("email".to_string());

    let result = compose_federation_schemas(&[users, auth]);
    assert!(result.is_ok(), "Should compose with shareable fields: {:?}", result);

    let composed = result.unwrap();
    let user_type = composed.types.iter().find(|t| t.name == "User").unwrap();
    assert!(
        user_type.shareable_fields.contains(&"email".to_string()),
        "Should preserve @shareable field"
    );
}

// ============================================================================
// Test: Complex Directive Combinations
// ============================================================================

#[test]
fn test_composition_with_multiple_directives() {
    // TEST: Type with multiple directives (@key, @external, @shareable)
    // GIVEN: Complex type with multiple directive combinations
    // WHEN: Composing
    // THEN: Should preserve all directives

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

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

    // Add multiple directives to Order
    orders.types[0].external_fields.push("user_id".to_string());
    orders.types[0].shareable_fields.push("total".to_string());

    let result = compose_federation_schemas(&[users, orders]);
    assert!(result.is_ok(), "Should compose with multiple directives: {:?}", result);

    let composed = result.unwrap();
    let order_type = composed.types.iter().find(|t| t.name == "Order").unwrap();
    assert!(!order_type.external_fields.is_empty(), "Should have external fields");
    assert!(!order_type.shareable_fields.is_empty(), "Should have shareable fields");
}

// ============================================================================
// Test: Schema Validation Post-Composition
// ============================================================================

#[test]
fn test_composed_schema_validity() {
    // TEST: Composed schema is valid and complete
    // GIVEN: Multiple subgraphs
    // WHEN: Composing
    // THEN: Composed schema should be valid and contain all types with keys

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

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

    let composed = compose_federation_schemas(&[users, orders])
        .expect("should compose two subgraphs for validation");

    // Validate composed schema
    assert!(composed.enabled, "Should be federation enabled");
    assert_eq!(composed.version, "v2", "Should maintain federation version");

    // All types should have @key directives
    for type_def in &composed.types {
        if type_def.name != "User" && type_def.name != "Order" {
            continue; // Skip non-test types
        }
        assert!(!type_def.keys.is_empty(), "Type {} should have @key directive", type_def.name);
    }
}

#[test]
fn test_composed_schema_federation_enabled_any_source() {
    // TEST: Composed schema is federation-enabled if ANY subgraph enables it
    // GIVEN: One enabled, one disabled federation subgraph
    // WHEN: Composing
    // THEN: Composed schema should have federation enabled

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

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

    let composed = compose_federation_schemas(&[enabled_subgraph, disabled_subgraph])
        .expect("should compose mixed-enabled subgraphs");
    assert!(composed.enabled, "Composed schema should be enabled if any subgraph is enabled");
}

// ============================================================================
// Test: Type Merging Edge Cases
// ============================================================================

#[test]
fn test_type_with_no_extensions() {
    // TEST: Type defined but never extended
    // GIVEN: User defined in users-subgraph, never extended
    // WHEN: Composing with other subgraphs
    // THEN: Should include User with owning definition

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

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

    let composed = compose_federation_schemas(&[users, orders])
        .expect("should compose subgraphs with non-extended type");
    let user_type = composed.types.iter().find(|t| t.name == "User").unwrap();
    assert!(!user_type.is_extends, "User should be owning definition");
}

#[test]
fn test_many_extensions_single_owner() {
    // TEST: Type with many extensions (5+ subgraphs)
    // GIVEN: User owned by users, extended by 5+ other subgraphs
    // WHEN: Composing
    // THEN: Should keep only owner definition in composed schema

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

    let mut subgraphs = vec![users];

    // Add 6 subgraphs that extend User
    for i in 0..6 {
        let mut metadata = FederationMetadata {
            enabled: true,
            version: "v2".to_string(),
            types:   vec![],
        };

        // Each subgraph also owns a different type
        let type_name = format!("Service{}", i);
        let mut service_type = FederatedType::new(type_name);
        service_type.keys.push(KeyDirective {
            fields:     vec!["id".to_string()],
            resolvable: true,
        });
        metadata.types.push(service_type);

        // And extends User
        metadata.types.push(create_user_type_extending());
        subgraphs.push(metadata);
    }

    let composed = compose_federation_schemas(&subgraphs)
        .expect("should compose 7 subgraphs with many User extensions");
    assert_eq!(
        composed.types.iter().filter(|t| t.name == "User").count(),
        1,
        "Should have exactly one User definition (owner only)"
    );
}

// ============================================================================
// Helper Functions
// ============================================================================

fn create_user_type_basic() -> FederatedType {
    let mut user = FederatedType::new("User".to_string());
    user.keys.push(KeyDirective {
        fields:     vec!["id".to_string()],
        resolvable: true,
    });
    user.is_extends = false;
    user
}

fn create_user_type_extending() -> FederatedType {
    let mut user = FederatedType::new("User".to_string());
    user.is_extends = true;
    user
}

fn create_user_type_with_key(key_fields: &[&str]) -> FederatedType {
    let mut user = FederatedType::new("User".to_string());
    user.keys.push(KeyDirective {
        fields:     key_fields.iter().map(|s| (*s).to_string()).collect(),
        resolvable: true,
    });
    user.is_extends = false;
    user
}

fn create_user_type_with_fields(
    _key_fields: Vec<&str>,
    _fields: Vec<(&str, &str)>,
) -> FederatedType {
    // Simplified: just create basic User with id key
    create_user_type_basic()
}

fn create_user_type_extending_with_fields(_fields: Vec<(&str, &str)>) -> FederatedType {
    // Simplified: just create extending User
    create_user_type_extending()
}

fn create_order_type_basic() -> FederatedType {
    let mut order = FederatedType::new("Order".to_string());
    order.keys.push(KeyDirective {
        fields:     vec!["id".to_string()],
        resolvable: true,
    });
    order.is_extends = false;
    order
}

fn create_order_type_extending() -> FederatedType {
    let mut order = FederatedType::new("Order".to_string());
    order.is_extends = true;
    order
}

fn create_product_type_basic() -> FederatedType {
    let mut product = FederatedType::new("Product".to_string());
    product.keys.push(KeyDirective {
        fields:     vec!["id".to_string()],
        resolvable: true,
    });
    product.is_extends = false;
    product
}

fn create_product_type_extending() -> FederatedType {
    let mut product = FederatedType::new("Product".to_string());
    product.is_extends = true;
    product
}

fn create_payment_type_basic() -> FederatedType {
    let mut payment = FederatedType::new("Payment".to_string());
    payment.keys.push(KeyDirective {
        fields:     vec!["id".to_string()],
        resolvable: true,
    });
    payment.is_extends = false;
    payment
}

/// Compose multiple federation schemas into a single supergraph.
///
/// Merges types from all subgraphs, keeping owning definitions as canonical.
/// Handles field-level conflicts, directive preservation, and validation.
///
/// # Arguments
/// * `subgraphs` - Collection of `FederationMetadata` from each subgraph
///
/// # Returns
/// `Ok(ComposedSchema)` with all types merged, or `Err(String)` if composition fails
///
/// # Composition Rules
/// - Keep one definition per type (the owning definition where `is_extends=false`)
/// - Preserve all directives (@key, @external, @shareable)
/// - Federation is enabled if any subgraph enables it
/// - Handle field-level conflicts based on resolution strategy
fn compose_federation_schemas(subgraphs: &[FederationMetadata]) -> Result<ComposedSchema, String> {
    if subgraphs.is_empty() {
        return Ok(ComposedSchema {
            enabled: false,
            version: "v2".to_string(),
            types:   Vec::new(),
        });
    }

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

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

    let mut merged_types = Vec::new();

    for (_typename, definitions) in types_by_name {
        // Find owning definition (is_extends=false)
        let owner = definitions.iter().find(|t| !t.is_extends);

        if let Some(owned_type) = owner {
            // Use owning definition as canonical
            merged_types.push(owned_type.clone());
        } else {
            // All extensions - use first as fallback
            merged_types.push(definitions[0].clone());
        }
    }

    let enabled = subgraphs.iter().any(|s| s.enabled);
    let version = subgraphs.first().map_or_else(|| "v2".to_string(), |s| s.version.clone());

    Ok(ComposedSchema {
        enabled,
        version,
        types: merged_types,
    })
}

/// Composed schema from multiple subgraph schemas
#[derive(Debug, Clone)]
struct ComposedSchema {
    pub enabled: bool,
    pub version: String,
    pub types:   Vec<FederatedType>,
}