this-rs 0.0.9

Framework for building complex multi-entity REST and GraphQL APIs with many relationships
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
//! Dynamic GraphQL schema using async-graphql with runtime type generation
//!
//! This module creates a GraphQL schema that dynamically exposes entity types
//! based on the registered entities in the ServerHost, using their actual field
//! definitions from the entity structs.

#[cfg(feature = "graphql")]
use async_graphql::*;
#[cfg(feature = "graphql")]
use serde_json::Value;
#[cfg(feature = "graphql")]
use std::sync::Arc;
#[cfg(feature = "graphql")]
use uuid::Uuid;

#[cfg(feature = "graphql")]
use crate::core::link::LinkEntity;
#[cfg(feature = "graphql")]
use crate::server::host::ServerHost;

/// Wrapper type for JSON values to satisfy orphan rules
#[cfg(feature = "graphql")]
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct JsonValue(pub Value);

/// JSON scalar type for dynamic fields
#[cfg(feature = "graphql")]
#[Scalar]
impl ScalarType for JsonValue {
    fn parse(value: async_graphql::Value) -> InputValueResult<Self> {
        fn parse_value(value: async_graphql::Value) -> Result<Value, String> {
            match value {
                async_graphql::Value::Null => Ok(Value::Null),
                async_graphql::Value::Number(n) => {
                    if let Some(i) = n.as_i64() {
                        Ok(Value::Number(serde_json::Number::from(i)))
                    } else if let Some(f) = n.as_f64() {
                        Ok(serde_json::Number::from_f64(f)
                            .map(Value::Number)
                            .unwrap_or(Value::Null))
                    } else {
                        Ok(Value::Null)
                    }
                }
                async_graphql::Value::String(s) => Ok(Value::String(s)),
                async_graphql::Value::Boolean(b) => Ok(Value::Bool(b)),
                async_graphql::Value::List(list) => {
                    let mut values = Vec::new();
                    for item in list {
                        values.push(parse_value(item)?);
                    }
                    Ok(Value::Array(values))
                }
                async_graphql::Value::Object(obj) => {
                    let mut map = serde_json::Map::new();
                    for (k, v) in obj {
                        map.insert(k.to_string(), parse_value(v)?);
                    }
                    Ok(Value::Object(map))
                }
                _ => Err("Invalid JSON value".to_string()),
            }
        }
        parse_value(value)
            .map(JsonValue)
            .map_err(InputValueError::custom)
    }

    fn to_value(&self) -> async_graphql::Value {
        fn to_gql_value(value: &Value) -> async_graphql::Value {
            match value {
                Value::Null => async_graphql::Value::Null,
                Value::Bool(b) => async_graphql::Value::Boolean(*b),
                Value::Number(n) => {
                    if let Some(i) = n.as_i64() {
                        async_graphql::Value::Number(i.into())
                    } else if let Some(f) = n.as_f64() {
                        async_graphql::Value::Number(
                            async_graphql::Number::from_f64(f).unwrap_or(0.into()),
                        )
                    } else {
                        async_graphql::Value::Null
                    }
                }
                Value::String(s) => async_graphql::Value::String(s.clone()),
                Value::Array(arr) => {
                    async_graphql::Value::List(arr.iter().map(to_gql_value).collect())
                }
                Value::Object(obj) => {
                    let mut map = async_graphql::indexmap::IndexMap::new();
                    for (k, v) in obj {
                        map.insert(async_graphql::Name::new(k), to_gql_value(v));
                    }
                    async_graphql::Value::Object(map)
                }
            }
        }
        to_gql_value(&self.0)
    }
}

/// Dynamic Query Root that creates resolvers for all registered entities
#[cfg(feature = "graphql")]
#[allow(dead_code)]
pub struct DynamicQueryRoot {
    pub host: Arc<ServerHost>,
}

#[cfg(feature = "graphql")]
#[Object]
impl DynamicQueryRoot {
    /// Get a list of all registered entity types
    async fn entity_types(&self) -> Vec<String> {
        self.host
            .entity_types()
            .into_iter()
            .map(|s| s.to_string())
            .collect()
    }

