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
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
//! Schema merger - combines language-generated types.json with TOML configuration
//!
//! This module merges:
//! - types.json: Generated by language implementations (Python, Go, etc.)
//! - fraiseql.toml: Configuration (security, federation, observers, caching, etc.)
//!
//! Result: Complete IntermediateSchema ready for compilation

use std::fs;

use anyhow::{Context, Result};
use serde_json::{Value, json};

use crate::{config::TomlSchema, schema::IntermediateSchema};

/// Schema merger combining language types and TOML config
pub struct SchemaMerger;

impl SchemaMerger {
    /// Merge types.json file with TOML configuration
    ///
    /// # Arguments
    /// * `types_path` - Path to types.json (from language implementation)
    /// * `toml_path` - Path to fraiseql.toml (configuration)
    ///
    /// # Returns
    /// Combined IntermediateSchema
    pub fn merge_files(types_path: &str, toml_path: &str) -> Result<IntermediateSchema> {
        // Load types.json
        let types_json = fs::read_to_string(types_path)
            .context(format!("Failed to read types.json from {types_path}"))?;
        let types_value: Value =
            serde_json::from_str(&types_json).context("Failed to parse types.json")?;

        // Load TOML
        let toml_schema = TomlSchema::from_file(toml_path)
            .context(format!("Failed to load TOML from {toml_path}"))?;

        // Note: TOML validation is skipped here because queries may reference types
        // from types.json (not yet loaded). Validation happens in the compiler after merge.

        // Merge
        Self::merge_values(&types_value, &toml_schema)
    }

    /// Merge TOML-only (no types.json)
    ///
    /// # Arguments
    /// * `toml_path` - Path to fraiseql.toml with inline type definitions
    ///
    /// # Returns
    /// IntermediateSchema from TOML definitions
    pub fn merge_toml_only(toml_path: &str) -> Result<IntermediateSchema> {
        let toml_schema = TomlSchema::from_file(toml_path)
            .context(format!("Failed to load TOML from {toml_path}"))?;

        toml_schema.validate()?;

        // Convert TOML to intermediate schema
        let types_value = toml_schema.to_intermediate_schema();
        Self::merge_values(&types_value, &toml_schema)
    }

    /// Merge from directory with auto-discovery
    ///
    /// # Arguments
    /// * `toml_path` - Path to fraiseql.toml (configuration)
    /// * `schema_dir` - Path to directory containing schema files
    ///
    /// # Returns
    /// IntermediateSchema from loaded files + TOML definitions
    pub fn merge_from_directory(toml_path: &str, schema_dir: &str) -> Result<IntermediateSchema> {
        let toml_schema = TomlSchema::from_file(toml_path)
            .context(format!("Failed to load TOML from {toml_path}"))?;

        toml_schema.validate()?;

        // Load all files from directory
        let types_value = crate::schema::MultiFileLoader::load_from_directory(schema_dir)
            .context(format!("Failed to load schema from directory {schema_dir}"))?;

        // Merge with TOML definitions
        Self::merge_values(&types_value, &toml_schema)
    }

    /// Load a named section from a set of files, returning `None` when the list is empty.
    fn load_section(files: &[String], key: &str) -> Result<Option<serde_json::Value>> {
        if files.is_empty() {
            return Ok(None);
        }
        let paths: Vec<std::path::PathBuf> = files.iter().map(std::path::PathBuf::from).collect();
        let loaded = crate::schema::MultiFileLoader::load_from_paths(&paths)
            .with_context(|| format!("Failed to load {key} files"))?;
        Ok(loaded.get(key).cloned())
    }

