fraiseql-core 2.15.0

Core execution engine for FraiseQL v2 - Compiled GraphQL over SQL
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
//! Result projection - transforms JSONB database results to GraphQL responses.

use serde_json::{Map, Value as JsonValue};

use crate::{
    backend::types::JsonbValue,
    error::{FraiseQLError, Result},
    graphql::FieldSelection,
    schema::{CompiledSchema, FieldDefinition, FieldType},
    utils::casing::{to_camel_case, to_snake_case},
};

/// Field mapping for projection with alias support.
#[derive(Debug, Clone)]
pub struct FieldMapping {
    /// JSONB key name (source).
    pub source:          String,
    /// Output key name (alias if different from source).
    pub output:          String,
    /// Fallback stored key, tried when `source` is not present in the row.
    ///
    /// Populated by every constructor from `stored_key_candidates`. It is not
    /// support for `camelCase` *storage* — a view exposes `snake_case` keys and
    /// columns, and the SQL projection generator has no fallback at all. It is
    /// there because this projector runs over **two different maps**: the raw
    /// stored document, whose keys are `snake_case`, and the output of
    /// `jsonb_build_object`, whose keys are the camelCase *response* names the
    /// SQL projection already emitted. One mapping has to read both.
    ///
    /// Until #1271 this field had **no producer** anywhere in the workspace —
    /// its documented job was described and never performed, and the primary
    /// `source` was the declared name verbatim, so the raw-document case was
    /// simply unreadable.
    pub source_fallback: Option<String>,
    /// For nested object fields, the typename to add.
    /// This enables `__typename` to be added recursively to nested objects.
    pub nested_typename: Option<String>,
    /// Nested field mappings (for related objects).
    pub nested_fields:   Option<Vec<FieldMapping>>,
    /// The schema positively declares this field a **scalar** (#1192).
    ///
    /// Set by [`ProjectionMapper::with_declared_scalars`], which is the only
    /// thing that knows. It gates the JSON-text re-parse below: a value the
    /// database handed back as a string is re-read as an object or an array when
    /// it parses as one, to recover a nested object the SQL side extracted with
    /// `->>` — and the comment justifying that said scalar strings "won't parse
    /// as Object/Array, so this is safe for all field types". That premise is
    /// false for exactly the rows a text column full of serialized JSON exists
    /// to carry: an audit payload, a webhook body, an imported document. A field
    /// declared `String` was returned as the value its text encodes, so the
    /// response violated the schema the server publishes, per row, depending on
    /// whether that row's text happened to parse.
    ///
    /// Defaults to `false` — *not known to be scalar* — so a mapping built
    /// without schema knowledge keeps the recovery behaviour it had.
    pub declared_scalar: bool,
}

/// The stored JSONB key a declared field name resolves to, plus the legacy
/// spelling to fall back on.
///
/// **The single definition of the rule**, shared by the two Rust projectors:
/// [`FieldMapping`]'s constructors resolve it once per query, and
/// [`lookup_source`] resolves it per lookup for the selection-driven
/// [`project_entity`]. Before #1271 they were two implementations and only one
/// of them was right — the mapper read the declared name verbatim, so a
/// `camelCase` field over a `snake_case` view was silently absent from a 200.
///
/// `snake_case` first, because that is the key the SQL projection generator
/// (`projection_generator::render_field`) and the `where` parser both derive;
/// the `camelCase` spelling second, for stored rows built with the surface
/// casing. A single-word name yields the same string twice, so it has no
/// fallback.
pub fn stored_key_candidates(field_name: &str) -> (String, Option<String>) {
    let snake = to_snake_case(field_name);
    let camel = to_camel_case(field_name);
    let fallback = (camel != snake).then_some(camel);
    (snake, fallback)
}

impl FieldMapping {
    /// Create a simple field mapping (no alias).
    ///
    /// `name` is the **declared field name**; the stored key it reads is
    /// derived by `stored_key_candidates`, not taken verbatim.
    ///
    /// ```rust
    /// # use fraiseql_core::runtime::FieldMapping;
    /// let mapping = FieldMapping::simple("fkUser");
    /// assert_eq!(mapping.source, "fk_user");
    /// assert_eq!(mapping.output, "fkUser");
    /// ```
    #[must_use]
    pub fn simple(name: impl Into<String>) -> Self {
        let name = name.into();
        let (source, source_fallback) = stored_key_candidates(&name);
        Self {
            source,
            output: name,
            source_fallback,
            nested_typename: None,
            nested_fields: None,
            declared_scalar: false,
        }
    }