    /// Get an entity by ID and type - returns the full JSON object
    async fn entity(
        &self,
        id: ID,
        entity_type: String,
    ) -> async_graphql::Result<Option<JsonValue>> {
        let uuid = Uuid::parse_str(&id).map_err(|e| Error::new(format!("Invalid UUID: {}", e)))?;

        if let Some(fetcher) = self.host.entity_fetchers.get(&entity_type) {
            match fetcher.fetch_as_json(&uuid).await {
                Ok(json) => Ok(Some(JsonValue(json))),
                Err(_) => Ok(None),
            }
        } else {
            Err(Error::new(format!("Unknown entity type: {}", entity_type)))
        }
    }

    /// List entities of a specific type - returns full JSON objects
    async fn entities(
        &self,
        entity_type: String,
        limit: Option<i32>,
        offset: Option<i32>,
    ) -> async_graphql::Result<Vec<JsonValue>> {
        if let Some(fetcher) = self.host.entity_fetchers.get(&entity_type) {
            fetcher
                .list_as_json(limit, offset)
                .await
                .map(|list| list.into_iter().map(JsonValue).collect())
                .map_err(|e| Error::new(format!("Failed to list entities: {}", e)))
        } else {
            Err(Error::new(format!("Unknown entity type: {}", entity_type)))
        }
    }

    /// Get links for an entity (as source)
    async fn entity_links(&self, entity_id: ID) -> async_graphql::Result<Vec<JsonValue>> {
        let uuid =
            Uuid::parse_str(&entity_id).map_err(|e| Error::new(format!("Invalid UUID: {}", e)))?;

        let links = self
            .host
            .link_service
            .find_by_source(&uuid, None, None)
            .await
            .map_err(|e| Error::new(format!("Failed to get links: {}", e)))?;

        // Convert links to JSON
        let json_links: Vec<JsonValue> = links
            .into_iter()
            .map(|link| {
                JsonValue(serde_json::json!({
                    "id": link.id.to_string(),
                    "sourceId": link.source_id.to_string(),
                    "targetId": link.target_id.to_string(),
                    "linkType": link.link_type,
                    "metadata": link.metadata,
                    "createdAt": link.created_at.to_rfc3339(),
                }))
            })
            .collect();

        Ok(json_links)
    }
}

/// Dynamic Mutation Root for CRUD operations
#[cfg(feature = "graphql")]
#[allow(dead_code)]
pub struct DynamicMutationRoot {
    pub host: Arc<ServerHost>,
}

#[cfg(feature = "graphql")]
#[Object]
impl DynamicMutationRoot {
    /// Create a new entity of the specified type
    async fn create_entity(
        &self,
        entity_type: String,
        data: JsonValue,
    ) -> async_graphql::Result<JsonValue> {
        if let Some(creator) = self.host.entity_creators.get(&entity_type) {
            creator
                .create_from_json(data.0)
                .await
                .map(JsonValue)
                .map_err(|e| Error::new(format!("Failed to create entity: {}", e)))
        } else {
            Err(Error::new(format!("Unknown entity type: {}", entity_type)))
        }
    }

    /// Update an existing entity
    async fn update_entity(
        &self,
        id: ID,
        entity_type: String,
        data: JsonValue,
    ) -> async_graphql::Result<JsonValue> {
        let uuid = Uuid::parse_str(&id).map_err(|e| Error::new(format!("Invalid UUID: {}", e)))?;

        if let Some(creator) = self.host.entity_creators.get(&entity_type) {
            creator
                .update_from_json(&uuid, data.0)
                .await
                .map(JsonValue)
                .map_err(|e| Error::new(format!("Failed to update entity: {}", e)))
        } else {
            Err(Error::new(format!("Unknown entity type: {}", entity_type)))
        }
    }

