juniper 0.17.0

GraphQL server library.
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
//! Types used to describe a `GraphQL` schema

use std::borrow::ToOwned;

use arcstr::ArcStr;
use derive_more::with_trait::Debug;

use crate::{
    FieldError, IntoFieldError,
    ast::{FromInputValue, InputValue, Type},
    parser::{ParseError, ScalarToken},
    schema::model::SchemaType,
    types::base::TypeKind,
    value::{DefaultScalarValue, ParseScalarValue},
};

/// Whether an item is deprecated, with context.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum DeprecationStatus {
    /// The field/variant is not deprecated.
    Current,

    /// The field/variant is deprecated, with an optional reason
    Deprecated(Option<ArcStr>),
}

impl DeprecationStatus {
    /// If this deprecation status indicates the item is deprecated.
    pub fn is_deprecated(&self) -> bool {
        match self {
            Self::Current => false,
            Self::Deprecated(_) => true,
        }
    }

    /// An optional reason for the deprecation, or none if `Current`.
    pub fn reason(&self) -> Option<&ArcStr> {
        match self {
            Self::Current => None,
            Self::Deprecated(rsn) => rsn.as_ref(),
        }
    }
}

/// Scalar type metadata
#[derive(Debug)]
pub struct ScalarMeta<S> {
    #[doc(hidden)]
    pub name: ArcStr,
    #[doc(hidden)]
    pub description: Option<ArcStr>,
    #[doc(hidden)]
    pub specified_by_url: Option<ArcStr>,
    #[debug(ignore)]
    pub(crate) try_parse_fn: InputValueParseFn<S>,
    #[debug(ignore)]
    pub(crate) parse_fn: ScalarTokenParseFn<S>,
}

impl<S> ScalarMeta<S> {
    /// Builds a new [`ScalarMeta`] type with the specified `name`.
    pub fn new<T>(name: impl Into<ArcStr>) -> Self
    where
        T: FromInputValue<S> + ParseScalarValue<S>,
        T::Error: IntoFieldError<S>,
    {
        Self {
            name: name.into(),
            description: None,
            specified_by_url: None,
            try_parse_fn: try_parse_fn::<S, T>,
            parse_fn: <T as ParseScalarValue<S>>::from_str,
        }
    }

    /// Sets the `description` of this [`ScalarMeta`] type.
    ///
    /// Overwrites any previously set description.
    #[must_use]
    pub fn description(mut self, description: impl Into<ArcStr>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Sets the [specification URL][0] for this [`ScalarMeta`] type.
    ///
    /// Overwrites any previously set [specification URL][0].
    ///
    /// [0]: https://spec.graphql.org/October2021#sec--specifiedBy
    #[must_use]
    pub fn specified_by_url(mut self, url: impl Into<ArcStr>) -> Self {
        self.specified_by_url = Some(url.into());
        self
    }

    /// Wraps this [`ScalarMeta`] type into a generic [`MetaType`].
    pub fn into_meta(self) -> MetaType<S> {
        MetaType::Scalar(self)
    }
}

/// Shortcut for an [`InputValue`] parsing function.
pub type InputValueParseFn<S> = for<'b> fn(&'b InputValue<S>) -> Result<(), FieldError<S>>;

/// Shortcut for a [`ScalarToken`] parsing function.
pub type ScalarTokenParseFn<S> = for<'b> fn(ScalarToken<'b>) -> Result<S, ParseError>;

/// List type metadata
#[derive(Debug)]
pub struct ListMeta {
    #[doc(hidden)]
    pub of_type: Type,

    #[doc(hidden)]
    pub expected_size: Option<usize>,
}

impl ListMeta {
    /// Builds a new [`ListMeta`] type by wrapping the specified [`Type`].
    ///
    /// Specifying `expected_size` will be used to ensure that values of this type will always match
    /// it.
    pub fn new(of_type: Type, expected_size: Option<usize>) -> Self {
        Self {
            of_type,
            expected_size,
        }
    }

    /// Wraps this [`ListMeta`] type into a generic [`MetaType`].
    pub fn into_meta<S>(self) -> MetaType<S> {
        MetaType::List(self)
    }
}

/// Nullable type metadata
#[derive(Debug)]
pub struct NullableMeta {
    #[doc(hidden)]
    pub of_type: Type,
}

