cynic-codegen 3.13.2

Procedural macro code generation for cynic - a code first GraphQL client for Rust
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
use proc_macro2::Span;
use syn::spanned::Spanned;

use {
    super::parsing::{RustType, parse_rust_type},
    crate::schema::types::{InputType, InputValue, OutputType, TypeRef},
};

#[derive(Debug, PartialEq, Eq)]
pub enum CheckMode {
    OutputTypes,
    Flattening,
    Recursing,
    Spreading,
    Skippable,
    Defaulted,
}

pub fn check_types_are_compatible(
    gql_type: &TypeRef<'_, OutputType<'_>>,
    rust_type: &syn::Type,
    mode: CheckMode,
) -> Result<(), syn::Error> {
    let parsed_rust_type = parse_rust_type(rust_type);
    match mode {
        CheckMode::Flattening => output_type_check(gql_type, &parsed_rust_type, true)?,
        CheckMode::OutputTypes => output_type_check(gql_type, &parsed_rust_type, false)?,
        CheckMode::Recursing => recursing_check(gql_type, &parsed_rust_type)?,
        CheckMode::Skippable => {
            if !outer_type_is_option(rust_type) {
                return Err(TypeValidationError::SkippableFieldWithoutError {
                    provided_type: rust_type.to_string(),
                    span: rust_type.span(),
                }
                .into());
            }
            output_type_check(gql_type, &parsed_rust_type, false)?;
        }
        CheckMode::Defaulted => {
            let gql_type = match gql_type.clone() {
                TypeRef::Nullable(inner_ty) => *inner_ty,
                ty => ty,
            };
            output_type_check(&gql_type, &parsed_rust_type, false)?;
        }
        CheckMode::Spreading => {
            panic!("check_types_are_compatible shouldn't be called with CheckMode::Spreading")
        }
    }

    Ok(())
}

pub fn check_input_types_are_compatible(
    gql_type: &InputValue<'_>,
    rust_type: &syn::Type,
) -> Result<(), syn::Error> {
    let parsed_type = parse_rust_type(rust_type);

    input_type_check(&gql_type.value_type, gql_type.has_default, &parsed_type)?;

    Ok(())
}

pub fn check_spread_type(rust_type: &syn::Type) -> Result<(), syn::Error> {
    fn inner_fn(rust_type: &RustType<'_>) -> Result<(), syn::Error> {
        match rust_type {
            RustType::Ref { inner, .. } => {
                // Box is a transparent container for the purposes of checking compatibility
                // so just recurse
                inner_fn(inner.as_ref())
            }
            RustType::Optional { .. } => Err(TypeValidationError::SpreadOnOption {
                span: rust_type.span(),
            }
            .into()),
            RustType::List { .. } => Err(TypeValidationError::SpreadOnVec {
                span: rust_type.span(),
            }
            .into()),
            RustType::SimpleType { .. } => {
                // No way to tell if the given type is actually compatible,
                // but the rust compiler should help us with that.
                Ok(())
            }
        }
    }

    inner_fn(&parse_rust_type(rust_type))
}

/// Checks if the type is `Option`, `&Option`, `Box<Option>` etc.
pub fn outer_type_is_option(rust_type: &syn::Type) -> bool {
    fn inner_fn(rust_type: &RustType<'_>) -> bool {
        match rust_type {
            RustType::Optional { .. } => true,
            RustType::List { .. } => false,
            RustType::Ref { inner, .. } => inner_fn(inner.as_ref()),
            RustType::SimpleType { .. } => false,
        }
    }

    inner_fn(&parse_rust_type(rust_type))
}