    /// Parse a JSON file and extend the target vectors with its `types`, `queries`, and
    /// `mutations` arrays. Missing keys are silently skipped.
    fn extend_from_json_file(
        path: &std::path::Path,
        all_types: &mut Vec<Value>,
        all_queries: &mut Vec<Value>,
        all_mutations: &mut Vec<Value>,
    ) -> Result<()> {
        let content = fs::read_to_string(path)
            .with_context(|| format!("Failed to read {}", path.display()))?;
        let value: Value = serde_json::from_str(&content)
            .with_context(|| format!("Failed to parse {}", path.display()))?;
        for (vec, key) in [
            (all_types as &mut Vec<Value>, "types"),
            (all_queries, "queries"),
            (all_mutations, "mutations"),
        ] {
            if let Some(Value::Array(items)) = value.get(key) {
                vec.extend(items.iter().cloned());
            }
        }
        Ok(())
    }

    /// Apply TOML metadata (`sql_source`, `description`) to a type JSON object in place.
    fn enrich_type_from_toml(enriched_type: &mut Value, toml_type: &crate::config::toml_schema::TypeDefinition) {
        enriched_type["sql_source"] = json!(toml_type.sql_source);
        if let Some(desc) = &toml_type.description {
            enriched_type["description"] = json!(desc);
        }
    }

    /// Merge explicit file lists
    ///
    /// # Arguments
    /// * `toml_path` - Path to fraiseql.toml (configuration)
    /// * `type_files` - Vector of type file paths
    /// * `query_files` - Vector of query file paths
    /// * `mutation_files` - Vector of mutation file paths
    ///
    /// # Returns
    /// IntermediateSchema from loaded files + TOML definitions
    pub fn merge_explicit_files(
        toml_path: &str,
        type_files: &[String],
        query_files: &[String],
        mutation_files: &[String],
    ) -> Result<IntermediateSchema> {
        let toml_schema = TomlSchema::from_file(toml_path)
            .context(format!("Failed to load TOML from {toml_path}"))?;

        toml_schema.validate()?;

        let mut types_value = serde_json::json!({
            "types": [],
            "queries": [],
            "mutations": []
        });

        if let Some(v) = Self::load_section(type_files, "types")? {
            types_value["types"] = v;
        }
        if let Some(v) = Self::load_section(query_files, "queries")? {
            types_value["queries"] = v;
        }
        if let Some(v) = Self::load_section(mutation_files, "mutations")? {
            types_value["mutations"] = v;
        }

        Self::merge_values(&types_value, &toml_schema)
    }

    /// Merge from domains (domain-based organization)
    ///
    /// # Arguments
    /// * `toml_path` - Path to fraiseql.toml with domain_discovery enabled
    ///
    /// # Returns
    /// IntermediateSchema from all domains (types.json, queries.json, mutations.json)
    pub fn merge_from_domains(toml_path: &str) -> Result<IntermediateSchema> {
        let toml_schema = TomlSchema::from_file(toml_path)
            .context(format!("Failed to load TOML from {toml_path}"))?;

        toml_schema.validate()?;

        // Resolve domains from configuration
        let domains = toml_schema
            .domain_discovery
            .resolve_domains()
            .context("Failed to discover domains")?;

        if domains.is_empty() {
            // No domains found, return empty schema merged with TOML definitions
            let empty_value = serde_json::json!({
                "types": [],
                "queries": [],
                "mutations": []
            });
            return Self::merge_values(&empty_value, &toml_schema);
        }

        let mut all_types = Vec::new();
        let mut all_queries = Vec::new();
        let mut all_mutations = Vec::new();

        for domain in domains {
            for filename in ["types.json", "queries.json", "mutations.json"] {
                let path = domain.path.join(filename);
                if path.exists() {
                    Self::extend_from_json_file(
                        &path,
                        &mut all_types,
                        &mut all_queries,
                        &mut all_mutations,
                    )?;
                }
            }
        }

        let types_value = serde_json::json!({
            "types": all_types,
            "queries": all_queries,
            "mutations": all_mutations,
        });

        // Merge with TOML definitions
        Self::merge_values(&types_value, &toml_schema)
    }