impl NullableMeta {
    /// Builds a new [`NullableMeta`] type by wrapping the specified [`Type`].
    pub fn new(of_type: Type) -> Self {
        Self { of_type }
    }

    /// Wraps this [`NullableMeta`] type into a generic [`MetaType`].
    pub fn into_meta<S>(self) -> MetaType<S> {
        MetaType::Nullable(self)
    }
}

/// Object type metadata
#[derive(Debug)]
pub struct ObjectMeta<S> {
    #[doc(hidden)]
    pub name: ArcStr,
    #[doc(hidden)]
    pub description: Option<ArcStr>,
    #[doc(hidden)]
    pub fields: Vec<Field<S>>,
    #[doc(hidden)]
    pub interface_names: Vec<ArcStr>,
}

impl<S> ObjectMeta<S> {
    /// Builds a new [`ObjectMeta`] type with the specified `name` and `fields`.
    pub fn new(name: impl Into<ArcStr>, fields: &[Field<S>]) -> Self
    where
        S: Clone,
    {
        Self {
            name: name.into(),
            description: None,
            fields: fields.to_vec(),
            interface_names: vec![],
        }
    }

    /// Sets the `description` of this [`ObjectMeta`] type.
    ///
    /// Overwrites any previously set description.
    #[must_use]
    pub fn description(mut self, description: impl Into<ArcStr>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Sets the `interfaces` this [`ObjectMeta`] type implements.
    ///
    /// Overwrites any previously set list of interfaces.
    #[must_use]
    pub fn interfaces(mut self, interfaces: &[Type]) -> Self {
        self.interface_names = interfaces
            .iter()
            .map(|t| t.innermost_name().into())
            .collect();
        self
    }

    /// Wraps this [`ObjectMeta`] type into a generic [`MetaType`].
    pub fn into_meta(self) -> MetaType<S> {
        MetaType::Object(self)
    }
}

/// Enum type metadata
#[derive(Debug)]
pub struct EnumMeta<S> {
    #[doc(hidden)]
    pub name: ArcStr,
    #[doc(hidden)]
    pub description: Option<ArcStr>,
    #[doc(hidden)]
    pub values: Vec<EnumValue>,
    #[debug(ignore)]
    pub(crate) try_parse_fn: InputValueParseFn<S>,
}

impl<S> EnumMeta<S> {
    /// Builds a new [`EnumMeta`] type with the specified `name` and possible `values`.
    pub fn new<T>(name: impl Into<ArcStr>, values: &[EnumValue]) -> Self
    where
        T: FromInputValue<S>,
        T::Error: IntoFieldError<S>,
    {
        Self {
            name: name.into(),
            description: None,
            values: values.to_owned(),
            try_parse_fn: try_parse_fn::<S, T>,
        }
    }

    /// Sets the `description` of this [`EnumMeta`] type.
    ///
    /// Overwrites any previously set description.
    #[must_use]
    pub fn description(mut self, description: impl Into<ArcStr>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Wraps this [`EnumMeta`] type into a generic [`MetaType`].
    pub fn into_meta(self) -> MetaType<S> {
        MetaType::Enum(self)
    }
}

/// Interface type metadata
#[derive(Debug)]
pub struct InterfaceMeta<S> {
    #[doc(hidden)]
    pub name: ArcStr,
    #[doc(hidden)]
    pub description: Option<ArcStr>,
    #[doc(hidden)]
    pub fields: Vec<Field<S>>,
    #[doc(hidden)]
    pub interface_names: Vec<ArcStr>,
}

impl<S> InterfaceMeta<S> {
    /// Builds a new [`InterfaceMeta`] type with the specified `name` and `fields`.
    pub fn new(name: impl Into<ArcStr>, fields: &[Field<S>]) -> Self
    where
        S: Clone,
    {
        Self {
            name: name.into(),
            description: None,
            fields: fields.to_vec(),
            interface_names: Vec::new(),
        }
    }