    /// Create a field mapping with an alias.
    ///
    /// `source` is the **declared field name** and `alias` the response key —
    /// the same split the SQL projection generator makes, where an aliased
    /// field `myName: fullName` reads `data->>'full_name'` and is emitted as
    /// `myName` (#418). The stored key is derived from `source` by
    /// `stored_key_candidates`.
    #[must_use]
    pub fn aliased(source: impl Into<String>, alias: impl Into<String>) -> Self {
        let (source, source_fallback) = stored_key_candidates(&source.into());
        Self {
            source,
            output: alias.into(),
            source_fallback,
            nested_typename: None,
            nested_fields: None,
            declared_scalar: false,
        }
    }

    /// Create a field mapping for a nested object with its own typename.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use fraiseql_core::runtime::FieldMapping;
    /// // For a Post with nested author (User type)
    /// let mapping = FieldMapping::nested_object("author", "User", vec![
    ///     FieldMapping::simple("id"),
    ///     FieldMapping::simple("name"),
    /// ]);
    /// assert_eq!(mapping.source, "author");
    /// ```
    #[must_use]
    pub fn nested_object(
        name: impl Into<String>,
        typename: impl Into<String>,
        fields: Vec<FieldMapping>,
    ) -> Self {
        let name = name.into();
        let (source, source_fallback) = stored_key_candidates(&name);
        Self {
            source,
            output: name,
            source_fallback,
            nested_typename: Some(typename.into()),
            nested_fields: Some(fields),
            declared_scalar: false,
        }
    }

    /// Create an aliased nested object field.
    #[must_use]
    pub fn nested_object_aliased(
        source: impl Into<String>,
        alias: impl Into<String>,
        typename: impl Into<String>,
        fields: Vec<FieldMapping>,
    ) -> Self {
        let (source, source_fallback) = stored_key_candidates(&source.into());
        Self {
            source,
            output: alias.into(),
            source_fallback,
            nested_typename: Some(typename.into()),
            nested_fields: Some(fields),
            declared_scalar: false,
        }
    }

    /// Set the typename for a nested object field.
    #[must_use]
    pub fn with_nested_typename(mut self, typename: impl Into<String>) -> Self {
        self.nested_typename = Some(typename.into());
        self
    }

    /// Set nested field mappings.
    #[must_use]
    pub fn with_nested_fields(mut self, fields: Vec<FieldMapping>) -> Self {
        self.nested_fields = Some(fields);
        self
    }

    /// Read this field's value out of a stored JSONB object.
    ///
    /// The primary key first, then [`source_fallback`](Self::source_fallback) —
    /// both resolved once at construction, so this stays allocation-free on the
    /// per-row path.
    #[must_use]
    pub fn lookup_in<'a>(
        &self,
        map: &'a serde_json::Map<String, JsonValue>,
    ) -> Option<&'a JsonValue> {
        map.get(&self.source)
            .or_else(|| self.source_fallback.as_ref().and_then(|fb| map.get(fb)))
    }
}

/// Projection mapper - maps JSONB fields to GraphQL selection set.
#[derive(Debug, Clone)]
pub struct ProjectionMapper {
    /// Fields to project (with optional aliases).
    pub fields:          Vec<FieldMapping>,
    /// Optional `__typename` value to add to each object.
    pub typename:        Option<String>,
    /// When `true`, `__typename` is injected unconditionally regardless of selection set.
    /// Used by federation `_entities` resolver where the gateway always expects `__typename`.
    pub federation_mode: bool,
}

/// Does this declared type's value space **exclude** JSON objects and arrays?
///
/// The question [`FieldMapping::declared_scalar`] needs, and deliberately not
/// [`FieldType::is_scalar`], which answers a different one. `is_scalar` is true
/// of `Json` — whose value space is *all* JSON — and of the vector types, which
/// serialize as arrays. Marking either would stop the text-recovery re-parse
/// from firing where it is doing its job.
///
/// A **custom** scalar is also left unmarked. A project defines its own
/// serialization, so whether one of its values can be a JSON object is a
/// question this module cannot adjudicate — the same rule the argument-value
/// validator follows: narrow to what is positively known, never widen. #1192
/// raised the custom-scalar case explicitly and this is the answer to it.
const fn excludes_json_composites(field_type: &FieldType) -> bool {
    matches!(
        field_type,
        FieldType::String
            | FieldType::Int
            | FieldType::Float
            | FieldType::Boolean
            | FieldType::Id
            | FieldType::DateTime
            | FieldType::Date
            | FieldType::Time
            | FieldType::Uuid
            | FieldType::Decimal
    )
}