    /// Merge with TOML includes (glob patterns for schema files)
    ///
    /// # Arguments
    /// * `toml_path` - Path to fraiseql.toml with schema.includes section
    ///
    /// # Returns
    /// IntermediateSchema from loaded files + TOML definitions
    pub fn merge_with_includes(toml_path: &str) -> Result<IntermediateSchema> {
        let toml_schema = TomlSchema::from_file(toml_path)
            .context(format!("Failed to load TOML from {toml_path}"))?;

        toml_schema.validate()?;

        // If includes are specified, load and merge files
        let types_value = if toml_schema.includes.is_empty() {
            // No includes specified, use empty schema
            serde_json::json!({
                "types": [],
                "queries": [],
                "mutations": []
            })
        } else {
            let resolved = toml_schema
                .includes
                .resolve_globs()
                .context("Failed to resolve glob patterns in schema.includes")?;

            // Load all type files
            let type_files: Vec<std::path::PathBuf> = resolved.types;
            let mut merged_types = if type_files.is_empty() {
                serde_json::json!({
                    "types": [],
                    "queries": [],
                    "mutations": []
                })
            } else {
                crate::schema::MultiFileLoader::load_from_paths(&type_files)
                    .context("Failed to load type files")?
            };

            // Load and merge query files
            if !resolved.queries.is_empty() {
                let loaded = crate::schema::MultiFileLoader::load_from_paths(&resolved.queries)
                    .context("Failed to load query files")?;
                let new_items =
                    loaded.get("queries").and_then(Value::as_array).cloned().unwrap_or_default();
                if let Some(Value::Array(existing)) = merged_types.get_mut("queries") {
                    existing.extend(new_items);
                }
            }

            // Load and merge mutation files
            if !resolved.mutations.is_empty() {
                let loaded = crate::schema::MultiFileLoader::load_from_paths(&resolved.mutations)
                    .context("Failed to load mutation files")?;
                let new_items = loaded
                    .get("mutations")
                    .and_then(Value::as_array)
                    .cloned()
                    .unwrap_or_default();
                if let Some(Value::Array(existing)) = merged_types.get_mut("mutations") {
                    existing.extend(new_items);
                }
            }

            merged_types
        };

        // Merge with TOML definitions
        Self::merge_values(&types_value, &toml_schema)
    }