    /// Sets the `description` of this [`InterfaceMeta`] type.
    ///
    /// Overwrites any previously set description.
    #[must_use]
    pub fn description(mut self, description: impl Into<ArcStr>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Sets the `interfaces` this [`InterfaceMeta`] interface implements.
    ///
    /// Overwrites any previously set list of interfaces.
    #[must_use]
    pub fn interfaces(mut self, interfaces: &[Type]) -> Self {
        self.interface_names = interfaces
            .iter()
            .map(|t| t.innermost_name().into())
            .collect();
        self
    }

    /// Wraps this [`InterfaceMeta`] type into a generic [`MetaType`].
    pub fn into_meta(self) -> MetaType<S> {
        MetaType::Interface(self)
    }
}

/// Union type metadata
#[derive(Debug)]
pub struct UnionMeta {
    #[doc(hidden)]
    pub name: ArcStr,
    #[doc(hidden)]
    pub description: Option<ArcStr>,
    #[doc(hidden)]
    pub of_type_names: Vec<ArcStr>,
}

impl UnionMeta {
    /// Builds a new [`UnionMeta`] type with the specified `name` and possible [`Type`]s.
    pub fn new(name: impl Into<ArcStr>, of_types: &[Type]) -> Self {
        Self {
            name: name.into(),
            description: None,
            of_type_names: of_types.iter().map(|t| t.innermost_name().into()).collect(),
        }
    }

    /// Sets the `description` of this [`UnionMeta`] type.
    ///
    /// Overwrites any previously set description.
    #[must_use]
    pub fn description(mut self, description: impl Into<ArcStr>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Wraps this [`UnionMeta`] type into a generic [`MetaType`].
    pub fn into_meta<S>(self) -> MetaType<S> {
        MetaType::Union(self)
    }
}

/// Input object metadata
#[derive(Debug)]
pub struct InputObjectMeta<S> {
    #[doc(hidden)]
    pub name: ArcStr,
    #[doc(hidden)]
    pub description: Option<ArcStr>,
    #[doc(hidden)]
    pub input_fields: Vec<Argument<S>>,
    #[debug(ignore)]
    pub(crate) try_parse_fn: InputValueParseFn<S>,
}

impl<S> InputObjectMeta<S> {
    /// Builds a new [`InputObjectMeta`] type with the specified `name` and `input_fields`.
    pub fn new<T>(name: impl Into<ArcStr>, input_fields: &[Argument<S>]) -> Self
    where
        T: FromInputValue<S>,
        T::Error: IntoFieldError<S>,
        S: Clone,
    {
        Self {
            name: name.into(),
            description: None,
            input_fields: input_fields.to_vec(),
            try_parse_fn: try_parse_fn::<S, T>,
        }
    }

    /// Sets the `description` of this [`InputObjectMeta`] type.
    ///
    /// Overwrites any previously set description.
    #[must_use]
    pub fn description(mut self, description: impl Into<ArcStr>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Wraps this [`InputObjectMeta`] type into a generic [`MetaType`].
    pub fn into_meta(self) -> MetaType<S> {
        MetaType::InputObject(self)
    }
}

/// A placeholder for not-yet-registered types
///
/// After a type's `meta` method has been called but before it has returned, a placeholder type
/// is inserted into a registry to indicate existence.
#[derive(Debug)]
pub struct PlaceholderMeta {
    #[doc(hidden)]
    pub of_type: Type,
}

/// Metadata for a field
#[derive(Debug, Clone)]
pub struct Field<S> {
    #[doc(hidden)]
    pub name: ArcStr,
    #[doc(hidden)]
    pub description: Option<ArcStr>,
    #[doc(hidden)]
    pub arguments: Option<Vec<Argument<S>>>,
    #[doc(hidden)]
    pub field_type: Type,
    #[doc(hidden)]
    pub deprecation_status: DeprecationStatus,
}

impl<S> Field<S> {
    /// Sets the `description` of this [`Field`].
    ///
    /// Overwrites any previously set description.
    #[must_use]
    pub fn description(mut self, description: impl Into<ArcStr>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Adds an `argument` to this [`Field`].
    ///
    /// Arguments are unordered and can't contain duplicates by name.
    #[must_use]
    pub fn argument(mut self, argument: Argument<S>) -> Self {
        match self.arguments {
            None => {
                self.arguments = Some(vec![argument]);
            }
            Some(ref mut args) => {
                args.push(argument);
            }
        };
        self
    }

    /// Indicates whether this [`Field`] is GraphQL built-in.
    #[must_use]
    pub fn is_builtin(&self) -> bool {
        // "used exclusively by GraphQL’s introspection system"
        self.name.starts_with("__")
    }

    /// Sets this [`Field`] as deprecated with an optional `reason`.
    ///
    /// Overwrites any previously set deprecation reason.
    #[must_use]
    pub fn deprecated(mut self, reason: Option<impl Into<ArcStr>>) -> Self {
        self.deprecation_status = DeprecationStatus::Deprecated(reason.map(Into::into));
        self
    }
}

/// Metadata for an argument to a field
#[derive(Debug, Clone)]
pub struct Argument<S> {
    #[doc(hidden)]
    pub name: ArcStr,
    #[doc(hidden)]
    pub description: Option<ArcStr>,
    #[doc(hidden)]
    pub arg_type: Type,
    #[doc(hidden)]
    pub default_value: Option<InputValue<S>>,
}

impl<S> Argument<S> {
    /// Builds a new [`Argument`] of the given [`Type`] with the given `name`.
    pub fn new(name: impl Into<ArcStr>, arg_type: Type) -> Self {
        Self {
            name: name.into(),
            description: None,
            arg_type,
            default_value: None,
        }
    }