impl ProjectionMapper {
    /// Create new projection mapper from field names (no aliases).
    #[must_use]
    pub fn new(fields: Vec<String>) -> Self {
        Self {
            fields:          fields.into_iter().map(FieldMapping::simple).collect(),
            typename:        None,
            federation_mode: false,
        }
    }

    /// Create new projection mapper with field mappings (supports aliases).
    #[must_use]
    pub const fn with_mappings(fields: Vec<FieldMapping>) -> Self {
        Self {
            fields,
            typename: None,
            federation_mode: false,
        }
    }

    /// Mark every mapping whose field `entity_type` declares as a **scalar**
    /// (#1192), so the JSON-text recovery in [`FieldMapping::declared_scalar`]
    /// does not fire on a `String` whose characters happen to be JSON.
    ///
    /// Lookup is by the mapping's `output` name — the GraphQL field name, which
    /// is what a type definition carries — falling back to `source` for a
    /// mapping built from a stored key. A field the schema does not describe
    /// stays unmarked: this narrows the recovery to what is positively known,
    /// and never widens it.
    #[must_use]
    pub fn with_declared_scalars(mut self, schema: &CompiledSchema, entity_type: &str) -> Self {
        let Some(type_def) = schema.find_type(entity_type) else {
            return self;
        };
        for mapping in &mut self.fields {
            let declared = type_def
                .fields
                .iter()
                .find(|f| f.name == mapping.output || f.name == mapping.source);
            if let Some(fd) = declared {
                mapping.declared_scalar = excludes_json_composites(&fd.field_type);
            }
        }
        self
    }

    /// Set `__typename` to include in projected objects.
    #[must_use]
    pub fn with_typename(mut self, typename: impl Into<String>) -> Self {
        self.typename = Some(typename.into());
        self
    }

    /// Enable federation mode: `__typename` is always injected regardless of selection set.
    #[must_use]
    pub const fn with_federation_mode(mut self, enabled: bool) -> Self {
        self.federation_mode = enabled;
        self
    }

    /// Project fields from JSONB value.
    ///
    /// # Arguments
    ///
    /// * `jsonb` - JSONB value from database
    ///
    /// # Returns
    ///
    /// Projected JSON value with only requested fields (and aliases applied)
    ///
    /// # Errors
    ///
    /// Returns error if projection fails.
    pub fn project(&self, jsonb: &JsonbValue) -> Result<JsonValue> {
        // Extract the inner serde_json::Value
        let value = jsonb.as_value();

        match value {
            JsonValue::Object(map) => self.project_json_object(map),
            JsonValue::Array(arr) => self.project_json_array(arr),
            v => Ok(v.clone()),
        }
    }

    /// Project object fields from JSON object.
    ///
    /// Maps source keys to output keys according to the configured `FieldMapping`s,
    /// injects `__typename` when configured, and recursively projects nested objects
    /// and arrays.
    ///
    /// # Errors
    ///
    /// Returns error if nested value projection fails.
    pub fn project_json_object(
        &self,
        map: &serde_json::Map<String, JsonValue>,
    ) -> Result<JsonValue> {
        let mut result = Map::new();

        // Add __typename first if configured (GraphQL convention)
        if let Some(ref typename) = self.typename {
            result.insert("__typename".to_string(), JsonValue::String(typename.clone()));
        }

        // Project fields with alias support, resolving each stored key by the
        // rule every consumer of a declared field name shares (#1271).
        for field in &self.fields {
            if let Some(value) = field.lookup_in(map) {
                let projected_value = self.project_nested_value(value, field)?;
                result.insert(field.output.clone(), projected_value);
            }
        }

        Ok(JsonValue::Object(result))
    }

