apollo-smith 0.16.0

A GraphQL test case generator.
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
use crate::generators::Generator;
use crate::generators::Generators;
use crate::random::RandomProvider;
use crate::random::ResponseError;
use apollo_compiler::executable::Field;
use apollo_compiler::executable::Selection;
use apollo_compiler::executable::SelectionSet;
use apollo_compiler::schema::ExtendedType;
use apollo_compiler::validation::Valid;
use apollo_compiler::ExecutableDocument;
use apollo_compiler::Name;
use apollo_compiler::Node;
use apollo_compiler::Schema;
use indexmap::IndexMap;
use serde_json_bytes::json;
use serde_json_bytes::Map;
use serde_json_bytes::Value;

const TYPENAME: &str = "__typename";

/// Builds a GraphQL response which matches the shape of a given executable GraphQL document.
///
/// `ResponseBuilder` is generic over its randomness source via the [`RandomProvider`] trait.
/// This allows it to be used with [`arbitrary::Unstructured`] for fuzz testing, with
/// [`RandProvider`][crate::RandProvider] for standard random generation, or with any custom
/// implementation.
///
/// # Example
///
/// ```ignore
/// use apollo_smith::{ResponseBuilder, RandProvider};
///
/// let mut rng = RandProvider(rand::rng());
/// let response = ResponseBuilder::new(&mut rng, &doc, &schema).build()?;
/// ```
pub struct ResponseBuilder<'a, 'doc, 'schema, R: RandomProvider> {
    rng: &'a mut R,
    doc: &'doc Valid<ExecutableDocument>,
    schema: &'schema Valid<Schema>,
    generators: Generators<R>,
    min_list_size: usize,
    max_list_size: usize,
    null_ratio: Option<(u32, u32)>,
    operation_name: Option<&'doc str>,
}

impl<'a, 'doc, 'schema, R: RandomProvider> ResponseBuilder<'a, 'doc, 'schema, R> {
    /// Create a new `ResponseBuilder`.
    pub fn new(
        rng: &'a mut R,
        doc: &'doc Valid<ExecutableDocument>,
        schema: &'schema Valid<Schema>,
    ) -> Self {
        Self {
            rng,
            doc,
            schema,
            generators: Generators::default(),
            min_list_size: 0,
            max_list_size: 5,
            null_ratio: None,
            operation_name: None,
        }
    }

    /// Register a [`Generator`] for the named GraphQL type.
    ///
    /// When the builder is about to produce a value of this type — at the root or any
    /// nested position, including each item of a list — the generator is invoked
    /// instead of the default behavior. For object, interface, and union types, the
    /// generator receives the requested fields with fragment spreads and inline
    /// fragments already flattened and grouped by response key, and its return value
    /// is used as-is (the builder does not recurse into it). For scalar types, the
    /// `fields` argument is empty and the generator's return value replaces the
    /// configured default.
    pub fn with_generator<G>(mut self, type_name: Name, generator: G) -> Self
    where
        G: Generator<R> + 'static,
    {
        self.generators.insert(type_name, Box::new(generator));
        self
    }

    /// Set the minimum number of items per list field. If unset, defaults to 0.
    pub fn with_min_list_size(mut self, min_size: usize) -> Self {
        self.min_list_size = min_size;
        self
    }

    /// Set the maximum number of items per list field. If unset, defaults to 5.
    pub fn with_max_list_size(mut self, max_size: usize) -> Self {
        self.max_list_size = max_size;
        self
    }

    /// Set the frequency of null values for nullable fields. If unset, fields will never be null.
    pub fn with_null_ratio(mut self, numerator: u32, denominator: u32) -> Self {
        self.null_ratio = Some((numerator, denominator));
        self
    }

    /// Set the operation name to generate a response for. If unset, uses the anonymous operation.
    /// If the operation does not exist, returns a response with `data: null`.
    pub fn with_operation_name(mut self, operation_name: Option<&'doc str>) -> Self {
        self.operation_name = operation_name;
        self
    }

    /// Builds a complete GraphQL response `Value` with a `data` key, matching the shape of `self.doc`.
    pub fn build(mut self) -> Result<Value, ResponseError> {
        if let Ok(operation) = self.doc.operations.get(self.operation_name) {
            let data = self.selection_set(&operation.selection_set)?;
            Ok(json!({ "data": data }))
        } else {
            Ok(json!({ "data": null }))
        }
    }