    /// Merge JSON types with TOML schema
    fn merge_values(types_value: &Value, toml_schema: &TomlSchema) -> Result<IntermediateSchema> {
        // Start with arrays for types, queries, mutations (not objects!)
        // This matches IntermediateSchema structure which uses Vec<T>
        let mut types_array: Vec<Value> = Vec::new();
        let mut queries_array: Vec<Value> = Vec::new();
        let mut mutations_array: Vec<Value> = Vec::new();

        // Process types from types.json (comes as array from language SDKs)
        if let Some(types_obj) = types_value.get("types") {
            match types_obj {
                // Handle array format (from language SDKs)
                Value::Array(types_list) => {
                    for type_item in types_list {
                        if let Some(type_name) = type_item.get("name").and_then(|v| v.as_str()) {
                            let mut enriched_type = type_item.clone();
                            if let Some(toml_type) = toml_schema.types.get(type_name) {
                                Self::enrich_type_from_toml(&mut enriched_type, toml_type);
                            }
                            types_array.push(enriched_type);
                        }
                    }
                },
                // Handle object format (from TOML-only, for backward compatibility)
                Value::Object(types_map) => {
                    for (type_name, type_value) in types_map {
                        let mut enriched_type = type_value.clone();
                        enriched_type["name"] = json!(type_name);

                        // Convert fields from object to array format if needed
                        if let Some(Value::Object(fields_map)) = enriched_type.get("fields") {
                            let fields_array: Vec<Value> = fields_map
                                .iter()
                                .map(|(field_name, field_value)| {
                                    let mut field = field_value.clone();
                                    field["name"] = json!(field_name);
                                    field
                                })
                                .collect();
                            enriched_type["fields"] = json!(fields_array);
                        }

                        if let Some(toml_type) = toml_schema.types.get(type_name) {
                            Self::enrich_type_from_toml(&mut enriched_type, toml_type);
                        }

                        types_array.push(enriched_type);
                    }
                },
                _ => {},
            }
        }

        // Add types from TOML that aren't already in types_array
        let existing_type_names: std::collections::HashSet<_> = types_array
            .iter()
            .filter_map(|t| {
                t.get("name").and_then(|v| v.as_str()).map(str::to_string)
            })
            .collect();

        for (type_name, toml_type) in &toml_schema.types {
            if !existing_type_names.contains(type_name) {
                types_array.push(json!({
                    "name": type_name,
                    "sql_source": toml_type.sql_source,
                    "description": toml_type.description,
                    "fields": toml_type.fields.iter().map(|(fname, fdef)| json!({
                        "name": fname,
                        "type": fdef.field_type,
                        "nullable": fdef.nullable,
                        "description": fdef.description,
                    })).collect::<Vec<_>>(),
                }));
            }
        }

        if let Some(Value::Array(queries_list)) = types_value.get("queries") {
            queries_array.clone_from(queries_list);
        }

        // Add queries from TOML
        for (query_name, toml_query) in &toml_schema.queries {
            queries_array.push(json!({
                "name": query_name,
                "return_type": toml_query.return_type,
                "return_array": toml_query.return_array,
                "sql_source": toml_query.sql_source,
                "description": toml_query.description,
                "args": toml_query.args.iter().map(|arg| json!({
                    "name": arg.name,
                    "type": arg.arg_type,
                    "required": arg.required,
                    "default": arg.default,
                    "description": arg.description,
                })).collect::<Vec<_>>(),
            }));
        }

        if let Some(Value::Array(mutations_list)) = types_value.get("mutations") {
            mutations_array.clone_from(mutations_list);
        }

        // Add mutations from TOML
        for (mutation_name, toml_mutation) in &toml_schema.mutations {
            mutations_array.push(json!({
                "name": mutation_name,
                "return_type": toml_mutation.return_type,
                "sql_source": toml_mutation.sql_source,
                "operation": toml_mutation.operation,
                "description": toml_mutation.description,
                "args": toml_mutation.args.iter().map(|arg| json!({
                    "name": arg.name,
                    "type": arg.arg_type,
                    "required": arg.required,
                    "default": arg.default,
                    "description": arg.description,
                })).collect::<Vec<_>>(),
            }));
        }

        // Build merged schema with arrays
        let mut merged = serde_json::json!({
            "version": "2.0.0",
            "types": types_array,
            "queries": queries_array,
            "mutations": mutations_array,
        });

        // Add security configuration if available in TOML
        merged["security"] = json!({
            "default_policy": toml_schema.security.default_policy,
            "rules": toml_schema.security.rules.iter().map(|r| json!({
                "name": r.name,
                "rule": r.rule,
                "description": r.description,
                "cacheable": r.cacheable,
                "cache_ttl_seconds": r.cache_ttl_seconds,
            })).collect::<Vec<_>>(),
            "policies": toml_schema.security.policies.iter().map(|p| json!({
                "name": p.name,
                "type": p.policy_type,
                "rule": p.rule,
                "roles": p.roles,
                "strategy": p.strategy,
                "attributes": p.attributes,
                "description": p.description,
                "cache_ttl_seconds": p.cache_ttl_seconds,
            })).collect::<Vec<_>>(),
            "field_auth": toml_schema.security.field_auth.iter().map(|fa| json!({
                "type_name": fa.type_name,
                "field_name": fa.field_name,
                "policy": fa.policy,
            })).collect::<Vec<_>>(),
            "enterprise": json!({
                "rate_limiting_enabled": toml_schema.security.enterprise.rate_limiting_enabled,
                "auth_endpoint_max_requests": toml_schema.security.enterprise.auth_endpoint_max_requests,
                "auth_endpoint_window_seconds": toml_schema.security.enterprise.auth_endpoint_window_seconds,
                "audit_logging_enabled": toml_schema.security.enterprise.audit_logging_enabled,
                "audit_log_backend": toml_schema.security.enterprise.audit_log_backend,
                "audit_retention_days": toml_schema.security.enterprise.audit_retention_days,
                "error_sanitization": toml_schema.security.enterprise.error_sanitization,
                "hide_implementation_details": toml_schema.security.enterprise.hide_implementation_details,
                "constant_time_comparison": toml_schema.security.enterprise.constant_time_comparison,
                "pkce_enabled": toml_schema.security.enterprise.pkce_enabled,
            }),
        });

        // Embed observers configuration if enabled or if any backend URL is set
        if toml_schema.observers.enabled
            || toml_schema.observers.redis_url.is_some()
            || toml_schema.observers.nats_url.is_some()
        {
            if toml_schema.observers.backend == "nats" && toml_schema.observers.nats_url.is_none() {
                tracing::warn!(
                    "observers.backend is \"nats\" but observers.nats_url is not set; \
                     the runtime will require FRAISEQL_NATS_URL to be configured"
                );
            }
            merged["observers_config"] = json!({
                "enabled": toml_schema.observers.enabled,
                "backend": toml_schema.observers.backend,
                "redis_url": toml_schema.observers.redis_url,
                "nats_url": toml_schema.observers.nats_url,
                "handlers": toml_schema.observers.handlers.iter().map(|h| json!({
                    "name": h.name,
                    "event": h.event,
                    "action": h.action,
                    "webhook_url": h.webhook_url,
                    "retry_strategy": h.retry_strategy,
                    "max_retries": h.max_retries,
                    "description": h.description,
                })).collect::<Vec<_>>(),
            });
        }

        // Embed federation configuration if enabled
        if toml_schema.federation.enabled {
            merged["federation_config"] = serde_json::to_value(&toml_schema.federation)
                .unwrap_or_default();
        }

        // Convert to IntermediateSchema
        serde_json::from_value::<IntermediateSchema>(merged)
            .context("Failed to convert merged schema to IntermediateSchema")
    }
}