    /// Project a nested value, adding typename if configured.
    #[allow(clippy::self_only_used_in_recursion)] // Reason: &self required for method dispatch; recursive structure is intentional
    fn project_nested_value(&self, value: &JsonValue, field: &FieldMapping) -> Result<JsonValue> {
        match value {
            JsonValue::Object(obj) => {
                // If this field has nested typename, add it
                if let Some(ref typename) = field.nested_typename {
                    let mut result = Map::new();
                    result.insert("__typename".to_string(), JsonValue::String(typename.clone()));

                    // If we have nested field mappings, use them; otherwise copy all fields
                    if let Some(ref nested_fields) = field.nested_fields {
                        for nested_field in nested_fields {
                            // #1271: the nested branch reads its own sub-map and
                            // had its own verbatim lookup, so it needs the rule
                            // explicitly rather than inheriting it from above.
                            if let Some(nested_value) = nested_field.lookup_in(obj) {
                                let projected =
                                    self.project_nested_value(nested_value, nested_field)?;
                                result.insert(nested_field.output.clone(), projected);
                            }
                        }
                    } else {
                        // No specific field mappings - copy all fields from source
                        for (k, v) in obj {
                            result.insert(k.clone(), v.clone());
                        }
                    }
                    Ok(JsonValue::Object(result))
                } else {
                    // No typename for this nested object - return as-is
                    Ok(value.clone())
                }
            },
            JsonValue::Array(arr) => {
                // For arrays of objects, add typename to each element
                if field.nested_typename.is_some() {
                    let projected: Result<Vec<JsonValue>> =
                        arr.iter().map(|item| self.project_nested_value(item, field)).collect();
                    Ok(JsonValue::Array(projected?))
                } else {
                    Ok(value.clone())
                }
            },
            _ => {
                // If the value is a JSON string that encodes an object or array
                // (which happens when the database uses ->>'field' text extraction
                // instead of ->'field' JSONB extraction), attempt to re-parse it.
                //
                // #1192: unless the schema declares the field a scalar. This used
                // to run for every field type, justified by "scalar strings won't
                // parse as Object/Array" — true of `"hello"`, false of the
                // serialized JSON that text columns routinely hold, and the
                // response then carried an object where the published schema
                // promised a string.
                if field.declared_scalar {
                    return Ok(value.clone());
                }
                if let JsonValue::String(ref s) = *value {
                    if let Ok(parsed @ (JsonValue::Object(_) | JsonValue::Array(_))) =
                        serde_json::from_str::<JsonValue>(s)
                    {
                        return self.project_nested_value(&parsed, field);
                    }
                }
                Ok(value.clone())
            },
        }
    }

    /// Project array elements from JSON array.
    ///
    /// A row that fails projection propagates as an error rather than being
    /// silently dropped from the result set (#736): `project_json_object` is
    /// currently infallible, but if it ever gains an error path, an array with
    /// a bad element must not shrink without a trace.
    fn project_json_array(&self, arr: &[JsonValue]) -> Result<JsonValue> {
        let projected: Result<Vec<JsonValue>> = arr
            .iter()
            .map(|item| {
                if let JsonValue::Object(obj) = item {
                    self.project_json_object(obj)
                } else {
                    Ok(item.clone())
                }
            })
            .collect();

        Ok(JsonValue::Array(projected?))
    }
}

/// Result projector - high-level result transformation.
pub struct ResultProjector {
    mapper: ProjectionMapper,
}

impl ResultProjector {
    /// Create new result projector from field names (no aliases).
    #[must_use]
    pub fn new(fields: Vec<String>) -> Self {
        Self {
            mapper: ProjectionMapper::new(fields),
        }
    }

    /// Create new result projector with field mappings (supports aliases).
    #[must_use]
    pub const fn with_mappings(fields: Vec<FieldMapping>) -> Self {
        Self {
            mapper: ProjectionMapper::with_mappings(fields),
        }
    }