    /// Delete an entity
    async fn delete_entity(&self, id: ID, entity_type: String) -> async_graphql::Result<bool> {
        let uuid = Uuid::parse_str(&id).map_err(|e| Error::new(format!("Invalid UUID: {}", e)))?;

        if let Some(creator) = self.host.entity_creators.get(&entity_type) {
            creator
                .delete(&uuid)
                .await
                .map(|_| true)
                .map_err(|e| Error::new(format!("Failed to delete entity: {}", e)))
        } else {
            Err(Error::new(format!("Unknown entity type: {}", entity_type)))
        }
    }

    /// Create a link between two entities
    async fn create_link(
        &self,
        source_id: ID,
        target_id: ID,
        link_type: String,
        metadata: Option<JsonValue>,
    ) -> async_graphql::Result<JsonValue> {
        let source_uuid = Uuid::parse_str(&source_id)
            .map_err(|e| Error::new(format!("Invalid source UUID: {}", e)))?;
        let target_uuid = Uuid::parse_str(&target_id)
            .map_err(|e| Error::new(format!("Invalid target UUID: {}", e)))?;

        let metadata_value = metadata.map(|j| j.0);

        let link_entity = LinkEntity::new(link_type, source_uuid, target_uuid, metadata_value);

        let link = self
            .host
            .link_service
            .create(link_entity)
            .await
            .map_err(|e| Error::new(format!("Failed to create link: {}", e)))?;

        Ok(JsonValue(serde_json::json!({
            "id": link.id.to_string(),
            "sourceId": link.source_id.to_string(),
            "targetId": link.target_id.to_string(),
            "linkType": link.link_type,
            "metadata": link.metadata,
            "createdAt": link.created_at.to_rfc3339(),
        })))
    }

    /// Delete a link
    async fn delete_link(&self, link_id: ID) -> async_graphql::Result<bool> {
        let uuid =
            Uuid::parse_str(&link_id).map_err(|e| Error::new(format!("Invalid UUID: {}", e)))?;

        self.host
            .link_service
            .delete(&uuid)
            .await
            .map(|_| true)
            .map_err(|e| Error::new(format!("Failed to delete link: {}", e)))
    }
}

/// Build the dynamic GraphQL schema
#[cfg(feature = "graphql")]
#[allow(dead_code)]
pub fn build_dynamic_schema(
    host: Arc<ServerHost>,
) -> Schema<DynamicQueryRoot, DynamicMutationRoot, EmptySubscription> {
    Schema::build(
        DynamicQueryRoot { host: host.clone() },
        DynamicMutationRoot { host },
        EmptySubscription,
    )
    .finish()
}

#[cfg(test)]
#[cfg(feature = "graphql")]
mod tests {
    use super::*;
    use crate::config::{EntityAuthConfig, EntityConfig, LinksConfig};
    use crate::core::{EntityCreator, EntityFetcher};
    use crate::server::entity_registry::{EntityDescriptor, EntityRegistry};
    use crate::server::host::ServerHost;
    use crate::storage::in_memory::InMemoryLinkService;
    use async_trait::async_trait;
    use axum::Router;
    use serde_json::json;
    use std::collections::HashMap;

    // -----------------------------------------------------------------------
    // Mock infrastructure
    // -----------------------------------------------------------------------

    struct MockFetcher {
        entities: std::sync::Mutex<HashMap<Uuid, Value>>,
    }

    impl MockFetcher {
        fn new() -> Self {
            Self {
                entities: std::sync::Mutex::new(HashMap::new()),
            }
        }

        fn with_entity(self, id: Uuid, entity: Value) -> Self {
            self.entities
                .lock()
                .expect("lock poisoned")
                .insert(id, entity);
            self
        }
    }

    #[async_trait]
    impl EntityFetcher for MockFetcher {
        async fn fetch_as_json(&self, entity_id: &Uuid) -> anyhow::Result<Value> {
            let entities = self.entities.lock().expect("lock poisoned");
            entities
                .get(entity_id)
                .cloned()
                .ok_or_else(|| anyhow::anyhow!("Entity not found: {}", entity_id))
        }