#[cfg(test)]
mod tests {
    use std::fs;

    use tempfile::TempDir;

    use super::*;

    #[test]
    fn test_merge_toml_only() {
        let toml_content = r#"
[schema]
name = "test"
version = "1.0.0"
database_target = "postgresql"

[database]
url = "postgresql://localhost/test"

[types.User]
sql_source = "v_user"

[types.User.fields.id]
type = "ID"

[types.User.fields.name]
type = "String"

[queries.users]
return_type = "User"
return_array = true
sql_source = "v_user"
"#;

        // Write temp file
        let temp_path = "/tmp/test_fraiseql.toml";
        std::fs::write(temp_path, toml_content).unwrap();

        // Merge
        let result = SchemaMerger::merge_toml_only(temp_path);
        assert!(result.is_ok());

        // Clean up
        let _ = std::fs::remove_file(temp_path);
    }

    #[test]
    fn test_merge_with_includes() -> Result<()> {
        let temp_dir = TempDir::new()?;

        // Create schema files
        let user_types = serde_json::json!({
            "types": [{"name": "User", "fields": []}],
            "queries": [],
            "mutations": []
        });
        fs::write(temp_dir.path().join("user.json"), user_types.to_string())?;

        let post_types = serde_json::json!({
            "types": [{"name": "Post", "fields": []}],
            "queries": [],
            "mutations": []
        });
        fs::write(temp_dir.path().join("post.json"), post_types.to_string())?;

        // Create TOML with includes
        let toml_content = format!(
            r#"
[schema]
name = "test"
version = "1.0.0"
database_target = "postgresql"

[database]
url = "postgresql://localhost/test"

[includes]
types = ["{}/*.json"]
queries = []
mutations = []
"#,
            temp_dir.path().to_string_lossy()
        );

        let toml_path = temp_dir.path().join("fraiseql.toml");
        fs::write(&toml_path, toml_content)?;

        // Merge
        let result = SchemaMerger::merge_with_includes(toml_path.to_str().unwrap());
        assert!(result.is_ok());

        let schema = result?;
        assert_eq!(schema.types.len(), 2);

        Ok(())
    }