    /// Builds just the data portion of the response (without the `{ "data": ... }` wrapper).
    ///
    /// This is useful if you need to manipulate the response data before wrapping it into JSON.
    pub fn build_data(&mut self) -> Result<Value, ResponseError> {
        if let Ok(operation) = self.doc.operations.get(self.operation_name) {
            self.selection_set(&operation.selection_set)
        } else {
            Ok(Value::Null)
        }
    }

    /// Collect fields from a selection set, grouping by response key (alias or field name).
    ///
    /// Inline fragments and fragment spreads are flattened into the result, but only when
    /// their type condition applies to `concrete_type`. Fields sharing a response key are
    /// merged into a single entry.
    fn collect_fields(
        &self,
        selection_set: &SelectionSet,
        concrete_type: &Name,
    ) -> IndexMap<String, Vec<Node<Field>>> {
        let mut collected: IndexMap<String, Vec<Node<Field>>> = IndexMap::new();

        for selection in &selection_set.selections {
            match selection {
                Selection::Field(field) => {
                    let key = field.alias.as_ref().unwrap_or(&field.name).to_string();
                    collected.entry(key).or_default().push(field.clone());
                }
                Selection::FragmentSpread(fragment) => {
                    if let Some(fragment_def) = self.doc.fragments.get(&fragment.fragment_name) {
                        if self.type_condition_matches(fragment_def.type_condition(), concrete_type)
                        {
                            for (key, mut fields) in
                                self.collect_fields(&fragment_def.selection_set, concrete_type)
                            {
                                collected.entry(key).or_default().append(&mut fields);
                            }
                        }
                    }
                }
                Selection::InlineFragment(inline_fragment) => {
                    let matches = match &inline_fragment.type_condition {
                        None => true,
                        Some(cond) => self.type_condition_matches(cond, concrete_type),
                    };
                    if matches {
                        for (key, mut fields) in
                            self.collect_fields(&inline_fragment.selection_set, concrete_type)
                        {
                            collected.entry(key).or_default().append(&mut fields);
                        }
                    }
                }
            }
        }

        collected
    }