        async fn list_as_json(
            &self,
            limit: Option<i32>,
            offset: Option<i32>,
        ) -> anyhow::Result<Vec<Value>> {
            let entities = self.entities.lock().expect("lock poisoned");
            let mut all: Vec<Value> = entities.values().cloned().collect();
            let start = offset.unwrap_or(0) as usize;
            if start < all.len() {
                all = all.split_off(start);
            } else {
                all.clear();
            }
            if let Some(lim) = limit {
                all.truncate(lim as usize);
            }
            Ok(all)
        }
    }

    struct MockCreator;

    #[async_trait]
    impl EntityCreator for MockCreator {
        async fn create_from_json(&self, mut data: Value) -> anyhow::Result<Value> {
            let id = Uuid::new_v4();
            if let Some(obj) = data.as_object_mut() {
                obj.insert("id".to_string(), json!(id.to_string()));
            }
            Ok(data)
        }

        async fn update_from_json(
            &self,
            entity_id: &Uuid,
            mut data: Value,
        ) -> anyhow::Result<Value> {
            if let Some(obj) = data.as_object_mut() {
                obj.insert("id".to_string(), json!(entity_id.to_string()));
            }
            Ok(data)
        }

        async fn delete(&self, _entity_id: &Uuid) -> anyhow::Result<()> {
            Ok(())
        }
    }

    struct StubDescriptor {
        entity_type: String,
        plural: String,
    }

    impl StubDescriptor {
        fn new(singular: &str, plural: &str) -> Self {
            Self {
                entity_type: singular.to_string(),
                plural: plural.to_string(),
            }
        }
    }

    impl EntityDescriptor for StubDescriptor {
        fn entity_type(&self) -> &str {
            &self.entity_type
        }
        fn plural(&self) -> &str {
            &self.plural
        }
        fn build_routes(&self) -> Router {
            Router::new()
        }
    }

    fn build_test_host(
        fetchers: HashMap<String, Arc<dyn EntityFetcher>>,
        creators: HashMap<String, Arc<dyn EntityCreator>>,
    ) -> Arc<ServerHost> {
        let link_service = Arc::new(InMemoryLinkService::new());
        let config = LinksConfig {
            entities: vec![EntityConfig {
                singular: "order".to_string(),
                plural: "orders".to_string(),
                auth: EntityAuthConfig::default(),
            }],
            links: vec![],
            validation_rules: None,
            events: None,
            sinks: None,
        };

        let mut registry = EntityRegistry::new();
        registry.register(Box::new(StubDescriptor::new("order", "orders")));

        Arc::new(
            ServerHost::from_builder_components(link_service, config, registry, fetchers, creators)
                .expect("should build test host"),
        )
    }