    /// Sets the `description` of this [`Argument`].
    ///
    /// Overwrites any previously set description.
    #[must_use]
    pub fn description(mut self, description: impl Into<ArcStr>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Indicates whether this [`Argument`] is GraphQL built-in.
    #[must_use]
    pub fn is_builtin(&self) -> bool {
        // "used exclusively by GraphQL’s introspection system"
        self.name.starts_with("__")
    }

    /// Sets the default value of this [`Argument`].
    ///
    /// Overwrites any previously set default value.
    #[must_use]
    pub fn default_value(mut self, val: InputValue<S>) -> Self {
        self.default_value = Some(val);
        self
    }
}

/// Metadata for a single value in an enum
#[derive(Debug, Clone)]
pub struct EnumValue {
    /// The name of the enum value
    ///
    /// This is the string literal representation of the enum in responses.
    pub name: ArcStr,

    /// The optional description of the enum value.
    ///
    /// Note: this is not the description of the enum itself; it's the
    /// description of this enum _value_.
    pub description: Option<ArcStr>,

    /// Whether the field is deprecated or not, with an optional reason.
    pub deprecation_status: DeprecationStatus,
}

impl EnumValue {
    /// Constructs a new [`EnumValue`] with the provided `name`.
    pub fn new(name: impl Into<ArcStr>) -> Self {
        Self {
            name: name.into(),
            description: None,
            deprecation_status: DeprecationStatus::Current,
        }
    }

    /// Sets the `description` of this [`EnumValue`].
    ///
    /// Overwrites any previously set description.
    #[must_use]
    pub fn description(mut self, description: impl Into<ArcStr>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Sets this [`EnumValue`] as deprecated with an optional `reason`.
    ///
    /// Overwrites any previously set deprecation reason.
    #[must_use]
    pub fn deprecated(mut self, reason: Option<impl Into<ArcStr>>) -> Self {
        self.deprecation_status = DeprecationStatus::Deprecated(reason.map(Into::into));
        self
    }
}

/// Generic type metadata
#[derive(Debug)]
pub enum MetaType<S = DefaultScalarValue> {
    #[doc(hidden)]
    Scalar(ScalarMeta<S>),
    #[doc(hidden)]
    List(ListMeta),
    #[doc(hidden)]
    Nullable(NullableMeta),
    #[doc(hidden)]
    Object(ObjectMeta<S>),
    #[doc(hidden)]
    Enum(EnumMeta<S>),
    #[doc(hidden)]
    Interface(InterfaceMeta<S>),
    #[doc(hidden)]
    Union(UnionMeta),
    #[doc(hidden)]
    InputObject(InputObjectMeta<S>),
    #[doc(hidden)]
    Placeholder(PlaceholderMeta),
}

impl<S> MetaType<S> {
    /// Returns the name of the represented type, if applicable.
    ///
    /// [Lists][`ListMeta`], [`null`ables][`NullableMeta`] and [placeholders][`PlaceholderMeta`]
    /// don't have a name.
    pub fn name(&self) -> Option<&ArcStr> {
        match self {
            Self::Enum(EnumMeta { name, .. })
            | Self::InputObject(InputObjectMeta { name, .. })
            | Self::Interface(InterfaceMeta { name, .. })
            | Self::Object(ObjectMeta { name, .. })
            | Self::Scalar(ScalarMeta { name, .. })
            | Self::Union(UnionMeta { name, .. }) => Some(name),
            Self::List(..) | Self::Nullable(..) | Self::Placeholder(..) => None,
        }
    }