fn output_type_check<'a>(
    gql_type: &TypeRef<'a, OutputType<'a>>,
    rust_type: &RustType<'a>,
    flattening: bool,
) -> Result<(), TypeValidationError> {
    match (&gql_type, rust_type) {
        (_, RustType::Ref { inner, .. }) => {
            // Box is a transparent container for the purposes of checking compatibility
            // so just recurse
            output_type_check(gql_type, inner.as_ref(), flattening)
        }
        (TypeRef::Nullable(inner_gql), RustType::Optional { inner, .. }) => {
            output_type_check(inner_gql, inner.as_ref(), flattening)
        }
        (TypeRef::Nullable(inner_gql), _) if flattening => {
            // If we're flattening then we should still check the inner types line up...
            output_type_check(inner_gql, rust_type, flattening)
        }
        (TypeRef::Nullable(_), _) => Err(TypeValidationError::FieldIsOptional {
            provided_type: rust_type.to_syn().to_string(),

            span: rust_type.span(),
        }),
        (gql_type, RustType::Optional { inner, .. }) => {
            // It should be fine for an output field to be `Option` if the schema
            // type isn't nullable.  it's pointless, but won't crash so
            // we just need to check the inner types
            output_type_check(gql_type, inner.as_ref(), flattening)
        }
        (TypeRef::List(item_type), RustType::List { inner, .. }) => {
            output_type_check(item_type.as_ref(), inner.as_ref(), flattening)
        }
        (TypeRef::List(_), _) => {
            // If the server is going to return a list we can't not have a Vec here...
            Err(TypeValidationError::FieldIsList {
                provided_type: rust_type.to_syn().to_string(),
                span: rust_type.span(),
            })
        }
        (_, RustType::List { inner, .. }) => Err(TypeValidationError::FieldIsNotList {
            provided_type: inner.to_syn().to_string(),
            span: rust_type.span(),
        }),
        (TypeRef::Named(_, _), RustType::SimpleType { .. }) => Ok(()),
    }
}

fn input_type_check<'a>(
    gql_type: &TypeRef<'a, InputType<'a>>,
    has_default: bool,
    rust_type: &RustType<'_>,
) -> Result<(), TypeValidationError> {
    match (&gql_type, rust_type) {
        (gql_type, RustType::Ref { inner, .. }) => {
            // Box is a transparent container for the purposes of checking compatibility
            // so just recurse
            input_type_check(gql_type, has_default, inner.as_ref())
        }
        (TypeRef::Nullable(inner_gql), RustType::Optional { inner, .. }) => {
            input_type_check(inner_gql, false, inner.as_ref())
        }
        (TypeRef::Nullable(inner_gql), _) => {
            // For input types its fine if a field isn't actually optional.
            // We just need to check that the inner types line up.
            input_type_check(inner_gql, false, rust_type)
        }
        (_, RustType::Optional { inner, .. }) if has_default => {
            // If an input type is required but has a default then
            // it's ok for it to be wrapped in option.
            input_type_check(gql_type, false, inner)
        }
        (_, RustType::Optional { inner, .. }) => Err(TypeValidationError::FieldIsRequired {
            provided_type: inner.to_syn().to_string(),
            span: rust_type.span(),
        }),
        (TypeRef::List(item_type), RustType::List { inner, .. }) => {
            input_type_check(item_type.as_ref(), false, inner)
        }
        (TypeRef::List(item_type), _) => {
            // For input types its fine to provide a single item instead of a list.
            // We just need to check that the inner types line up.
            input_type_check(item_type, false, rust_type)
        }
        (_, RustType::List { inner, .. }) => Err(TypeValidationError::FieldIsNotList {
            provided_type: inner.to_syn().to_string(),
            span: rust_type.span(),
        }),
        (TypeRef::Named(_, _), RustType::SimpleType { .. }) => Ok(()),
    }
}

fn recursing_check(
    gql_type: &TypeRef<'_, OutputType<'_>>,
    rust_type: &RustType<'_>,
) -> Result<(), TypeValidationError> {
    if let TypeRef::Nullable(_) = gql_type {
        // If the field is nullable then we just defer to the normal checks.
        return output_type_check(gql_type, rust_type, false);
    };

    if let RustType::Optional { inner, .. } = rust_type {
        output_type_check(gql_type, inner.as_ref(), false)
    } else {
        Err(TypeValidationError::RecursiveFieldWithoutOption {
            provided_type: rust_type.to_syn().to_string(),
            span: rust_type.span(),
        })
    }
}