    #[test]
    fn test_merge_with_includes_missing_files() -> Result<()> {
        let temp_dir = TempDir::new()?;

        let toml_content = r#"
[schema]
name = "test"
version = "1.0.0"
database_target = "postgresql"

[database]
url = "postgresql://localhost/test"

[includes]
types = ["/nonexistent/path/*.json"]
queries = []
mutations = []
"#;

        let toml_path = temp_dir.path().join("fraiseql.toml");
        fs::write(&toml_path, toml_content)?;

        // Should succeed but with no files loaded (glob matches nothing)
        let result = SchemaMerger::merge_with_includes(toml_path.to_str().unwrap());
        assert!(result.is_ok());

        let schema = result?;
        assert_eq!(schema.types.len(), 0);

        Ok(())
    }

    #[test]
    fn test_merge_from_domains() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let schema_dir = temp_dir.path().join("schema");
        fs::create_dir(&schema_dir)?;

        // Create domain structure
        fs::create_dir(schema_dir.join("auth"))?;
        fs::create_dir(schema_dir.join("products"))?;

        let auth_types = serde_json::json!({
            "types": [{"name": "User", "fields": []}],
            "queries": [{"name": "getUser", "return_type": "User"}],
            "mutations": []
        });
        fs::write(schema_dir.join("auth/types.json"), auth_types.to_string())?;

        let product_types = serde_json::json!({
            "types": [{"name": "Product", "fields": []}],
            "queries": [{"name": "getProduct", "return_type": "Product"}],
            "mutations": []
        });
        fs::write(schema_dir.join("products/types.json"), product_types.to_string())?;

        // Create TOML with domain discovery (use absolute path)
        let schema_dir_str = schema_dir.to_string_lossy().to_string();
        let toml_content = format!(
            r#"
[schema]
name = "test"
version = "1.0.0"
database_target = "postgresql"

[database]
url = "postgresql://localhost/test"

[domain_discovery]
enabled = true
root_dir = "{schema_dir_str}"
"#
        );

        let toml_path = temp_dir.path().join("fraiseql.toml");
        fs::write(&toml_path, toml_content)?;

        // Merge
        let result = SchemaMerger::merge_from_domains(toml_path.to_str().unwrap());

        assert!(result.is_ok());
        let schema = result?;

        // Should have 2 types (from both domains)
        assert_eq!(schema.types.len(), 2);
        // Should have 2 queries (from both domains)
        assert_eq!(schema.queries.len(), 2);

        Ok(())
    }

    #[test]
    fn test_merge_from_domains_alphabetical_order() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let schema_dir = temp_dir.path().join("schema");
        fs::create_dir(&schema_dir)?;

        // Create domains in non-alphabetical order
        fs::create_dir(schema_dir.join("zebra"))?;
        fs::create_dir(schema_dir.join("alpha"))?;
        fs::create_dir(schema_dir.join("middle"))?;

        for domain in &["zebra", "alpha", "middle"] {
            let types = serde_json::json!({
                "types": [{"name": domain.to_uppercase(), "fields": []}],
                "queries": [],
                "mutations": []
            });
            fs::write(schema_dir.join(format!("{domain}/types.json")), types.to_string())?;
        }

        let schema_dir_str = schema_dir.to_string_lossy().to_string();
        let toml_content = format!(
            r#"
[schema]
name = "test"
version = "1.0.0"
database_target = "postgresql"

[database]
url = "postgresql://localhost/test"

[domain_discovery]
enabled = true
root_dir = "{schema_dir_str}"
"#
        );

        let toml_path = temp_dir.path().join("fraiseql.toml");
        fs::write(&toml_path, toml_content)?;

        let result = SchemaMerger::merge_from_domains(toml_path.to_str().unwrap());

        assert!(result.is_ok());
        let schema = result?;

        // Types should be loaded in alphabetical order: ALPHA, MIDDLE, ZEBRA
        let type_names: Vec<String> = schema.types.iter().map(|t| t.name.clone()).collect();

        assert_eq!(type_names[0], "ALPHA");
        assert_eq!(type_names[1], "MIDDLE");
        assert_eq!(type_names[2], "ZEBRA");

        Ok(())
    }
}