    /// Returns the description of the represented type, if applicable.
    ///
    /// [Lists][`ListMeta`], [`null`ables][`NullableMeta`] and [placeholders][`PlaceholderMeta`]
    /// don't have a description.
    pub fn description(&self) -> Option<&ArcStr> {
        match self {
            Self::Enum(EnumMeta { description, .. })
            | Self::InputObject(InputObjectMeta { description, .. })
            | Self::Interface(InterfaceMeta { description, .. })
            | Self::Object(ObjectMeta { description, .. })
            | Self::Scalar(ScalarMeta { description, .. })
            | Self::Union(UnionMeta { description, .. }) => description.as_ref(),
            Self::List(..) | Self::Nullable(..) | Self::Placeholder(..) => None,
        }
    }

    /// Returns the [specification URL][0] of the represented type, if applicable.
    ///
    /// Only custom GraphQL scalars can have a [specification URL][0].
    ///
    /// [0]: https://spec.graphql.org/October2021#sec--specifiedBy
    pub fn specified_by_url(&self) -> Option<&ArcStr> {
        match self {
            Self::Scalar(ScalarMeta {
                specified_by_url, ..
            }) => specified_by_url.as_ref(),
            Self::Enum(..)
            | Self::InputObject(..)
            | Self::Interface(..)
            | Self::List(..)
            | Self::Nullable(..)
            | Self::Object(..)
            | Self::Placeholder(..)
            | Self::Union(..) => None,
        }
    }

    /// Construct a [`TypeKind`] out of this [`MetaType`].
    ///
    /// # Panics
    ///
    /// If this is [`MetaType::Nullable`] or [``MetaType::Placeholder`].
    pub fn type_kind(&self) -> TypeKind {
        match self {
            Self::Scalar(..) => TypeKind::Scalar,
            Self::List(..) => TypeKind::List,
            Self::Nullable(..) => panic!("сan't take `type_kind` of `MetaType::Nullable`"),
            Self::Object(..) => TypeKind::Object,
            Self::Enum(..) => TypeKind::Enum,
            Self::Interface(..) => TypeKind::Interface,
            Self::Union(..) => TypeKind::Union,
            Self::InputObject(..) => TypeKind::InputObject,
            Self::Placeholder(..) => panic!("сan't take `type_kind` of `MetaType::Placeholder`"),
        }
    }

    /// Returns a [`Field`]'s metadata by its `name`.
    ///
    /// Only [objects][`ObjectMeta`] and [interfaces][`InterfaceMeta`] have fields.
    pub fn field_by_name(&self, name: &str) -> Option<&Field<S>> {
        match self {
            Self::Interface(InterfaceMeta { fields, .. })
            | Self::Object(ObjectMeta { fields, .. }) => fields.iter().find(|f| f.name == name),
            Self::Enum(..)
            | Self::InputObject(..)
            | Self::List(..)
            | Self::Nullable(..)
            | Self::Placeholder(..)
            | Self::Scalar(..)
            | Self::Union(..) => None,
        }
    }

    /// Returns an input field's metadata by its `name`.
    ///
    /// Only [input objects][`InputObjectMeta`] have input fields.
    pub fn input_field_by_name(&self, name: &str) -> Option<&Argument<S>> {
        match self {
            Self::InputObject(InputObjectMeta { input_fields, .. }) => {
                input_fields.iter().find(|f| f.name == name)
            }
            Self::Enum(..)
            | Self::Interface(..)
            | Self::List(..)
            | Self::Nullable(..)
            | Self::Object(..)
            | Self::Placeholder(..)
            | Self::Scalar(..)
            | Self::Union(..) => None,
        }
    }

    /// Construct a [`Type`] literal out of this [`MetaType`].
    pub fn as_type(&self) -> Type {
        match self {
            Self::Enum(EnumMeta { name, .. })
            | Self::InputObject(InputObjectMeta { name, .. })
            | Self::Interface(InterfaceMeta { name, .. })
            | Self::Object(ObjectMeta { name, .. })
            | Self::Scalar(ScalarMeta { name, .. })
            | Self::Union(UnionMeta { name, .. }) => Type::nullable(name.clone()).wrap_non_null(),
            Self::List(ListMeta {
                of_type,
                expected_size,
            }) => of_type.clone().wrap_list(*expected_size).wrap_non_null(),
            Self::Nullable(NullableMeta { of_type }) => of_type.clone().into_nullable(),
            Self::Placeholder(PlaceholderMeta { of_type }) => of_type.clone(),
        }
    }