#[derive(Debug)]
enum TypeValidationError {
    FieldIsOptional { provided_type: String, span: Span },
    FieldIsRequired { provided_type: String, span: Span },
    FieldIsList { provided_type: String, span: Span },
    FieldIsNotList { provided_type: String, span: Span },
    RecursiveFieldWithoutOption { provided_type: String, span: Span },
    SpreadOnOption { span: Span },
    SpreadOnVec { span: Span },
    SkippableFieldWithoutError { provided_type: String, span: Span },
}

impl From<TypeValidationError> for syn::Error {
    fn from(err: TypeValidationError) -> Self {
        let span = err.span();
        let message = match err {
            TypeValidationError::FieldIsOptional { provided_type, .. } => format!(
                "This field is nullable but you're not wrapping the type in Option.  Did you mean Option<{}>",
                provided_type
            ),
            TypeValidationError::FieldIsRequired { provided_type, .. } => format!(
                "This field is not nullable but you're wrapping the type in Option.  Did you mean {}",
                provided_type
            ),
            TypeValidationError::FieldIsList { provided_type, .. } => {
                format!(
                    "This field is a list but you're not wrapping the type in Vec.  Did you mean Vec<{}>",
                    provided_type
                )
            }
            TypeValidationError::FieldIsNotList { provided_type, .. } => {
                format!(
                    "This field is not a list but you're wrapping the type in Vec.  Did you mean {}",
                    provided_type
                )
            }
            TypeValidationError::RecursiveFieldWithoutOption { provided_type, .. } => {
                format!(
                    "Recursive types must be wrapped in Option.  Did you mean Option<{}>",
                    provided_type
                )
            }
            TypeValidationError::SpreadOnOption { .. } => {
                "You can't spread on an optional field".to_string()
            }
            TypeValidationError::SpreadOnVec { .. } => {
                "You can't spread on a list field".to_string()
            }
            TypeValidationError::SkippableFieldWithoutError { provided_type, .. } => format!(
                "This field has @skip or @include on it so it must be optional.  Did you mean Option<{provided_type}>"
            ),
        };

        syn::Error::new(span, message)
    }
}

impl From<TypeValidationError> for crate::Errors {
    fn from(err: TypeValidationError) -> Self {
        crate::Errors::from(syn::Error::from(err))
    }
}

impl TypeValidationError {
    fn span(&self) -> Span {
        match self {
            TypeValidationError::FieldIsOptional { span, .. } => *span,
            TypeValidationError::FieldIsRequired { span, .. } => *span,
            TypeValidationError::FieldIsList { span, .. } => *span,
            TypeValidationError::FieldIsNotList { span, .. } => *span,
            TypeValidationError::RecursiveFieldWithoutOption { span, .. } => *span,
            TypeValidationError::SpreadOnOption { span } => *span,
            TypeValidationError::SpreadOnVec { span } => *span,
            TypeValidationError::SkippableFieldWithoutError { span, .. } => *span,
        }
    }
}

trait SynTypeExt {
    fn to_string(&self) -> String;
}

impl SynTypeExt for syn::Type {
    fn to_string(&self) -> String {
        quote::quote! { #self }.to_string().replace(' ', "")
    }
}

#[cfg(test)]
mod tests {
    use std::marker::PhantomData;

    use {
        super::*,
        crate::schema::types::{InputType, OutputType},
    };

    use {assert_matches::assert_matches, quote::quote, rstest::rstest, syn::parse_quote};

    type OutputTypeRef<'a> = super::TypeRef<'a, OutputType<'a>>;
    type InputTypeRef<'a> = super::TypeRef<'a, InputType<'a>>;

