pub struct ResultProjector { /* private fields */ }Expand description
Result projector - high-level result transformation.
Implementations§
Source§impl ResultProjector
impl ResultProjector
Sourcepub fn new(fields: Vec<String>) -> Self
pub fn new(fields: Vec<String>) -> Self
Create new result projector from field names (no aliases).
Sourcepub const fn with_mappings(fields: Vec<FieldMapping>) -> Self
pub const fn with_mappings(fields: Vec<FieldMapping>) -> Self
Create new result projector with field mappings (supports aliases).
Sourcepub fn with_typename(self, typename: impl Into<String>) -> Self
pub fn with_typename(self, typename: impl Into<String>) -> Self
Set __typename to include in all projected objects.
Per GraphQL spec §2.7, __typename returns the name of the object type.
This should be called when the client requests __typename in the selection set.
Sourcepub fn configure_typename_from_selections(
self,
selections: &[FieldSelection],
entity_type: &str,
) -> Self
pub fn configure_typename_from_selections( self, selections: &[FieldSelection], entity_type: &str, ) -> Self
Configure typename injection from the query selection set.
Inspects the root selection’s nested fields for __typename. If found,
enables typename injection via with_typename.
Sourcepub fn with_federation_mode(self, enabled: bool) -> Self
pub fn with_federation_mode(self, enabled: bool) -> Self
Enable federation mode: __typename is always injected regardless of selection set.
Used by the _entities federation resolver where the gateway always expects
__typename in entity results.
Sourcepub fn project_results(
&self,
results: &[JsonbValue],
is_list: bool,
) -> Result<JsonValue>
pub fn project_results( &self, results: &[JsonbValue], is_list: bool, ) -> Result<JsonValue>
Project database results to GraphQL response.
§Arguments
results- Database results as JSONB values (borrowed; not mutated).is_list- Whether the query returns a list.
§Returns
A freshly-allocated GraphQL-compatible JSON response. The projector
never aliases the input slice: each field of every JsonbValue is
cloned out into a new serde_json::Value tree (see F029 — ownership
contract is on JsonbValue itself).
§Errors
Returns error if projection fails.
Sourcepub fn wrap_in_data_envelope(result: JsonValue, query_name: &str) -> JsonValue
pub fn wrap_in_data_envelope(result: JsonValue, query_name: &str) -> JsonValue
Sourcepub fn add_typename_only(
&self,
projected_data: &JsonbValue,
typename: &str,
) -> Result<JsonValue>
pub fn add_typename_only( &self, projected_data: &JsonbValue, typename: &str, ) -> Result<JsonValue>
Add __typename field to SQL-projected data.
For data that has already been projected at the SQL level, we only need to add
the __typename field in Rust. This is much faster than projecting all fields
since the SQL already filtered to only requested fields.
§Arguments
projected_data- JSONB data already projected by SQLtypename- GraphQL type name to add
§Returns
New JSONB value with __typename field added
§Example
let projector = ResultProjector::new(vec!["id".to_string(), "name".to_string()]);
// Database already returned only: { "id": "123", "name": "Alice" }
let result = projector.add_typename_only(
&JsonbValue::new(json!({ "id": "123", "name": "Alice" })),
"User"
).unwrap();
// Result: { "id": "123", "name": "Alice", "__typename": "User" }
assert_eq!(result["__typename"], "User");§Errors
Returns FraiseQLError::Validation if the projected data contains a
list element that is not a JSON object, making __typename injection impossible.