    /// Returns the [`InputValueParseFn`] of the represented type, if applicable.
    ///
    /// Only [scalars][`ScalarMeta`], [enums][`EnumMeta`] and [input objects][`InputObjectMeta`]
    /// have an [`InputValueParseFn`].
    pub fn input_value_parse_fn(&self) -> Option<InputValueParseFn<S>> {
        match self {
            Self::Enum(EnumMeta { try_parse_fn, .. })
            | Self::InputObject(InputObjectMeta { try_parse_fn, .. })
            | Self::Scalar(ScalarMeta { try_parse_fn, .. }) => Some(*try_parse_fn),
            Self::Interface(..)
            | Self::List(..)
            | Self::Nullable(..)
            | Self::Object(..)
            | Self::Placeholder(..)
            | Self::Union(..) => None,
        }
    }

    /// Indicates whether the represented type is a composite one.
    ///
    /// [Objects][`ObjectMeta`], [interfaces][`InterfaceMeta`] and [unions][`UnionMeta`] are
    /// composite types.
    pub fn is_composite(&self) -> bool {
        matches!(
            self,
            Self::Interface(..) | Self::Object(..) | Self::Union(..)
        )
    }

    /// Indicates whether the represented type can occur in leaf positions of queries.
    ///
    /// Only [enums][`EnumMeta`] and [scalars][`ScalarMeta`] are leaf types.
    pub fn is_leaf(&self) -> bool {
        matches!(self, Self::Enum(..) | Self::Scalar(..))
    }

    /// Indicates whether the represented type is abstract.
    ///
    /// Only [interfaces][`InterfaceMeta`] and [unions][`UnionMeta`] are abstract types.
    pub fn is_abstract(&self) -> bool {
        matches!(self, Self::Interface(..) | Self::Union(..))
    }

    /// Indicates whether the represented type can be used in input positions (e.g. arguments or
    /// variables).
    ///
    /// Only [scalars][`ScalarMeta`], [enums][`EnumMeta`] and [input objects][`InputObjectMeta`] are
    /// input types.
    pub fn is_input(&self) -> bool {
        matches!(
            self,
            Self::Enum(..) | Self::InputObject(..) | Self::Scalar(..)
        )
    }

    /// Indicates whether the represented type is GraphQL built-in.
    pub fn is_builtin(&self) -> bool {
        if let Some(name) = self.name() {
            // "used exclusively by GraphQL’s introspection system"
            {
                name.starts_with("__") ||
            // https://spec.graphql.org/October2021#sec-Scalars
            name == "Boolean" || name == "String" || name == "Int" || name == "Float" || name == "ID" ||
            // Our custom empty markers
            name == "_EmptyMutation" || name == "_EmptySubscription"
            }
        } else {
            false
        }
    }

    pub(crate) fn fields<'s>(&self, schema: &'s SchemaType<S>) -> Option<Vec<&'s Field<S>>> {
        schema
            .lookup_type(&self.as_type())
            .and_then(|tpe| match tpe {
                Self::Interface(i) => Some(i.fields.iter().collect()),
                Self::Object(o) => Some(o.fields.iter().collect()),
                Self::Union(u) => Some(
                    u.of_type_names
                        .iter()
                        .filter_map(|n| schema.concrete_type_by_name(n))
                        .filter_map(|t| t.fields(schema))
                        .flatten()
                        .collect(),
                ),
                Self::Enum(..)
                | Self::InputObject(..)
                | Self::List(..)
                | Self::Nullable(..)
                | Self::Placeholder(..)
                | Self::Scalar(..) => None,
            })
    }
}

fn try_parse_fn<S, T>(v: &InputValue<S>) -> Result<(), FieldError<S>>
where
    T: FromInputValue<S>,
    T::Error: IntoFieldError<S>,
{
    T::from_input_value(v)
        .map_err(T::Error::into_field_error)
        .map(drop)
}