    /// Mark the mappings the schema declares scalar (#1192).
    ///
    /// See [`ProjectionMapper::with_declared_scalars`].
    #[must_use]
    pub fn with_declared_scalars(mut self, schema: &CompiledSchema, entity_type: &str) -> Self {
        self.mapper = self.mapper.with_declared_scalars(schema, entity_type);
        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.
    #[must_use]
    pub fn with_typename(mut self, typename: impl Into<String>) -> Self {
        self.mapper = self.mapper.with_typename(typename);
        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`](Self::with_typename).
    #[must_use]
    pub fn configure_typename_from_selections(
        self,
        selections: &[FieldSelection],
        entity_type: &str,
    ) -> Self {
        let wants_typename = selections
            .first()
            .is_some_and(|root| root.nested_fields.iter().any(|f| f.name == "__typename"));
        if wants_typename {
            self.with_typename(entity_type)
        } else {
            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.
    #[must_use]
    pub fn with_federation_mode(mut self, enabled: bool) -> Self {
        self.mapper = self.mapper.with_federation_mode(enabled);
        self
    }

    /// 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.
    pub fn project_results(&self, results: &[JsonbValue], is_list: bool) -> Result<JsonValue> {
        if is_list {
            // Project array of results
            let projected: Result<Vec<JsonValue>> =
                results.iter().map(|r| self.mapper.project(r)).collect();

            Ok(JsonValue::Array(projected?))
        } else {
            // Project single result
            if let Some(first) = results.first() {
                self.mapper.project(first)
            } else {
                Ok(JsonValue::Null)
            }
        }
    }

    /// Wrap result in GraphQL data envelope.
    ///
    /// # Arguments
    ///
    /// * `result` - Projected result
    /// * `query_name` - Query operation name
    ///
    /// # Returns
    ///
    /// GraphQL response with `{ "data": { "queryName": result } }` structure
    #[must_use]
    pub fn wrap_in_data_envelope(result: JsonValue, query_name: &str) -> JsonValue {
        let mut data = Map::new();
        data.insert(query_name.to_string(), result);

        let mut response = Map::new();
        response.insert("data".to_string(), JsonValue::Object(data));

        JsonValue::Object(response)
    }

    /// 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 SQL
    /// * `typename` - GraphQL type name to add
    ///
    /// # Returns
    ///
    /// New JSONB value with `__typename` field added
    ///
    /// # Example
    ///
    /// ```rust
    /// # use fraiseql_core::runtime::ResultProjector;
    /// # use fraiseql_core::db::types::JsonbValue;
    /// # use serde_json::json;
    /// 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.
    pub fn add_typename_only(
        &self,
        projected_data: &JsonbValue,
        typename: &str,
    ) -> Result<JsonValue> {
        let value = projected_data.as_value();

        match value {
            JsonValue::Object(map) => {
                let mut result = map.clone();
                result.insert("__typename".to_string(), JsonValue::String(typename.to_string()));
                Ok(JsonValue::Object(result))
            },
            JsonValue::Array(arr) => {
                let updated: Result<Vec<JsonValue>> = arr
                    .iter()
                    .map(|item| {
                        if let JsonValue::Object(obj) = item {
                            let mut result = obj.clone();
                            result.insert(
                                "__typename".to_string(),
                                JsonValue::String(typename.to_string()),
                            );
                            Ok(JsonValue::Object(result))
                        } else {
                            Ok(item.clone())
                        }
                    })
                    .collect();
                Ok(JsonValue::Array(updated?))
            },
            v => Ok(v.clone()),
        }
    }

    /// Wrap error in GraphQL error envelope.
    ///
    /// # Arguments
    ///
    /// * `error` - Error to wrap
    ///
    /// # Returns
    ///
    /// GraphQL error response with `{ "errors": [...] }` structure
    #[must_use]
    pub fn wrap_error(error: &FraiseQLError) -> JsonValue {
        let mut error_obj = Map::new();
        error_obj.insert("message".to_string(), JsonValue::String(error.to_string()));

        let mut response = Map::new();
        response.insert("errors".to_string(), JsonValue::Array(vec![JsonValue::Object(error_obj)]));

        JsonValue::Object(response)
    }
}

/// Project an entity-shaped JSONB value into a GraphQL response object, mirroring
/// the query path's SQL projection exactly.
///
/// This is the **single, canonical** entity projector, shared by the mutation
/// success arm (projecting the returned entity) and the error arm (projecting the
/// error metadata). It is behaviourally equivalent to the SQL query projection
/// (`projection_generator::render_field`):
///
/// - **output key** = the selection's response key (the camelCase GraphQL surface name, honouring
///   aliases),
/// - **source key** = [`to_snake_case`](crate::utils::casing::to_snake_case) of the field name (the
///   stored JSONB key), with a `camelCase` fallback for legacy metadata that used the surface
///   casing,
/// - **object fields**, single or listed, are projected through their sub-selection at **any
///   depth**. The recursion follows the selection, which `max_query_depth` bounds, so there is no
///   depth at which a stored sub-object passes through: that fall-through, at a cap of 4, served
///   every stored key of an object five levels down, gated or not. An object field with no
///   sub-selection selects none of its fields and projects to `{}`,
/// - **scalar fields** pass through their stored value,
/// - **`__typename`** is emitted only where the client selected it.
///
/// `type_name` is the concrete GraphQL object type of `entity` (resolved by the
/// caller). `selections` is the result selection set for this level, with inline
/// `... on T` fragments preserved.
///
/// # An empty selection set projects **nothing**
///
/// It used to return the stored entity unchanged, and that one line is what made
/// #1352 and #1357 invisible: an empty slice is the *permissive* shape, not a
/// neutral one, so a transport that arrived without a selection set was not
/// refused — it was granted every field of the row, policy-gated ones included,
/// while `selection_set_selects_gated_field` reported that nothing gated was
/// selected and the #423 field authorizer took zero calls.
///
/// Nothing legitimate reaches here empty any more: GraphQL § 5.3.3 refuses a
/// composite field named without a selection set (#1357), every mutation's return
/// type is composite (#1358), and the write entries take a non-empty
/// [`WriteSelections`](crate::runtime::WriteSelections). An empty slice arriving
/// here is therefore a defect upstream, and the safe answer to a defect is the
/// empty object — which is also what the read path already answers for a selection
/// set that `@skip` resolves to nothing, so the two agree.
///
/// There is deliberately **no `project_entity_unfiltered`** to reach for. Every
/// caller in the tree either has a real selection set or guards emptiness before
/// calling (federation returns early on `root.nested_fields.is_empty()`), so a
/// named permissive helper would be a public function with no caller — and an
/// unused facility is how the permissive branch stayed reachable in the first
/// place. A caller that genuinely wants the whole entity can write `entity.clone()`
/// and own that decision where it is made.
#[must_use]
pub fn project_entity(
    entity: &JsonValue,
    type_name: &str,
    selections: &[FieldSelection],
    schema: &CompiledSchema,
) -> JsonValue {
    if selections.is_empty() {
        return JsonValue::Object(Map::new());
    }
    project_entity_at(entity, type_name, selections, schema)
}

fn project_entity_at(
    entity: &JsonValue,
    type_name: &str,
    selections: &[FieldSelection],
    schema: &CompiledSchema,
) -> JsonValue {
    let JsonValue::Object(obj) = entity else {
        // Not an object (e.g. null) — nothing to project.
        return entity.clone();
    };
    let type_def = schema.find_type(type_name);
    let mut out = Map::new();

    for sel in effective_selections(selections, type_name, schema) {
        if sel.name == "__typename" {
            out.insert(sel.response_key().to_string(), JsonValue::String(type_name.to_string()));
            continue;
        }
        let field_def =
            type_def.and_then(|td| td.fields.iter().find(|f| f.name.as_str() == sel.name));
        let Some(value) = lookup_source(obj, &sel.name) else {
            // Absent stored key → omit (matches query/SQL behaviour: absent stays absent).
            continue;
        };
        let projected = project_field_value(value, field_def, &sel.nested_fields, schema);
        out.insert(sel.response_key().to_string(), projected);
    }

    JsonValue::Object(out)
}

/// Project a single field value, recursing into nested object selections.
fn project_field_value(
    value: &JsonValue,
    field_def: Option<&FieldDefinition>,
    nested: &[FieldSelection],
    schema: &CompiledSchema,
) -> JsonValue {
    // An object field is projected through its sub-selection at every depth, and an empty
    // sub-selection projects nothing of it. Only a scalar returns its stored value.
    if let Some(fd) = field_def {
        if !fd.field_type.is_scalar() && !fd.field_type.is_list() {
            if let Some(child_type) = fd.field_type.type_name() {
                match value {
                    JsonValue::Object(_) => {
                        return project_entity_at(value, child_type, nested, schema);
                    },
                    // The DB sometimes returns a nested object as a JSON string
                    // (when extracted via `->>`). Re-parse and project it.
                    JsonValue::String(s) => {
                        if let Ok(parsed @ JsonValue::Object(_)) =
                            serde_json::from_str::<JsonValue>(s)
                        {
                            return project_entity_at(&parsed, child_type, nested, schema);
                        }
                    },
                    _ => {},
                }
            }
        } else if fd.field_type.is_list() {
            // #489: a nested LIST-of-object field. The SQL/stored side returns the
            // raw aggregated sub-blob (snake_case keys, unselected keys included);
            // project every element at the element type — the same recasing +
            // selection-set projection applied to single nested objects — so nested
            // list output matches top-level and nested-object output.
            if let Some(child_type) = fd.field_type.inner_type().and_then(FieldType::type_name) {
                if let JsonValue::Array(arr) = value {
                    return JsonValue::Array(
                        arr.iter()
                            .map(|el| project_list_element(el, child_type, nested, schema))
                            .collect(),
                    );
                }
                // The DB sometimes returns the whole array as a JSON string
                // (`->>` text extraction). Re-parse and project each element.
                if let JsonValue::String(s) = value {
                    if let Ok(JsonValue::Array(arr)) = serde_json::from_str::<JsonValue>(s) {
                        return JsonValue::Array(
                            arr.iter()
                                .map(|el| project_list_element(el, child_type, nested, schema))
                                .collect(),
                        );
                    }
                }
            }
        }
    }
    value.clone()
}

/// Project one element of a nested list-of-object field (#489): recase + project
/// object elements at `child_type`, passing scalars and non-object strings through.
fn project_list_element(
    element: &JsonValue,
    child_type: &str,
    nested: &[FieldSelection],
    schema: &CompiledSchema,
) -> JsonValue {
    match element {
        JsonValue::Object(_) => project_entity_at(element, child_type, nested, schema),
        // An element itself text-encoded as a JSON object.
        JsonValue::String(s) => match serde_json::from_str::<JsonValue>(s) {
            Ok(parsed @ JsonValue::Object(_)) => {
                project_entity_at(&parsed, child_type, nested, schema)
            },
            _ => element.clone(),
        },
        _ => element.clone(),
    }
}

/// #489 — recase + project nested LIST-of-object fields left raw by the SQL projection.
///
/// The query path projects top-level fields and nested single objects at the SQL level
/// (`jsonb_build_object`), but list fields fall back to the raw stored sub-blob
/// (`snake_case` keys, unselected keys included). This walks the already-projected
/// `value` guided by the selection set and, for each list-of-object field at any depth,
/// replaces its elements with the fully projected form via
/// [`project_entity`] at the element type. Non-list fields are left untouched — the SQL
/// side already projected them; single-object fields are only recursed into to reach
/// any lists nested inside them.
///
/// `type_name` is the entity type of `value` (or of each element when `value` is a
/// list-returning query's top-level array); `selections` is the entity-level selection
/// set (the query field's `nested_fields`).
pub fn project_nested_lists(
    value: &mut JsonValue,
    type_name: &str,
    selections: &[FieldSelection],
    schema: &CompiledSchema,
) {
    match value {
        // A list-returning query: each element is an entity of `type_name` (the list
        // itself is not a nesting level).
        JsonValue::Array(arr) => {
            for el in arr.iter_mut() {
                project_nested_lists(el, type_name, selections, schema);
            }
        },
        JsonValue::Object(obj) => {
            let type_def = schema.find_type(type_name);
            for sel in effective_selections(selections, type_name, schema) {
                if sel.name == "__typename" {
                    continue;
                }
                let Some(fd) =
                    type_def.and_then(|td| td.fields.iter().find(|f| f.name.as_str() == sel.name))
                else {
                    continue;
                };
                if fd.field_type.is_scalar() {
                    continue;
                }
                let key = sel.response_key();
                if fd.field_type.is_list() {
                    // The element type of a list-of-object field. `project_entity` fully
                    // projects each raw element, including any lists nested inside it.
                    if let Some(child_type) =
                        fd.field_type.inner_type().and_then(FieldType::type_name)
                    {
                        if let Some(JsonValue::Array(arr)) = obj.get_mut(key) {
                            for el in arr.iter_mut() {
                                *el = project_entity(el, child_type, &sel.nested_fields, schema);
                            }
                        }
                    }
                } else if let Some(child_type) = fd.field_type.type_name() {
                    // Single object already projected by the SQL side — recurse to reach
                    // any lists nested inside it.
                    if let Some(child) = obj.get_mut(key) {
                        project_nested_lists(child, child_type, &sel.nested_fields, schema);
                    }
                }
            }
        },
        _ => {},
    }
}

/// Stamp `__typename` on the nested single objects whose selection asked for it.
///
/// `__typename` is `String!` (GraphQL spec § Type Name Introspection): it can
/// never be null, and a requested field can never be absent. It is a meta-field,
/// not a JSONB key, so it is stripped from the SQL projection at every depth —
/// projecting it would emit `data->>'__typename'`, a literal NULL (the symptom
/// #912 reports). Something on the Rust side has to put it back.
///
/// Two of the three levels already had an owner: the root object is stamped by
/// [`ResultProjector::configure_typename_from_selections`], and list elements by
/// [`project_entity`]. A *single* nested object had none, so a requested nested
/// `__typename` was dropped from the response with no error at all.
///
/// The key is inserted at the position the client's selection set puts it, not
/// appended: a response's fields follow the query's order (spec § Response
/// Format).
pub fn stamp_nested_typenames(
    value: &mut JsonValue,
    type_name: &str,
    selections: &[FieldSelection],
    schema: &CompiledSchema,
) {
    match value {
        // A list-returning query: each element is an entity of `type_name` (the
        // list itself is not a nesting level).
        JsonValue::Array(arr) => {
            for el in arr.iter_mut() {
                stamp_nested_typenames(el, type_name, selections, schema);
            }
        },
        JsonValue::Object(obj) => {
            let type_def = schema.find_type(type_name);
            for sel in effective_selections(selections, type_name, schema) {
                // This level's own `__typename` belongs to whoever built this
                // object; only children are this pass's business.
                if sel.name == "__typename" {
                    continue;
                }
                let Some(fd) =
                    type_def.and_then(|td| td.fields.iter().find(|f| f.name.as_str() == sel.name))
                else {
                    continue;
                };
                // Lists are projected element-by-element through `project_entity`,
                // which stamps them already.
                if fd.field_type.is_scalar() || fd.field_type.is_list() {
                    continue;
                }
                let Some(child_type) = fd.field_type.type_name() else {
                    continue;
                };
                let key = sel.response_key().to_string();
                let child_selections = &sel.nested_fields;
                let Some(child) = obj.get_mut(&key) else {
                    continue;
                };
                if let JsonValue::Object(child_obj) = child {
                    reinsert_typename_in_order(child_obj, child_selections, child_type, schema);
                }
                stamp_nested_typenames(child, child_type, child_selections, schema);
            }
        },
        _ => {},
    }
}

/// Rebuild `obj` with its selected `__typename` keys in their requested
/// positions, leaving every other key in its existing order.
///
/// `serde_json::Map` preserves insertion order (`preserve_order`), so a plain
/// insert would append — putting `__typename` last however the client wrote it.
fn reinsert_typename_in_order(
    obj: &mut Map<String, JsonValue>,
    selections: &[FieldSelection],
    type_name: &str,
    schema: &CompiledSchema,
) {
    // Nothing to insert → leave the object, and its existing order, untouched.
    if !effective_selections(selections, type_name, schema)
        .iter()
        .any(|sel| sel.name == "__typename")
    {
        return;
    }

    let mut rebuilt = Map::new();
    for sel in effective_selections(selections, type_name, schema) {
        let key = sel.response_key();
        if sel.name == "__typename" {
            rebuilt.insert(key.to_string(), JsonValue::String(type_name.to_string()));
        } else if let Some(v) = obj.get(key) {
            rebuilt.insert(key.to_string(), v.clone());
        }
    }
    // Anything the selection walker did not account for keeps its place at the
    // end rather than being dropped — this pass adds a key, it never removes one.
    for (k, v) in obj.iter() {
        if !rebuilt.contains_key(k) {
            rebuilt.insert(k.clone(), v.clone());
        }
    }
    *obj = rebuilt;
}

/// Look up a field's stored value: canonical `snake_case` key first, then a
/// `camelCase` fallback for legacy metadata that used the GraphQL surface casing.
fn lookup_source<'a>(obj: &'a Map<String, JsonValue>, field_name: &str) -> Option<&'a JsonValue> {
    let (snake, camel) = stored_key_candidates(field_name);
    obj.get(&snake).or_else(|| camel.and_then(|c| obj.get(&c)))
}

/// Flatten a selection set for a concrete object type: direct fields, plus the
/// contents of any inline fragment `... on T` (or on an interface `T` implements).
/// Resolve inline `... on T` fragments in `selections` against `type_name`,
/// returning the flat list of field selections that apply to it.
///
/// Shared with the mutation runner's cascade projection, which navigates the
/// payload/envelope selection sets (`... on <Name>Payload`, `... on <EntityType>`)
/// the same way the entity projector does.
pub fn effective_selections<'a>(
    selections: &'a [FieldSelection],
    type_name: &str,
    schema: &CompiledSchema,
) -> Vec<&'a FieldSelection> {
    let mut out = Vec::new();
    for sel in selections {
        if let Some(frag_type) = sel.name.strip_prefix("...on ") {
            let frag_type = frag_type.trim();
            let applies = frag_type == type_name
                || schema
                    .find_type(type_name)
                    .is_some_and(|td| td.implements.iter().any(|i| i == frag_type));
            if applies {
                out.extend(effective_selections(&sel.nested_fields, type_name, schema));
            }
        } else {
            out.push(sel);
        }
    }
    out
}