    // -----------------------------------------------------------------------
    // JsonValue scalar: parse roundtrip tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_json_value_parse_null() {
        let result = <JsonValue as ScalarType>::parse(async_graphql::Value::Null)
            .expect("should parse null");
        assert_eq!(result.0, Value::Null);
    }

    #[test]
    fn test_json_value_parse_boolean_true() {
        let result = <JsonValue as ScalarType>::parse(async_graphql::Value::Boolean(true))
            .expect("should parse bool");
        assert_eq!(result.0, Value::Bool(true));
    }

    #[test]
    fn test_json_value_parse_boolean_false() {
        let result = <JsonValue as ScalarType>::parse(async_graphql::Value::Boolean(false))
            .expect("should parse bool");
        assert_eq!(result.0, Value::Bool(false));
    }

    #[test]
    fn test_json_value_parse_integer() {
        let gql_num = async_graphql::Value::Number(42.into());
        let result = <JsonValue as ScalarType>::parse(gql_num).expect("should parse int");
        assert_eq!(result.0, json!(42));
    }

    #[test]
    fn test_json_value_parse_float() {
        let gql_num =
            async_graphql::Value::Number(async_graphql::Number::from_f64(3.15).expect("valid f64"));
        let result = <JsonValue as ScalarType>::parse(gql_num).expect("should parse float");
        assert_eq!(result.0, json!(3.15));
    }

    #[test]
    fn test_json_value_parse_string() {
        let gql_str = async_graphql::Value::String("hello".to_string());
        let result = <JsonValue as ScalarType>::parse(gql_str).expect("should parse string");
        assert_eq!(result.0, Value::String("hello".to_string()));
    }

    #[test]
    fn test_json_value_parse_list() {
        let gql_list = async_graphql::Value::List(vec![
            async_graphql::Value::Number(1.into()),
            async_graphql::Value::String("two".to_string()),
            async_graphql::Value::Boolean(true),
        ]);
        let result = <JsonValue as ScalarType>::parse(gql_list).expect("should parse list");
        assert_eq!(result.0, json!([1, "two", true]));
    }

    #[test]
    fn test_json_value_parse_object() {
        let mut map = async_graphql::indexmap::IndexMap::new();
        map.insert(
            async_graphql::Name::new("key"),
            async_graphql::Value::String("value".to_string()),
        );
        let gql_obj = async_graphql::Value::Object(map);
        let result = <JsonValue as ScalarType>::parse(gql_obj).expect("should parse object");
        assert_eq!(result.0, json!({"key": "value"}));
    }

    // -----------------------------------------------------------------------
    // JsonValue scalar: to_value roundtrip tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_json_value_to_value_null() {
        let jv = JsonValue(Value::Null);
        assert_eq!(ScalarType::to_value(&jv), async_graphql::Value::Null);
    }

    #[test]
    fn test_json_value_to_value_bool() {
        let jv = JsonValue(Value::Bool(true));
        assert_eq!(
            ScalarType::to_value(&jv),
            async_graphql::Value::Boolean(true)
        );
    }

    #[test]
    fn test_json_value_to_value_integer() {
        let jv = JsonValue(json!(42));
        let gql_val = ScalarType::to_value(&jv);
        if let async_graphql::Value::Number(n) = gql_val {
            assert_eq!(n.as_i64(), Some(42));
        } else {
            panic!("expected Number variant");
        }
    }

    #[test]
    fn test_json_value_to_value_float() {
        let jv = JsonValue(json!(3.15));
        let gql_val = ScalarType::to_value(&jv);
        if let async_graphql::Value::Number(n) = gql_val {
            let f = n.as_f64().expect("should be f64");
            assert!((f - 3.15).abs() < 1e-10);
        } else {
            panic!("expected Number variant");
        }
    }

    #[test]
    fn test_json_value_to_value_string() {
        let jv = JsonValue(Value::String("hello".to_string()));
        assert_eq!(
            ScalarType::to_value(&jv),
            async_graphql::Value::String("hello".to_string())
        );
    }

    #[test]
    fn test_json_value_to_value_array() {
        let jv = JsonValue(json!([1, "two"]));
        let gql_val = ScalarType::to_value(&jv);
        if let async_graphql::Value::List(items) = gql_val {
            assert_eq!(items.len(), 2);
        } else {
            panic!("expected List variant");
        }
    }

    #[test]
    fn test_json_value_to_value_object() {
        let jv = JsonValue(json!({"a": 1}));
        let gql_val = ScalarType::to_value(&jv);
        if let async_graphql::Value::Object(map) = gql_val {
            assert!(map.contains_key("a"), "should contain key 'a'");
        } else {
            panic!("expected Object variant");
        }
    }

    // -----------------------------------------------------------------------
    // JsonValue scalar: full roundtrip parse -> to_value -> parse
    // -----------------------------------------------------------------------

    #[test]
    fn test_json_value_roundtrip() {
        let original = json!({"name": "test", "count": 5, "active": true, "items": [1, 2]});
        let jv = JsonValue(original.clone());
        let gql_val = ScalarType::to_value(&jv);
        let parsed = <JsonValue as ScalarType>::parse(gql_val).expect("should parse back");
        assert_eq!(parsed.0, original);
    }

    // -----------------------------------------------------------------------
    // build_dynamic_schema smoke test
    // -----------------------------------------------------------------------

    #[test]
    fn test_build_dynamic_schema_does_not_panic() {
        let host = build_test_host(HashMap::new(), HashMap::new());
        let _schema = build_dynamic_schema(host);
    }

    // -----------------------------------------------------------------------
    // DynamicQueryRoot: entity_types
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dynamic_query_entity_types() {
        let host = build_test_host(HashMap::new(), HashMap::new());
        let schema = build_dynamic_schema(host);

        let result = schema.execute("{ entityTypes }").await;
        assert!(result.errors.is_empty(), "errors: {:?}", result.errors);
        let data = result.data.into_json().expect("json");
        let types = data["entityTypes"].as_array().expect("array");
        let strs: Vec<&str> = types.iter().map(|v| v.as_str().expect("str")).collect();
        assert!(strs.contains(&"order"), "should have order");
    }

    // -----------------------------------------------------------------------
    // DynamicQueryRoot: entity found
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dynamic_query_entity_found() {
        let order_id = Uuid::new_v4();
        let order_json = json!({"id": order_id.to_string(), "name": "Order #1"});

        let mut fetchers: HashMap<String, Arc<dyn EntityFetcher>> = HashMap::new();
        fetchers.insert(
            "order".to_string(),
            Arc::new(MockFetcher::new().with_entity(order_id, order_json)),
        );

        let host = build_test_host(fetchers, HashMap::new());
        let schema = build_dynamic_schema(host);

        let query = format!(r#"{{ entity(id: "{}", entityType: "order") }}"#, order_id);
        let result = schema.execute(&query).await;
        assert!(result.errors.is_empty(), "errors: {:?}", result.errors);
    }

    // -----------------------------------------------------------------------
    // DynamicQueryRoot: entity unknown type returns error
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dynamic_query_entity_unknown_type_errors() {
        let host = build_test_host(HashMap::new(), HashMap::new());
        let schema = build_dynamic_schema(host);

        let id = Uuid::new_v4();
        let query = format!(r#"{{ entity(id: "{}", entityType: "widget") }}"#, id);
        let result = schema.execute(&query).await;
        assert!(
            !result.errors.is_empty(),
            "unknown type should produce error"
        );
        let err_msg = format!("{:?}", result.errors);
        assert!(
            err_msg.contains("Unknown entity type"),
            "should mention unknown entity type: {}",
            err_msg
        );
    }

    // -----------------------------------------------------------------------
    // DynamicQueryRoot: entity invalid UUID
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dynamic_query_entity_invalid_uuid() {
        let mut fetchers: HashMap<String, Arc<dyn EntityFetcher>> = HashMap::new();
        fetchers.insert("order".to_string(), Arc::new(MockFetcher::new()));

        let host = build_test_host(fetchers, HashMap::new());
        let schema = build_dynamic_schema(host);

        let query = r#"{ entity(id: "not-a-uuid", entityType: "order") }"#;
        let result = schema.execute(query).await;
        assert!(
            !result.errors.is_empty(),
            "invalid UUID should produce error"
        );
    }

    // -----------------------------------------------------------------------
    // DynamicQueryRoot: entities list
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dynamic_query_entities_list() {
        let id1 = Uuid::new_v4();
        let id2 = Uuid::new_v4();
        let mut fetchers: HashMap<String, Arc<dyn EntityFetcher>> = HashMap::new();
        fetchers.insert(
            "order".to_string(),
            Arc::new(
                MockFetcher::new()
                    .with_entity(id1, json!({"id": id1.to_string(), "name": "A"}))
                    .with_entity(id2, json!({"id": id2.to_string(), "name": "B"})),
            ),
        );

        let host = build_test_host(fetchers, HashMap::new());
        let schema = build_dynamic_schema(host);

        let query = r#"{ entities(entityType: "order") }"#;
        let result = schema.execute(query).await;
        assert!(result.errors.is_empty(), "errors: {:?}", result.errors);
    }

    // -----------------------------------------------------------------------
    // DynamicQueryRoot: entities for unknown type
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dynamic_query_entities_unknown_type() {
        let host = build_test_host(HashMap::new(), HashMap::new());
        let schema = build_dynamic_schema(host);

        let query = r#"{ entities(entityType: "widget") }"#;
        let result = schema.execute(query).await;
        assert!(
            !result.errors.is_empty(),
            "unknown type should produce error"
        );
    }

    // -----------------------------------------------------------------------
    // DynamicMutationRoot: createEntity
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dynamic_mutation_create_entity() {
        let mut fetchers: HashMap<String, Arc<dyn EntityFetcher>> = HashMap::new();
        fetchers.insert("order".to_string(), Arc::new(MockFetcher::new()));

        let mut creators: HashMap<String, Arc<dyn EntityCreator>> = HashMap::new();
        creators.insert("order".to_string(), Arc::new(MockCreator));

        let host = build_test_host(fetchers, creators);
        let schema = build_dynamic_schema(host);

        let query = r#"mutation { createEntity(entityType: "order", data: {name: "test"}) }"#;
        let result = schema.execute(query).await;
        assert!(result.errors.is_empty(), "errors: {:?}", result.errors);
    }

    // -----------------------------------------------------------------------
    // DynamicMutationRoot: createEntity unknown type
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dynamic_mutation_create_entity_unknown_type() {
        let host = build_test_host(HashMap::new(), HashMap::new());
        let schema = build_dynamic_schema(host);

        let query = r#"mutation { createEntity(entityType: "widget", data: {name: "x"}) }"#;
        let result = schema.execute(query).await;
        assert!(
            !result.errors.is_empty(),
            "unknown type should produce error"
        );
    }

    // -----------------------------------------------------------------------
    // DynamicMutationRoot: deleteEntity
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dynamic_mutation_delete_entity() {
        let mut creators: HashMap<String, Arc<dyn EntityCreator>> = HashMap::new();
        creators.insert("order".to_string(), Arc::new(MockCreator));

        let host = build_test_host(HashMap::new(), creators);
        let schema = build_dynamic_schema(host);

        let id = Uuid::new_v4();
        let query = format!(
            r#"mutation {{ deleteEntity(id: "{}", entityType: "order") }}"#,
            id
        );
        let result = schema.execute(&query).await;
        assert!(result.errors.is_empty(), "errors: {:?}", result.errors);
        let data = result.data.into_json().expect("json");
        assert_eq!(data["deleteEntity"], true);
    }

    // -----------------------------------------------------------------------
    // DynamicMutationRoot: createLink and entityLinks query
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dynamic_mutation_create_link_and_query_entity_links() {
        let host = build_test_host(HashMap::new(), HashMap::new());
        let schema = build_dynamic_schema(host);

        let source_id = Uuid::new_v4();
        let target_id = Uuid::new_v4();

        // Create a link
        let create_query = format!(
            r#"mutation {{ createLink(sourceId: "{}", targetId: "{}", linkType: "has_invoice") }}"#,
            source_id, target_id
        );
        let result = schema.execute(&create_query).await;
        assert!(
            result.errors.is_empty(),
            "create errors: {:?}",
            result.errors
        );

        // Query entity_links
        let links_query = format!(r#"{{ entityLinks(entityId: "{}") }}"#, source_id);
        let result = schema.execute(&links_query).await;
        assert!(
            result.errors.is_empty(),
            "query errors: {:?}",
            result.errors
        );
    }

    // -----------------------------------------------------------------------
    // DynamicMutationRoot: deleteLink
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_dynamic_mutation_delete_link() {
        let host = build_test_host(HashMap::new(), HashMap::new());

        // Create a link in the store first
        let source_id = Uuid::new_v4();
        let target_id = Uuid::new_v4();
        let link = LinkEntity::new("has_invoice", source_id, target_id, None);
        let created = host
            .link_service
            .create(link)
            .await
            .expect("should create link");

        let schema = build_dynamic_schema(host);

        let query = format!(r#"mutation {{ deleteLink(linkId: "{}") }}"#, created.id);
        let result = schema.execute(&query).await;
        assert!(result.errors.is_empty(), "errors: {:?}", result.errors);
        let data = result.data.into_json().expect("json");
        assert_eq!(data["deleteLink"], true);
    }
}