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
//! Federation query execution (_service and _entities).
use std::sync::Arc;
use super::Executor;
use crate::{
db::traits::DatabaseAdapter,
error::{FraiseQLError, Result},
};
impl<A: DatabaseAdapter> Executor<A> {
/// Execute a federation query (_service or _entities).
///
/// # Errors
///
/// * [`FraiseQLError::Validation`] — the query name is not `_service` or `_entities`, or
/// federation is not enabled in the compiled schema.
/// * [`FraiseQLError::Database`] — the `_entities` lookup query fails.
pub(super) async fn execute_federation_query(
&self,
query_name: &str,
query: &str,
variables: Option<&serde_json::Value>,
) -> Result<serde_json::Value> {
match query_name {
"_service" => self.execute_service_query().await,
"_entities" => self.execute_entities_query(query, variables).await,
_ => Err(FraiseQLError::Validation {
message: format!("Unknown federation query: {}", query_name),
path: None,
}),
}
}
/// Execute _service query returning federation SDL.
async fn execute_service_query(&self) -> Result<serde_json::Value> {
// Get federation metadata from schema
let fed_metadata =
self.schema.federation_metadata().ok_or_else(|| FraiseQLError::Validation {
message: "Federation not enabled in schema".to_string(),
path: None,
})?;
// Generate SDL with federation directives
let raw_schema = self.schema.raw_schema();
let sdl = crate::federation::generate_service_sdl(&raw_schema, &fed_metadata);
// Return federation response format
let response = serde_json::json!({
"data": {
"_service": {
"sdl": sdl
}
}
});
Ok(response)
}
/// Execute _entities query resolving federation entities.
async fn execute_entities_query(
&self,
query: &str,
variables: Option<&serde_json::Value>,
) -> Result<serde_json::Value> {
// Get federation metadata from schema
let fed_metadata =
self.schema.federation_metadata().ok_or_else(|| FraiseQLError::Validation {
message: "Federation not enabled in schema".to_string(),
path: None,
})?;
// Extract representations from variables
let representations_value =
variables.and_then(|v| v.get("representations")).ok_or_else(|| {
FraiseQLError::Validation {
message: "_entities query requires 'representations' variable".to_string(),
path: None,
}
})?;
// Parse representations
let representations =
crate::federation::parse_representations(representations_value, &fed_metadata)?;
// Validate representations
crate::federation::validate_representations(&representations, &fed_metadata)?;
// Create federation resolver
let fed_resolver = crate::federation::FederationResolver::new(fed_metadata);
// Extract actual field selection from GraphQL query AST.
// __typename is NOT added to the SQL field list — it is a GraphQL meta-field
// not stored in the database. The database_resolver injects it into results.
let selection = match crate::federation::selection_parser::parse_field_selection(query) {
Ok(sel) if !sel.fields.is_empty() => {
let fields: Vec<String> =
sel.fields.into_iter().filter(|f| f != "__typename").collect();
crate::federation::FieldSelection::new(fields)
},
_ => {
// Fallback to wildcard if parsing fails or no fields extracted
crate::federation::FieldSelection::new(vec![
"*".to_string(), // Wildcard for all fields (will be expanded by resolver)
])
},
};
// Extract or create trace context for federation operations
// Note: Trace context should ideally be passed from HTTP headers via ExecutionContext,
// but for now we create a new context for tracing federation operations.
// The trace context could be injected through the query variables or a request-scoped store
// in future versions to correlate with the incoming HTTP trace headers.
let trace_context = crate::federation::FederationTraceContext::new();
// Batch load entities from database with tracing support
let entities = crate::federation::batch_load_entities_with_tracing(
&representations,
&fed_resolver,
Arc::clone(&self.adapter),
&selection,
Some(trace_context),
)
.await?;
// Return federation response format
let response = serde_json::json!({
"data": {
"_entities": entities
}
});
Ok(response)
}
}