    /// Resolve the concrete object type that will be produced for `ty`.
    ///
    /// For unions, a random member is chosen. For interfaces, a random implementing
    /// object type is chosen. For object (or any other) types, `ty` is returned as-is.
    fn concrete_type<'s>(&mut self, ty: &'s Name) -> Result<&'s Name, ResponseError>
    where
        'schema: 's,
    {
        match self.schema.types.get(ty) {
            Some(ExtendedType::Union(union_ty)) => {
                let idx = self.rng.choose_index(union_ty.members.len())?;
                let member = union_ty
                    .members
                    .get_index(idx)
                    .expect("choose_index returned valid index");
                Ok(&member.name)
            }
            Some(ExtendedType::Interface(_)) => {
                let count = self
                    .schema
                    .types
                    .values()
                    .filter(|t| {
                        matches!(t, ExtendedType::Object(obj) if obj.implements_interfaces.contains(ty))
                    })
                    .count();
                if count == 0 {
                    return Ok(ty);
                }
                let idx = self.rng.choose_index(count)?;
                let chosen = self
                    .schema
                    .types
                    .iter()
                    .filter_map(|(name, t)| match t {
                        ExtendedType::Object(obj) if obj.implements_interfaces.contains(ty) => {
                            Some(name)
                        }
                        _ => None,
                    })
                    .nth(idx)
                    .expect("idx came from counting the same filter");
                Ok(chosen)
            }
            _ => Ok(ty),
        }
    }

    /// Whether a fragment with type condition `cond` should contribute fields when the
    /// object being generated has concrete type `concrete`.
    fn type_condition_matches(&self, cond: &Name, concrete: &Name) -> bool {
        if cond == concrete {
            return true;
        }
        match self.schema.types.get(cond) {
            Some(ExtendedType::Interface(_)) => matches!(
                self.schema.types.get(concrete),
                Some(ExtendedType::Object(obj)) if obj.implements_interfaces.contains(cond)
            ),
            Some(ExtendedType::Union(union_ty)) => {
                union_ty.members.iter().any(|m| m.name == *concrete)
            }
            _ => false,
        }
    }

    fn selection_set(&mut self, selection_set: &SelectionSet) -> Result<Value, ResponseError> {
        let concrete = self.concrete_type(&selection_set.ty)?;
        let grouped_fields = self.collect_fields(selection_set, concrete);

        if let Some(result) =
            self.generators
                .try_generate(&selection_set.ty, self.rng, &grouped_fields)
        {
            return result;
        }

        let mut result = Map::new();

        for (key, fields) in grouped_fields {
            // The first field is representative for schema-defined metadata (type, nullability, etc.)
            let meta_field = &fields[0];

            let val = if meta_field.name == TYPENAME {
                Value::String(concrete.to_string().into())
            } else if !meta_field.ty().is_non_null() && self.should_be_null()? {
                Value::Null
            } else {
                self.generate_field_value(&fields, meta_field)?
            };

            result.insert(key, val);
        }

        Ok(Value::Object(result))
    }

    /// Generate the value for a (possibly merged) field group.
    fn generate_field_value(
        &mut self,
        fields: &[Node<Field>],
        meta_field: &Node<Field>,
    ) -> Result<Value, ResponseError> {
        let has_selection_set = !meta_field.selection_set.is_empty();
        let is_list = meta_field.ty().is_list();

        if has_selection_set {
            // Merge sub-selections from all occurrences of this field
            let mut merged_selections = Vec::new();
            for field in fields {
                merged_selections.extend_from_slice(&field.selection_set.selections);
            }
            let full_selection_set = SelectionSet {
                ty: meta_field.selection_set.ty.clone(),
                selections: merged_selections,
            };

            if is_list {
                self.repeated_selection_set(&full_selection_set)
            } else {
                self.selection_set(&full_selection_set)
            }
        } else if is_list {
            self.repeated_leaf_field(meta_field.ty().inner_named_type())
        } else {
            self.leaf_field(meta_field.ty().inner_named_type())
        }
    }

    fn repeated_selection_set(
        &mut self,
        selection_set: &SelectionSet,
    ) -> Result<Value, ResponseError> {
        let num_values = self.arbitrary_len()?;
        let mut values = Vec::with_capacity(num_values);
        for _ in 0..num_values {
            values.push(self.selection_set(selection_set)?);
        }
        Ok(Value::Array(values))
    }

    fn leaf_field(&mut self, type_name: &Name) -> Result<Value, ResponseError> {
        let extended_ty = self
            .schema
            .types
            .get(type_name)
            .expect("validated schema should contain the type");
        match extended_ty {
            ExtendedType::Enum(enum_ty) => {
                let idx = self.rng.choose_index(enum_ty.values.len())?;
                let enum_value = enum_ty
                    .values
                    .values()
                    .nth(idx)
                    .expect("choose_index returned valid index");
                Ok(Value::String(enum_value.value.to_string().into()))
            }
            ExtendedType::Scalar(scalar) => self.generators.generate_scalar(&scalar.name, self.rng),
            _ => unreachable!("A field with an empty selection set must be a scalar or enum type"),
        }
    }

    fn repeated_leaf_field(&mut self, type_name: &Name) -> Result<Value, ResponseError> {
        let num_values = self.arbitrary_len()?;
        let mut values = Vec::with_capacity(num_values);
        for _ in 0..num_values {
            values.push(self.leaf_field(type_name)?);
        }
        Ok(Value::Array(values))
    }

    fn arbitrary_len(&mut self) -> Result<usize, ResponseError> {
        self.rng
            .gen_usize_range(self.min_list_size, self.max_list_size)
    }

    fn should_be_null(&mut self) -> Result<bool, ResponseError> {
        if let Some((numerator, denominator)) = self.null_ratio {
            self.rng.ratio(numerator, denominator)
        } else {
            Ok(false)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::RandProvider;

    /// Helper: parse schema + query, build a response with a seeded RNG.
    fn build_response(schema_sdl: &str, query: &str) -> Value {
        let schema = Schema::parse_and_validate(schema_sdl, "schema.graphql").unwrap();
        let doc = ExecutableDocument::parse_and_validate(&schema, query, "query.graphql").unwrap();
        let mut rng = RandProvider(rand::rng());
        ResponseBuilder::new(&mut rng, &doc, &schema)
            .with_null_ratio(0, 1) // never null, so we can assert field presence
            .with_min_list_size(1)
            .build()
            .unwrap()
    }

    const SIMPLE_SCHEMA: &str = r#"
        type Query {
            user(id: ID!): User
            posts: [Post!]!
        }
        type User {
            id: ID!
            name: String!
            email: String!
            address: Address!
            is_active: Boolean!
            distance: Float!
        }
        type Address {
            city: String!
            state: String!
        }
        type Post {
            id: ID!
            title: String!
            author: User!
            views: Int!
        }
    "#;

    const UNION_SCHEMA: &str = r#"
        type Query {
            user(id: ID!): User
        }
        type User {
            id: ID!
            name: String!
            content: [Content!]!
        }
        type Post {
            title: String!
            views: Int!
        }
        type Article {
            title: String!
            citations: [String!]!
        }
        union Content = Post | Article
    "#;

    const INTERFACE_SCHEMA: &str = r#"
        type Query {
            user(id: ID!): User
        }
        type User {
            id: ID!
            name: String!
            content: [Content!]!
        }
        interface Content {
            title: String!
        }
        type Post implements Content {
            title: String!
            views: Int!
        }
        type Article implements Content {
            title: String!
            citations: [String!]!
        }
    "#;

    #[test]
    fn basic_response_shape() {
        let response = build_response(
            SIMPLE_SCHEMA,
            "query { user(id: \"1\") { id name email is_active distance } }",
        );
        let data = response.get("data").expect("missing data");
        let user = data.get("user").expect("missing user");
        assert!(user.get("id").is_some());
        assert!(user.get("name").is_some());
        assert!(user.get("email").is_some());
        assert!(user.get("is_active").unwrap().is_boolean());
        assert!(user.get("distance").unwrap().is_number());
    }

    #[test]
    fn nested_objects() {
        let response = build_response(
            SIMPLE_SCHEMA,
            "query { user(id: \"1\") { id address { city state } } }",
        );
        let user = response.get("data").unwrap().get("user").unwrap();
        let address = user.get("address").expect("missing address");
        assert!(address.get("city").is_some());
        assert!(address.get("state").is_some());
    }

    #[test]
    fn list_fields() {
        let response = build_response(
            SIMPLE_SCHEMA,
            "query { posts { id title author { name } views } }",
        );
        let posts = response
            .get("data")
            .unwrap()
            .get("posts")
            .unwrap()
            .as_array()
            .expect("posts should be an array");
        assert!(!posts.is_empty(), "min_list_size is 1");
        for post in posts {
            assert!(post.get("id").is_some());
            assert!(post.get("title").is_some());
            assert!(post.get("views").is_some());
            assert!(post.get("author").unwrap().get("name").is_some());
        }
    }

    #[test]
    fn alias_support() {
        let response = build_response(
            SIMPLE_SCHEMA,
            r#"query { user(id: "1") { userId: id fullName: name } }"#,
        );
        let user = response.get("data").unwrap().get("user").unwrap();
        assert!(
            user.get("userId").is_some(),
            "alias 'userId' should be the key"
        );
        assert!(
            user.get("fullName").is_some(),
            "alias 'fullName' should be the key"
        );
        // The original field names should NOT appear
        assert!(user.get("id").is_none());
        assert!(user.get("name").is_none());
    }

    #[test]
    fn inline_fragment() {
        let response = build_response(
            SIMPLE_SCHEMA,
            r#"query { user(id: "1") { id ... on User { name email } } }"#,
        );
        let user = response.get("data").unwrap().get("user").unwrap();
        assert!(user.get("id").is_some());
        assert!(
            user.get("name").is_some(),
            "inline fragment field should be present"
        );
        assert!(
            user.get("email").is_some(),
            "inline fragment field should be present"
        );
    }

    #[test]
    fn fragment_spread() {
        let response = build_response(
            SIMPLE_SCHEMA,
            r#"
            fragment UserDetails on User {
                name
                email
                address { city state }
            }
            query { user(id: "1") { id ...UserDetails } }
            "#,
        );
        let user = response.get("data").unwrap().get("user").unwrap();
        assert!(user.get("id").is_some());
        assert!(
            user.get("name").is_some(),
            "fragment field should be present"
        );
        assert!(
            user.get("email").is_some(),
            "fragment field should be present"
        );
        let address = user
            .get("address")
            .expect("fragment nested object should be present");
        assert!(address.get("city").is_some());
        assert!(address.get("state").is_some());
    }

    #[test]
    fn union_typename_picks_member() {
        let schema = Schema::parse_and_validate(UNION_SCHEMA, "schema.graphql").unwrap();
        let query = r#"
            query {
                user(id: "1") {
                    content {
                        __typename
                        ... on Post { title views }
                        ... on Article { title citations }
                    }
                }
            }
        "#;
        let doc = ExecutableDocument::parse_and_validate(&schema, query, "query.graphql").unwrap();

        let mut seen_post = false;
        let mut seen_article = false;

        // Run multiple times to increase confidence that we see both members
        for _ in 0..100 {
            let mut rng = RandProvider(rand::rng());
            let response = ResponseBuilder::new(&mut rng, &doc, &schema)
                .with_null_ratio(0, 1)
                .with_min_list_size(1)
                .build()
                .unwrap();

            let content = response
                .get("data")
                .unwrap()
                .get("user")
                .unwrap()
                .get("content")
                .unwrap()
                .as_array()
                .unwrap();

            for item in content {
                let typename = item.get("__typename").unwrap().as_str().unwrap();
                assert_ne!(typename, "Content", "__typename must not be the union name");
                assert!(
                    typename == "Post" || typename == "Article",
                    "unexpected __typename: {typename}"
                );
                // Each item should only have the fields from the inline fragment whose
                // type condition matches the chosen __typename.
                match typename {
                    "Post" => {
                        assert!(item.get("title").is_some(), "Post should have title");
                        assert!(item.get("views").is_some(), "Post should have views");
                        assert!(
                            item.get("citations").is_none(),
                            "Post should not have citations"
                        );
                        seen_post = true;
                    }
                    "Article" => {
                        assert!(item.get("title").is_some(), "Article should have title");
                        assert!(
                            item.get("citations").is_some(),
                            "Article should have citations"
                        );
                        assert!(item.get("views").is_none(), "Article should not have views");
                        seen_article = true
                    }
                    _ => unreachable!(),
                }
            }
        }

        assert!(seen_post, "should have seen Post at least once in 100 runs");
        assert!(
            seen_article,
            "should have seen Article at least once in 100 runs"
        );
    }

    #[test]
    fn interface_typename_picks_implementer() {
        let schema = Schema::parse_and_validate(INTERFACE_SCHEMA, "schema.graphql").unwrap();
        // `title` is on the interface and is requested unconditionally; the inline
        // fragments contribute fields specific to each implementer.
        let query = r#"
            query {
                user(id: "1") {
                    content {
                        __typename
                        title
                        ... on Post { views }
                        ... on Article { citations }
                    }
                }
            }
        "#;
        let doc = ExecutableDocument::parse_and_validate(&schema, query, "query.graphql").unwrap();

        let mut seen_post = false;
        let mut seen_article = false;

        for _ in 0..100 {
            let mut rng = RandProvider(rand::rng());
            let response = ResponseBuilder::new(&mut rng, &doc, &schema)
                .with_null_ratio(0, 1)
                .with_min_list_size(1)
                .build()
                .unwrap();

            let content = response
                .get("data")
                .unwrap()
                .get("user")
                .unwrap()
                .get("content")
                .unwrap()
                .as_array()
                .unwrap();

            for item in content {
                let typename = item.get("__typename").unwrap().as_str().unwrap();
                assert_ne!(
                    typename, "Content",
                    "__typename must not be the interface name"
                );
                assert!(
                    typename == "Post" || typename == "Article",
                    "unexpected __typename: {typename}"
                );
                // `title` is selected at the interface level, so it should be present
                // regardless of which concrete type was chosen.
                assert!(
                    item.get("title").is_some(),
                    "title should always be present"
                );
                match typename {
                    "Post" => {
                        assert!(item.get("views").is_some(), "Post should have views");
                        assert!(
                            item.get("citations").is_none(),
                            "Post should not have citations"
                        );
                        seen_post = true;
                    }
                    "Article" => {
                        assert!(
                            item.get("citations").is_some(),
                            "Article should have citations"
                        );
                        assert!(item.get("views").is_none(), "Article should not have views");
                        seen_article = true;
                    }
                    _ => unreachable!(),
                }
            }
        }

        assert!(seen_post, "should have seen Post at least once in 100 runs");
        assert!(
            seen_article,
            "should have seen Article at least once in 100 runs"
        );
    }

    #[test]
    fn custom_object_override() {
        let schema_with_service = r#"
            type Query {
                _service: _Service!
            }
            type _Service {
                sdl: String!
            }
        "#;
        let schema = Schema::parse_and_validate(schema_with_service, "schema.graphql").unwrap();
        let query = "query { _service { sdl } }";
        let doc = ExecutableDocument::parse_and_validate(&schema, query, "query.graphql").unwrap();

        struct SDLGenerator {
            sdl: String,
        }

        impl<R: RandomProvider> Generator<R> for SDLGenerator {
            fn generate(
                &mut self,
                _rng: &mut R,
                _generators: &mut Generators<R>,
                fields: &IndexMap<String, Vec<Node<Field>>>,
            ) -> Result<Value, ResponseError> {
                let mut service_obj = Map::new();
                for (key, group) in fields {
                    if group[0].name == "sdl" {
                        service_obj.insert(key.clone(), Value::String(self.sdl.clone().into()));
                    }
                }
                Ok(Value::Object(service_obj))
            }
        }

        let custom_sdl = "type Query { hello: String }";
        let mut rng = RandProvider(rand::rng());
        let response = ResponseBuilder::new(&mut rng, &doc, &schema)
            .with_generator(
                Name::new_unchecked("_Service"),
                SDLGenerator {
                    sdl: custom_sdl.to_owned(),
                },
            )
            .build()
            .unwrap();

        let sdl = response
            .get("data")
            .unwrap()
            .get("_service")
            .unwrap()
            .get("sdl")
            .unwrap()
            .as_str()
            .unwrap();

        assert_eq!(sdl, custom_sdl);
    }

    #[test]
    fn custom_scalar_override() {
        let schema_sdl = r#"
            scalar UUID
            type Query {
                id: UUID!
            }
        "#;
        let schema = Schema::parse_and_validate(schema_sdl, "schema.graphql").unwrap();
        let query = "query { id }";
        let doc = ExecutableDocument::parse_and_validate(&schema, query, "query.graphql").unwrap();

        struct ConstantGenerator(&'static str);

        impl<R: RandomProvider> Generator<R> for ConstantGenerator {
            fn generate(
                &mut self,
                _rng: &mut R,
                _generators: &mut Generators<R>,
                _fields: &IndexMap<String, Vec<Node<Field>>>,
            ) -> Result<Value, ResponseError> {
                Ok(Value::String(self.0.into()))
            }
        }

        let mut rng = RandProvider(rand::rng());
        let response = ResponseBuilder::new(&mut rng, &doc, &schema)
            .with_generator(
                Name::new_unchecked("UUID"),
                ConstantGenerator("00000000-0000-0000-0000-000000000000"),
            )
            .build()
            .unwrap();

        let id = response
            .get("data")
            .unwrap()
            .get("id")
            .unwrap()
            .as_str()
            .unwrap();
        assert_eq!(id, "00000000-0000-0000-0000-000000000000");
    }

    #[test]
    fn object_generator_delegates_to_scalar_generators() {
        // An ObjectGenerator that doesn't want to hand-roll every leaf field can
        // delegate to the builder's registered scalar generators. Here we register a
        // custom `ID` generator and assert the object generator reuses it.
        let schema = Schema::parse_and_validate(SIMPLE_SCHEMA, "schema.graphql").unwrap();
        let query = r#"query { user(id: "1") { id name } }"#;
        let doc = ExecutableDocument::parse_and_validate(&schema, query, "query.graphql").unwrap();

        struct UserGenerator;
        impl<R: RandomProvider> Generator<R> for UserGenerator {
            fn generate(
                &mut self,
                rng: &mut R,
                generators: &mut Generators<R>,
                fields: &IndexMap<String, Vec<Node<Field>>>,
            ) -> Result<Value, ResponseError> {
                let mut obj = Map::new();
                for (key, group) in fields {
                    let scalar_name = group[0].ty().inner_named_type();
                    obj.insert(key.clone(), generators.generate_scalar(scalar_name, rng)?);
                }
                Ok(Value::Object(obj))
            }
        }

        struct ConstantGenerator(&'static str);
        impl<R: RandomProvider> Generator<R> for ConstantGenerator {
            fn generate(
                &mut self,
                _rng: &mut R,
                _generators: &mut Generators<R>,
                _fields: &IndexMap<String, Vec<Node<Field>>>,
            ) -> Result<Value, ResponseError> {
                Ok(Value::String(self.0.into()))
            }
        }

        let mut rng = RandProvider(rand::rng());
        let response = ResponseBuilder::new(&mut rng, &doc, &schema)
            .with_generator(Name::new_unchecked("ID"), ConstantGenerator("user-id"))
            .with_generator(Name::new_unchecked("User"), UserGenerator)
            .build()
            .unwrap();

        let user = response.get("data").unwrap().get("user").unwrap();
        assert_eq!(user.get("id").unwrap().as_str().unwrap(), "user-id");
        // `name` is a String — no custom scalar registered, so it falls back to the
        // default alphanumeric generator (length 1–10).
        let name = user.get("name").unwrap().as_str().unwrap();
        assert!((1..=10).contains(&name.len()));
        assert!(name.chars().all(|c| c.is_ascii_alphanumeric()));
    }
}