    fn call_output_type_check(
        gql_type: &OutputTypeRef<'_>,
        rust_type: &syn::Type,
        flattening: bool,
    ) -> Result<(), TypeValidationError> {
        output_type_check(gql_type, &parse_rust_type(rust_type), flattening)
    }

    #[test]
    fn test_output_type_check() {
        let required_field = TypeRef::Named("test".into(), PhantomData);
        let optional_field = TypeRef::Nullable(Box::new(required_field.clone()));

        assert_matches!(
            call_output_type_check(
                &required_field,
                &syn::parse2(quote! { i32 }).unwrap(),
                false
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &required_field,
                &syn::parse2(quote! { DateTime<Utc> }).unwrap(),
                false
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &optional_field,
                &syn::parse2(quote! { Option<i32> }).unwrap(),
                false
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &optional_field,
                &syn::parse2(quote! { i32 }).unwrap(),
                false
            ),
            Err(TypeValidationError::FieldIsOptional {
                provided_type,
                ..
            }) => {
                assert_eq!(provided_type, "i32")
            }
        );
        assert_matches!(
            call_output_type_check(
                &required_field,
                &syn::parse2(quote! { Option<i32> }).unwrap(),
                false
            ),
            Ok(())
        );
    }

    #[test]
    fn test_output_type_list_validation() {
        let named = TypeRef::Named("test".into(), PhantomData);
        let list = TypeRef::List(Box::new(named.clone()));
        let optional_list = TypeRef::Nullable(Box::new(TypeRef::List(Box::new(named.clone()))));
        let option_list_option = TypeRef::Nullable(Box::new(TypeRef::List(Box::new(
            TypeRef::Nullable(Box::new(named.clone())),
        ))));

        assert_matches!(
            call_output_type_check(&list, &syn::parse2(quote! { Vec<i32> }).unwrap(), false),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &optional_list,
                &syn::parse2(quote! { Option<Vec<i32>> }).unwrap(),
                false
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &option_list_option,
                &syn::parse2(quote! { Option<Vec<Option<i32>>> }).unwrap(),
                false
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &list,
                &syn::parse2(quote! { Vec<DateTime<Utc>> }).unwrap(),
                false
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &optional_list,
                &syn::parse2(quote! { Option<Vec<DateTime<Utc>>> }).unwrap(),
                false
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &option_list_option,
                &syn::parse2(quote! { Option<Vec<Option<DateTime<Utc>>>> }).unwrap(),
                false
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &list,
                &syn::parse2(quote! { Option<Vec<i32>> }).unwrap(),
                false
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(&list, &syn::parse2(quote! { i32 }).unwrap(), false),
            Err(TypeValidationError::FieldIsList { provided_type, .. }) => {
                assert_eq!(provided_type, "i32")
            }
        );
        assert_matches!(
            call_output_type_check(
                &optional_list,
                &syn::parse2(quote! { Vec<i32> }).unwrap(),
                false
            ),
            Err(TypeValidationError::FieldIsOptional { provided_type, .. }) => {
                assert_eq!(provided_type, "Vec<i32>")
            }
        );
        assert_matches!(
            call_output_type_check(
                &option_list_option,
                &syn::parse2(quote! { Option<Vec<i32>> }).unwrap(),
                false
            ),
            Err(TypeValidationError::FieldIsOptional { provided_type, .. }) => {
                assert_eq!(provided_type, "i32")
            }
        );
        assert_matches!(
            call_output_type_check(
                &option_list_option,
                &syn::parse2(quote! { Option<DateTime<Vec<Option<i32>>>> }).unwrap(),
                false
            ),
            Err(_)
        );
    }

