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
//! Complete TOML schema configuration supporting types, queries, mutations, federation, observers,
//! caching
//!
//! This module extends FraiseQLConfig to support the full TOML-based schema definition.
pub mod caching;
pub mod domain;
pub mod federation;
pub mod observability;
pub mod observers;
pub mod operations;
pub mod rest;
pub mod security;
pub mod server_settings;
pub mod subscriptions;
pub mod types;
use std::collections::BTreeMap;
use anyhow::{Context, Result};
/// Format "Did you mean?" suggestions from `suggest_similar` results.
fn format_suggestions(suggestions: Vec<&str>) -> String {
if suggestions.is_empty() {
String::new()
} else {
format!(". Did you mean: {}?", suggestions.join(", "))
}
}
pub use caching::{AnalyticsConfig, AnalyticsQuery, CacheRule, CachingConfig};
pub use domain::{Domain, DomainDiscovery, ResolvedIncludes, SchemaIncludes};
pub use federation::{
FederationCircuitBreakerConfig, FederationConfig, FederationEntity,
PerDatabaseCircuitBreakerOverride,
};
use fraiseql_core::schema::{CrudNamingConfig, NamingConvention};
pub use observability::ObservabilityConfig;
pub use observers::{EventHandler, ObserversConfig};
pub use operations::{MutationDefinition, QueryDefaults, QueryDefinition, SchemaMetadata};
use rest::RestTomlConfig;
pub use security::{
ApiKeySecurityConfig, AuthorizationPolicy, AuthorizationRule, CodeChallengeMethod,
EncryptionAlgorithm, EnterpriseSecurityConfig, ErrorSanitizationTomlConfig, FieldAuthRule,
KeySource, OidcClientConfig, PkceConfig, RateLimitingSecurityConfig, SecuritySettings,
StateEncryptionConfig, StaticApiKeyEntry, TokenRevocationSecurityConfig, TrustedDocumentMode,
TrustedDocumentsConfig,
};
use serde::{Deserialize, Serialize};
pub use server_settings::{DebugConfig, McpConfig, ValidationConfig};
pub use subscriptions::{SubscriptionHooksConfig, SubscriptionsConfig};
pub use types::{ArgumentDefinition, FieldDefinition, TypeDefinition};
use super::{
expand_env_vars,
runtime::{DatabaseRuntimeConfig, ServerRuntimeConfig},
};
/// Complete TOML schema configuration
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct TomlSchema {
/// Schema metadata
#[serde(rename = "schema")]
pub schema: SchemaMetadata,
/// Database connection pool configuration (optional — all fields have defaults).
///
/// Supports `${VAR}` environment variable interpolation in the `url` field.
#[serde(rename = "database")]
pub database: DatabaseRuntimeConfig,
/// HTTP server runtime configuration (optional — all fields have defaults).
///
/// CLI flags (`--port`, `--bind`) take precedence over these settings.
#[serde(rename = "server")]
pub server: ServerRuntimeConfig,
/// Type definitions
#[serde(rename = "types")]
pub types: BTreeMap<String, TypeDefinition>,
/// Query definitions
#[serde(rename = "queries")]
pub queries: BTreeMap<String, QueryDefinition>,
/// Mutation definitions
#[serde(rename = "mutations")]
pub mutations: BTreeMap<String, MutationDefinition>,
/// Federation configuration
#[serde(rename = "federation")]
pub federation: FederationConfig,
/// Security configuration
#[serde(rename = "security")]
pub security: SecuritySettings,
/// Observers/event system configuration
#[serde(rename = "observers")]
pub observers: ObserversConfig,
/// Result caching configuration
#[serde(rename = "caching")]
pub caching: CachingConfig,
/// Analytics configuration
#[serde(rename = "analytics")]
pub analytics: AnalyticsConfig,
/// Observability configuration
#[serde(rename = "observability")]
pub observability: ObservabilityConfig,
/// Schema includes configuration for multi-file composition
#[serde(default)]
pub includes: SchemaIncludes,
/// Domain discovery configuration for domain-based organization
#[serde(default)]
pub domain_discovery: DomainDiscovery,
/// Global defaults for list-query auto-params.
///
/// Provides project-wide defaults for `where`, `order_by`, `limit`, and `offset`
/// parameters on list queries. Per-query `auto_params` overrides are partial —
/// only the specified flags override the defaults. Relay queries and single-item
/// queries are never affected.
#[serde(default)]
pub query_defaults: QueryDefaults,
/// OAuth2 client identity for server-side PKCE flows.
///
/// Required when `[security.pkce] enabled = true`.
/// Holds the OIDC provider discovery URL, client_id, and a reference to
/// the env var containing the client secret. Never stores the secret itself.
#[serde(default)]
pub auth: Option<OidcClientConfig>,
/// WebSocket subscription configuration (hooks, limits).
#[serde(default)]
pub subscriptions: SubscriptionsConfig,
/// Query validation limits (depth, complexity).
#[serde(default)]
pub validation: ValidationConfig,
/// Debug/development settings (database EXPLAIN, SQL exposure).
#[serde(default)]
pub debug: DebugConfig,
/// MCP (Model Context Protocol) server configuration.
#[serde(default)]
pub mcp: McpConfig,
/// REST transport configuration.
#[serde(default)]
pub rest: RestTomlConfig,
/// Naming convention for GraphQL operation names.
///
/// `"preserve"` (default) keeps names as authored (snake_case from Python SDKs).
/// `"camelCase"` converts operation names to standard GraphQL camelCase.
#[serde(default)]
pub naming_convention: NamingConvention,
/// CRUD function naming config for automatic `sql_source` resolution.
///
/// When set, mutations that omit `sql_source` have their PostgreSQL function
/// name resolved at compile time using the configured template and the entity
/// name derived from `return_type`.
///
/// Example:
/// ```toml
/// [crud]
/// function_schema = "app"
/// function_naming = "trinity"
/// ```
#[serde(default)]
pub crud: Option<CrudNamingConfig>,
/// Hierarchy definitions for ID-based ltree operators (`descendantOfId`, `ancestorOfId`).
///
/// Maps a hierarchy name to its table and ltree path column. Used by the compiler
/// to generate subquery-based ltree WHERE clauses that resolve an entity's ltree
/// path from its UUID.
///
/// Example:
/// ```toml
/// [hierarchies.category]
/// table = "tb_category"
/// path_column = "category_path"
/// ```
#[serde(default)]
pub hierarchies: Option<std::collections::HashMap<String, HierarchyConfig>>,
}
/// Configuration for a single hierarchy used by ID-based ltree operators.
///
/// Defines the database table and ltree path column for a named hierarchy.
/// The `id` column is always `id` (UUID) per the trinity pattern — not configurable.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct HierarchyConfig {
/// Database table containing the ltree column (e.g., `"tb_category"`).
pub table: String,
/// Name of the ltree column in the table (e.g., `"category_path"`).
pub path_column: String,
}
impl HierarchyConfig {
/// Validate that required fields are non-empty.
///
/// # Errors
///
/// Returns an error if `table` or `path_column` is empty.
pub fn validate(&self) -> Result<()> {
if self.table.is_empty() {
anyhow::bail!("hierarchy table must not be empty");
}
if self.path_column.is_empty() {
anyhow::bail!("hierarchy path_column must not be empty");
}
Ok(())
}
}
impl TomlSchema {
/// Load schema from TOML file
///
/// # Errors
///
/// Returns an error if the file cannot be read or cannot be parsed as a
/// valid `TomlSchema`.
pub fn from_file(path: &str) -> Result<Self> {
let content =
std::fs::read_to_string(path).context(format!("Failed to read TOML file: {path}"))?;
Self::parse_toml(&content)
}
/// Parse schema from TOML string.
///
/// Expands `${VAR}` environment variable placeholders before parsing.
///
/// # Errors
///
/// Returns an error if the TOML string cannot be deserialized into a
/// `TomlSchema`.
pub fn parse_toml(content: &str) -> Result<Self> {
let expanded = expand_env_vars(content)?;
toml::from_str(&expanded).context("Failed to parse TOML schema")
}
/// Validate schema
///
/// # Errors
///
/// Returns an error if any query or mutation references an undefined type,
/// if a field auth rule references an undefined policy, if a federation
/// entity references an undefined type, or if server/database/circuit-breaker
/// configuration values are invalid.
pub fn validate(&self) -> Result<()> {
use fraiseql_core::runtime::suggest_similar;
let type_names: Vec<&str> = self.types.keys().map(String::as_str).collect();
// Validate that all query return types exist
for (query_name, query_def) in &self.queries {
if !self.types.contains_key(&query_def.return_type) {
let hint = format_suggestions(suggest_similar(&query_def.return_type, &type_names));
anyhow::bail!(
"Query '{query_name}' references undefined type '{}'{hint}",
query_def.return_type
);
}
}
// Validate that all mutation return types exist
for (mut_name, mut_def) in &self.mutations {
if !self.types.contains_key(&mut_def.return_type) {
let hint = format_suggestions(suggest_similar(&mut_def.return_type, &type_names));
anyhow::bail!(
"Mutation '{mut_name}' references undefined type '{}'{hint}",
mut_def.return_type
);
}
}
// Validate field auth rules reference existing policies
for field_auth in &self.security.field_auth {
let policy_exists = self.security.policies.iter().any(|p| p.name == field_auth.policy);
if !policy_exists {
let policy_names: Vec<&str> =
self.security.policies.iter().map(|p| p.name.as_str()).collect();
let hint = format_suggestions(suggest_similar(&field_auth.policy, &policy_names));
anyhow::bail!(
"Field auth references undefined policy '{}'{hint}",
field_auth.policy
);
}
}
// Validate field hierarchy references exist in hierarchies config
let hierarchy_names: std::collections::HashSet<&str> = self
.hierarchies
.as_ref()
.map(|h| h.keys().map(String::as_str).collect())
.unwrap_or_default();
for (type_name, type_def) in &self.types {
for (field_name, field_def) in &type_def.fields {
if let Some(ref h_name) = field_def.hierarchy {
if !hierarchy_names.contains(h_name.as_str()) {
let hint = format_suggestions(suggest_similar(
h_name,
&hierarchy_names.iter().copied().collect::<Vec<_>>(),
));
anyhow::bail!(
"Field '{type_name}.{field_name}' references undefined hierarchy \
'{h_name}'{hint}"
);
}
}
}
}
// Validate hierarchy configs have non-empty values
if let Some(ref hierarchies) = self.hierarchies {
for (name, config) in hierarchies {
config
.validate()
.map_err(|e| anyhow::anyhow!("Invalid hierarchy config '{name}': {e}"))?;
}
}
// Validate federation entities reference existing types
for entity in &self.federation.entities {
if !self.types.contains_key(&entity.name) {
let hint = format_suggestions(suggest_similar(&entity.name, &type_names));
anyhow::bail!(
"Federation entity '{}' references undefined type{hint}",
entity.name
);
}
}
self.server.validate()?;
self.database.validate()?;
// Validate federation circuit breaker configuration
if let Some(cb) = &self.federation.circuit_breaker {
if cb.failure_threshold == 0 {
anyhow::bail!(
"federation.circuit_breaker.failure_threshold must be greater than 0"
);
}
if cb.recovery_timeout_secs == 0 {
anyhow::bail!(
"federation.circuit_breaker.recovery_timeout_secs must be greater than 0"
);
}
if cb.success_threshold == 0 {
anyhow::bail!(
"federation.circuit_breaker.success_threshold must be greater than 0"
);
}
// Validate per-database overrides reference defined entity names
let entity_names: std::collections::HashSet<&str> =
self.federation.entities.iter().map(|e| e.name.as_str()).collect();
for override_cfg in &cb.per_database {
if !entity_names.contains(override_cfg.database.as_str()) {
anyhow::bail!(
"federation.circuit_breaker.per_database entry '{}' does not match \
any defined federation entity",
override_cfg.database
);
}
if override_cfg.failure_threshold == Some(0) {
anyhow::bail!(
"federation.circuit_breaker.per_database['{}'].failure_threshold \
must be greater than 0",
override_cfg.database
);
}
if override_cfg.recovery_timeout_secs == Some(0) {
anyhow::bail!(
"federation.circuit_breaker.per_database['{}'].recovery_timeout_secs \
must be greater than 0",
override_cfg.database
);
}
if override_cfg.success_threshold == Some(0) {
anyhow::bail!(
"federation.circuit_breaker.per_database['{}'].success_threshold \
must be greater than 0",
override_cfg.database
);
}
}
}
Ok(())
}
/// Convert to intermediate schema format (compatible with language-generated types.json)
pub fn to_intermediate_schema(&self) -> serde_json::Value {
let mut types_json = serde_json::Map::new();
for (type_name, type_def) in &self.types {
let mut fields_json = serde_json::Map::new();
for (field_name, field_def) in &type_def.fields {
fields_json.insert(
field_name.clone(),
serde_json::json!({
"type": field_def.field_type,
"nullable": field_def.nullable,
"description": field_def.description,
}),
);
}
types_json.insert(
type_name.clone(),
serde_json::json!({
"name": type_name,
"sql_source": type_def.sql_source,
"description": type_def.description,
"fields": fields_json,
}),
);
}
let mut queries_json = serde_json::Map::new();
for (query_name, query_def) in &self.queries {
let args: Vec<serde_json::Value> = query_def
.args
.iter()
.map(|arg| {
serde_json::json!({
"name": arg.name,
"type": arg.arg_type,
"required": arg.required,
"default": arg.default,
"description": arg.description,
})
})
.collect();
queries_json.insert(
query_name.clone(),
serde_json::json!({
"name": query_name,
"return_type": query_def.return_type,
"return_array": query_def.return_array,
"sql_source": query_def.sql_source,
"description": query_def.description,
"args": args,
}),
);
}
serde_json::json!({
"types": types_json,
"queries": queries_json,
})
}
}
#[cfg(test)]
mod tests;