    #[test]
    fn test_validation_when_flattening() {
        let named = TypeRef::Named("test".into(), PhantomData);
        let list = TypeRef::List(Box::new(named.clone()));
        let optional_list = TypeRef::Nullable(Box::new(TypeRef::List(Box::new(named.clone()))));
        let option_list_option = TypeRef::Nullable(Box::new(TypeRef::List(Box::new(
            TypeRef::Nullable(Box::new(named.clone())),
        ))));

        assert_matches!(
            call_output_type_check(
                &option_list_option,
                &syn::parse2(quote! { Vec<i32> }).unwrap(),
                true
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &option_list_option,
                &syn::parse2(quote! { Option<Vec<i32>> }).unwrap(),
                true
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &optional_list,
                &syn::parse2(quote! { Vec<i32> }).unwrap(),
                true
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &list,
                &syn::parse2(quote! { Vec<Option<i32>> }).unwrap(),
                true
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(
                &list,
                &syn::parse2(quote! { Option<Vec<i32>> }).unwrap(),
                true
            ),
            Ok(())
        );
        assert_matches!(
            call_output_type_check(&list, &syn::parse2(quote! { Option<i32> }).unwrap(), true),
            Err(TypeValidationError::FieldIsList { provided_type, .. }) => {
                assert_eq!(provided_type, "i32")
            }
        );
    }

    fn call_input_type_check(
        gql_type: &InputTypeRef<'_>,
        has_default: bool,
        rust_type: &syn::Type,
    ) -> Result<(), TypeValidationError> {
        input_type_check(gql_type, has_default, &parse_rust_type(rust_type))
    }

    #[test]
    fn test_input_type_validation() {
        let required_field = TypeRef::Named("test".into(), PhantomData);
        let optional_field = TypeRef::Nullable(Box::new(required_field.clone()));

        assert_matches!(
            call_input_type_check(
                &required_field,
                false,
                &syn::parse2(quote! { i32 }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &required_field,
                false,
                &syn::parse2(quote! { DateTime<Utc> }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &optional_field,
                false,
                &syn::parse2(quote! { Option<i32> }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &optional_field,
                false,
                &syn::parse2(quote! { i32 }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &required_field,
                false,
                &syn::parse2(quote! { Option<i32> }).unwrap(),
            ),
            Err(TypeValidationError::FieldIsRequired {provided_type, ..}) => {
                assert_eq!(provided_type, "i32")
            }
        );
    }

    #[test]
    fn test_input_type_validation_with_default() {
        let required_field = TypeRef::Named("test".into(), PhantomData);
        let optional_field = TypeRef::Nullable(Box::new(required_field.clone()));

        assert_matches!(
            call_input_type_check(&required_field, true, &syn::parse2(quote! { i32 }).unwrap(),),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &required_field,
                true,
                &syn::parse2(quote! { DateTime<Utc> }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &optional_field,
                true,
                &syn::parse2(quote! { Option<i32> }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(&optional_field, true, &syn::parse2(quote! { i32 }).unwrap(),),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &required_field,
                true,
                &syn::parse2(quote! { Option<i32> }).unwrap(),
            ),
            Ok(())
        );
    }

    #[test]
    fn test_input_type_list_validation() {
        let named = TypeRef::Named("test".into(), PhantomData);
        let list = TypeRef::List(Box::new(named.clone()));
        let optional_list = TypeRef::Nullable(Box::new(TypeRef::List(Box::new(named.clone()))));
        let option_list_option = TypeRef::Nullable(Box::new(TypeRef::List(Box::new(
            TypeRef::Nullable(Box::new(named.clone())),
        ))));

        assert_matches!(
            call_input_type_check(&list, false, &syn::parse2(quote! { Vec<i32> }).unwrap(),),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &optional_list,
                false,
                &syn::parse2(quote! { Option<Vec<i32>> }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &option_list_option,
                false,
                &syn::parse2(quote! { Option<Vec<Option<i32>>> }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &list,
                false,
                &syn::parse2(quote! { Vec<DateTime<Utc>> }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &optional_list,
                false,
                &syn::parse2(quote! { Option<Vec<DateTime<Utc>>> }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &option_list_option,
                false,
                &syn::parse2(quote! { Option<Vec<Option<DateTime<Utc>>>> }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(&list, false, &syn::parse2(quote! { i32 }).unwrap(),),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(&optional_list, false, &syn::parse2(quote! { i32 }).unwrap(),),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &optional_list,
                false,
                &syn::parse2(quote! { Vec<i32> }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &option_list_option,
                false,
                &syn::parse2(quote! { Option<Vec<i32>> }).unwrap(),
            ),
            Ok(())
        );
        assert_matches!(
            call_input_type_check(
                &option_list_option,
                false,
                &syn::parse2(quote! { Option<Vec<Inner<'a>>> }).unwrap(),
            ),
            Ok(())
        );
    }

    #[rstest(graphql_field, rust_field,
        case::required_t(
            TypeRef::Named("T".into(), PhantomData),
            parse_quote! { Option<Box<T>> }
        ),

        case::optional_t(
            TypeRef::Nullable(Box::new(TypeRef::Named("T".into(), PhantomData))),
            parse_quote! { Option<T> }
        ),

        case::option_vec_required_t(
            TypeRef::Nullable(Box::new(
                TypeRef::List(Box::new(TypeRef::Named("T".into(), PhantomData)))
            )),
            parse_quote! { Option<Vec<T>> }
        ),

        case::required_vec_required_t(
            TypeRef::List(Box::new(TypeRef::Named("T".into(), PhantomData))),
            parse_quote! { Option<Vec<T>> }
        ),
    )]
    fn test_recurse_validation_ok(graphql_field: OutputTypeRef<'_>, rust_field: syn::Type) {
        assert_matches!(
            check_types_are_compatible(&graphql_field, &rust_field, CheckMode::Recursing),
            Ok(())
        );
    }

    #[rstest(graphql_field, rust_field,
        case::required_t_box(
            TypeRef::Named("T".into(), PhantomData),
            parse_quote! { Box<T> }
        ),
        case::required_t_standalone(
            TypeRef::Named("T".into(), PhantomData),
            parse_quote! { T }
        ),

        case::optional_t_standalone(
            TypeRef::Nullable(Box::new(TypeRef::Named("T".into(), PhantomData))),
            parse_quote! { T }
        ),
        case::optional_t_box(
            TypeRef::Nullable(Box::new(TypeRef::Named("T".into(), PhantomData))),
            parse_quote! { Box<T> }
        ),

        case::option_vec_required_t(
            TypeRef::Nullable(Box::new(
                TypeRef::List(Box::new(TypeRef::Named("T".into(), PhantomData)))
            )),
            parse_quote! { Vec<T> }
        ),
        case::option_vec_required_t(
            TypeRef::Nullable(Box::new(
                TypeRef::List(Box::new(TypeRef::Named("T".into(), PhantomData)))
            )),
            parse_quote! { Vec<Option<T>> }
        ),

        case::required_vec_required_t(
            TypeRef::List(Box::new(TypeRef::Named("T".into(), PhantomData))),
            parse_quote! { Vec<T> }
        ),
        case::required_vec_required_t_no_vec(
            TypeRef::List(Box::new(TypeRef::Named("T".into(), PhantomData))),
            parse_quote! { T }
        ),

        case::required_vec_optional_t_no_vec(
            TypeRef::List(Box::new(
                TypeRef::Nullable(Box::new(TypeRef::Named("T".into(), PhantomData)))
            )),
            parse_quote! { Option<T> }
        ),
        case::required_vec_optional_t_wrong_nesting(
            TypeRef::List(Box::new(
                TypeRef::Nullable(Box::new(TypeRef::Named("T".into(), PhantomData)))
            )),
            parse_quote! { Option<Vec<T>> }
        ),
    )]
    fn test_recurse_validation_fail(graphql_field: OutputTypeRef<'_>, rust_field: syn::Type) {
        assert_matches!(
            check_types_are_compatible(&graphql_field, &rust_field, CheckMode::Recursing),
            Err(_)
        );
    }
}