graphql-query 1.0.1

Stupendously fast and easy GraphQL Query Language handling.
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
use crate::ast::{ASTContext, DefaultIn, NamedType, OperationKind, Type};
use bumpalo::collections::Vec;
use bumpalo::Bump;
use hashbrown::hash_map::DefaultHashBuilder;
use hashbrown::{HashMap, HashSet};

/// Schema Definition
///
/// A schema is created from root types for each kind of operation and is then used against
/// AST documents for validation and execution. In this library the schema is never executable and
/// serves only for metadata and type information. It is hence a "Client Schema".
/// [Reference](https://spec.graphql.org/October2021/#sec-Schema)
#[derive(Debug, Clone, PartialEq)]
pub struct Schema<'a> {
    pub(crate) query_type: Option<&'a SchemaObject<'a>>,
    pub(crate) mutation_type: Option<&'a SchemaObject<'a>>,
    pub(crate) subscription_type: Option<&'a SchemaObject<'a>>,
    pub(crate) types:
        hashbrown::HashMap<&'a str, &'a SchemaType<'a>, DefaultHashBuilder, &'a bumpalo::Bump>,
}

impl<'a> DefaultIn<'a> for Schema<'a> {
    fn default_in(arena: &'a Bump) -> Self {
        Schema {
            query_type: None,
            mutation_type: None,
            subscription_type: None,
            types: HashMap::new_in(arena),
        }
    }
}

impl<'a> Schema<'a> {
    /// Returns whether the schema is a default, empty schema
    pub fn is_empty(&self) -> bool {
        self.types.is_empty()
            && self.query_type.is_none()
            && self.mutation_type.is_none()
            && self.subscription_type.is_none()
    }

    /// Returns the root object type for query operations
    #[inline]
    pub fn query_type(&self) -> Option<&'a SchemaObject<'a>> {
        self.query_type
    }

    /// Returns the root object type for mutation operations
    #[inline]
    pub fn mutation_type(&self) -> Option<&'a SchemaObject<'a>> {
        self.mutation_type
    }

    /// Returns the root object type for subscription operations
    #[inline]
    pub fn subscription_type(&self) -> Option<&'a SchemaObject<'a>> {
        self.subscription_type
    }

    /// Returns the appropriate object type depending on the passed operation kind
    #[inline]
    pub fn get_root_type(&self, operation_kind: OperationKind) -> Option<&'a SchemaObject<'a>> {
        match operation_kind {
            OperationKind::Query => self.query_type,
            OperationKind::Mutation => self.mutation_type,
            OperationKind::Subscription => self.subscription_type,
        }
    }

    /// Retrieves a kind by name from known schema types.
    #[inline]
    pub fn get_type(&self, name: &'a str) -> Option<&'a SchemaType<'a>> {
        self.types.get(name).copied()
    }

    /// Checks whether a given type is a sub type of another.
    ///
    /// This is typically used for return types of fields. A return type may be any given sub type
    /// of the return type of said field.
    pub fn is_sub_type(&self, abstract_type: SchemaType<'a>, sub_type: SchemaType<'a>) -> bool {
        match abstract_type {
            SchemaType::Union(schema_union) => schema_union.is_sub_type(sub_type),
            SchemaType::Interface(schema_interface) => schema_interface.is_sub_type(sub_type),
            SchemaType::Object(schema_object) => {
                if let SchemaType::Object(sub_object_type) = sub_type {
                    sub_object_type == schema_object
                } else {
                    false
                }
            }
            _ => false,
        }
    }
}

/// Generic trait for any schema type that implements fields
pub trait SchemaFields<'a>: Sized {
    /// Add a new [SchemaField] to the list of fields
    fn add_field(&mut self, ctx: &'a ASTContext, field: SchemaField<'a>);

    /// Get a [Map] of all fields
    fn get_fields(
        &self,
    ) -> HashMap<&'a str, &'a SchemaField<'a>, DefaultHashBuilder, &'a bumpalo::Bump>;

    /// Get a known field by name
    fn get_field(&self, name: &'a str) -> Option<&'a SchemaField<'a>> {
        self.get_fields().get(name).copied()
    }
}

/// Generic trait for any schema type that implements interfaces
pub trait SchemaInterfaces<'a>: Sized {
    /// Add a new [SchemaInterface] to the list of implemented interfaces
    fn add_interface(&mut self, ctx: &'a ASTContext, interface: &'a str);

    /// Get list of implemented [SchemaInterface]s
    fn get_interfaces(&self) -> Vec<'a, &'a str>;

    /// Checks whether given [ObjectType] is a possible subtype
    #[inline]
    fn implements_interface(&self, schema_interface: &SchemaInterface<'a>) -> bool {
        self.get_interfaces()
            .into_iter()
            .any(|interface| interface == schema_interface.name)
    }
}

/// Generic trait for any schema type that implements interfaces
pub trait SchemaPossibleTypes<'a>: Sized {
    /// Add a new [SchemaObject] to the list of possible types
    fn add_possible_type(&mut self, ctx: &'a ASTContext, object: &'a str);

    /// Get list of possible [SchemaObject] types
    fn get_possible_types(&self) -> Vec<'a, &'a str>;

    /// Get a specific possible type by name if it exists on the type
    #[inline]
    fn get_possible_type(&self, name: &'a str) -> Option<&'a str> {
        self.get_possible_types()
            .into_iter()
            .find(|&possible_type| possible_type == name)
    }

    /// Checks whether given [ObjectType] is a possible subtype
    #[inline]
    fn is_possible_type(&self, schema_object: &SchemaObject<'a>) -> bool {
        self.get_possible_types()
            .into_iter()
            .any(|possible_type| possible_type == schema_object.name)
    }
}

/// Generic trait for any schema type that may be a super type of other types
pub trait SchemaSuperType<'a>: Sized {
    /// Checks whether a given type is a sub type of the current super type.
    fn is_sub_type(&self, subtype: SchemaType<'a>) -> bool;
}

/// An Object type definition.
///
/// Most types in GraphQL are objects and define a set of fields and the interfaces they implement.
/// [Reference](https://spec.graphql.org/October2021/#sec-Objects)
#[derive(Debug, Clone, PartialEq)]
pub struct SchemaObject<'a> {
    pub name: &'a str,
    pub(crate) fields: HashMap<&'a str, &'a SchemaField<'a>, DefaultHashBuilder, &'a bumpalo::Bump>,
    pub(crate) interfaces: Vec<'a, &'a str>,
}

impl<'a> SchemaObject<'a> {
    #[inline]
    pub fn new(ctx: &'a ASTContext, name: &'a str) -> Self {
        SchemaObject {
            name,
            fields: HashMap::new_in(&ctx.arena),
            interfaces: Vec::new_in(&ctx.arena),
        }
    }
}

impl<'a> SchemaFields<'a> for SchemaObject<'a> {
    /// Add a new [SchemaField] to the list of fields
    fn add_field(&mut self, ctx: &'a ASTContext, field: SchemaField<'a>) {
        self.fields.insert(field.name, ctx.alloc(field));
    }

    /// Get a [Map] of all fields on the [SchemaObject]
    fn get_fields(
        &self,
    ) -> HashMap<&'a str, &'a SchemaField<'a>, DefaultHashBuilder, &'a bumpalo::Bump> {
        self.fields.clone()
    }
}

impl<'a> SchemaInterfaces<'a> for SchemaObject<'a> {
    /// Add a new [SchemaInterface] to the list of implemented interfaces
    fn add_interface(&mut self, _ctx: &'a ASTContext, interface: &'a str) {
        // TODO: this used to prepend
        self.interfaces.push(interface);
    }

    /// Get list of implemented [SchemaInterface]s
    #[inline]
    fn get_interfaces(&self) -> Vec<'a, &'a str> {
        self.interfaces.clone()
    }
}

/// An Interface type definition.
///
/// Any object or other interfaces may implement one or more interfaces and must then adhere to the
/// definition of this interface. A field that returns an interface as its return type may return
/// any object that implements this interface.
/// [Reference](https://spec.graphql.org/October2021/#sec-Interfaces)
#[derive(Clone, Debug)]
pub struct SchemaInterface<'a> {
    pub name: &'a str,
    pub(crate) fields: HashMap<&'a str, &'a SchemaField<'a>, DefaultHashBuilder, &'a bumpalo::Bump>,
    pub(crate) interfaces: Vec<'a, &'a str>,
    pub(crate) possible_interfaces: Vec<'a, &'a str>,
    pub(crate) possible_types: Vec<'a, &'a str>,
}

impl<'a> PartialEq for SchemaInterface<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
            && maps_are_equal(self.fields.clone(), other.fields.clone())
            && self.interfaces == other.interfaces
            && self.possible_interfaces == other.possible_interfaces
            && self.possible_types == other.possible_types
    }
}

impl<'a> SchemaInterface<'a> {
    #[inline]
    pub fn new(ctx: &'a ASTContext, name: &'a str) -> Self {
        SchemaInterface {
            name,
            fields: HashMap::new_in(&ctx.arena),
            interfaces: Vec::new_in(&ctx.arena),
            possible_interfaces: Vec::new_in(&ctx.arena),
            possible_types: Vec::new_in(&ctx.arena),
        }
    }

    /// Add a new [SchemaInterface] to the list that implements this [SchemaInterface]
    pub fn add_possible_interface(&mut self, _ctx: &'a ASTContext, interface: &'a str) {
        self.possible_interfaces.push(interface);
    }

    /// Get list of possible [SchemaInterface]s that implement this [SchemaInterface]
    #[inline]
    pub fn get_possible_interfaces(&self) -> Vec<'a, &'a str> {
        self.possible_interfaces.clone()
    }
}

impl<'a> SchemaFields<'a> for SchemaInterface<'a> {
    /// Add a new [SchemaField] to the list of fields
    fn add_field(&mut self, ctx: &'a ASTContext, field: SchemaField<'a>) {
        self.fields.insert(field.name, ctx.alloc(field));
    }

    /// Get a [Map] of all fields on the [SchemaInterface]
    fn get_fields(
        &self,
    ) -> HashMap<&'a str, &'a SchemaField<'a>, DefaultHashBuilder, &'a bumpalo::Bump> {
        self.fields.clone()
    }
}

impl<'a> SchemaInterfaces<'a> for SchemaInterface<'a> {
    /// Add a new [SchemaInterface] to the list of implemented interfaces
    fn add_interface(&mut self, _ctx: &'a ASTContext, interface: &'a str) {
        self.interfaces.push(interface);
    }

    /// Get list of implemented [SchemaInterface]s
    #[inline]
    fn get_interfaces(&self) -> Vec<'a, &'a str> {
        self.interfaces.clone()
    }
}

impl<'a> SchemaPossibleTypes<'a> for SchemaInterface<'a> {
    /// Add a new [SchemaObject] to the list of possible types
    fn add_possible_type(&mut self, _ctx: &'a ASTContext, object: &'a str) {
        self.possible_types.push(object);
    }

    /// Get list of possible [SchemaObject] types
    #[inline]
    fn get_possible_types(&self) -> Vec<'a, &'a str> {
        self.possible_types.clone()
    }
}

impl<'a> SchemaSuperType<'a> for SchemaInterface<'a> {
    #[inline]
    fn is_sub_type(&self, sub_type: SchemaType<'a>) -> bool {
        match sub_type {
            SchemaType::Object(schema_object) => schema_object.implements_interface(self),
            SchemaType::Interface(schema_interface) => schema_interface.implements_interface(self),
            _ => false,
        }
    }
}

/// An object Field type definition.
///
/// A field is like a function that given its arguments as input values produces an output value.
/// [Reference](https://spec.graphql.org/October2021/#FieldsDefinition)
#[derive(Debug, Clone, PartialEq)]
pub struct SchemaField<'a> {
    pub name: &'a str,
    pub arguments: HashMap<&'a str, SchemaInputField<'a>, DefaultHashBuilder, &'a bumpalo::Bump>,
    pub output_type: &'a TypeRef<'a>,
}

impl<'a> SchemaField<'a> {
    #[inline]
    pub fn new(ctx: &'a ASTContext, name: &'a str, output_type: &'a TypeRef<'a>) -> Self {
        SchemaField {
            name,
            arguments: HashMap::new_in(&ctx.arena),
            output_type,
        }
    }

    pub fn add_argument(&mut self, _ctx: &'a ASTContext, arg: SchemaInputField<'a>) {
        self.arguments.insert(arg.name, arg);
    }

    #[inline]
    pub fn get_argument(&self, name: &'a str) -> Option<&SchemaInputField<'a>> {
        self.arguments.get(name)
    }
}

/// A Union type definition.
///
/// A union contains a list of possible types that can be returned in its stead when its defined as
/// an output type.
/// [Reference](https://spec.graphql.org/October2021/#sec-Unions)
#[derive(Debug, Clone)]
pub struct SchemaUnion<'a> {
    pub name: &'a str,
    possible_types: Vec<'a, &'a str>,
}

impl<'a> PartialEq for SchemaUnion<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name && self.possible_types == other.possible_types
    }
}

impl<'a> SchemaUnion<'a> {
    #[inline]
    pub fn new(ctx: &'a ASTContext, name: &'a str) -> Self {
        SchemaUnion {
            name,
            possible_types: Vec::new_in(&ctx.arena),
        }
    }

    #[inline]
    pub fn is_sub_type(&self, sub_type: SchemaType<'a>) -> bool {
        match sub_type {
            SchemaType::Object(schema_object) => self
                .possible_types
                .iter()
                .any(|possible| possible == &schema_object.name),
            _ => false,
        }
    }
}

impl<'a> SchemaPossibleTypes<'a> for SchemaUnion<'a> {
    /// Add a new [SchemaObject] to the list of possible types
    fn add_possible_type(&mut self, _ctx: &'a ASTContext, object: &'a str) {
        self.possible_types.push(object);
    }

    /// Get list of possible [SchemaObject] types
    #[inline]
    fn get_possible_types(&self) -> Vec<'a, &'a str> {
        self.possible_types.clone()
    }
}

impl<'a> SchemaSuperType<'a> for SchemaUnion<'a> {
    #[inline]
    fn is_sub_type(&self, sub_type: SchemaType<'a>) -> bool {
        if let SchemaType::Object(schema_object) = sub_type {
            self.is_possible_type(schema_object)
        } else {
            false
        }
    }
}

/// A Scalar type definition.
///
/// Scalars represent primitive leaf values in GraphQL that are represented with a specific
/// serializer and deserializer, which makes the values returnable to a GraphQL client or readable
/// by a GraphQL API.
/// [Reference](https://spec.graphql.org/October2021/#sec-Scalars)
#[derive(Debug, Clone, PartialEq)]
pub struct SchemaScalar<'a> {
    pub name: &'a str,
}

impl<'a> SchemaScalar<'a> {
    #[inline]
    pub fn new(name: &'a str) -> Self {
        SchemaScalar { name }
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct SchemaEnum<'a> {
    pub name: &'a str,
    pub values: HashSet<&'a str, DefaultHashBuilder, &'a bumpalo::Bump>,
}

impl<'a> SchemaEnum<'a> {
    #[inline]
    pub fn new(ctx: &'a ASTContext, name: &'a str) -> Self {
        SchemaEnum {
            name,
            values: HashSet::new_in(&ctx.arena),
        }
    }

    pub fn add_value(&mut self, _ctx: &'a ASTContext, value: &'a str) {
        self.values.insert(value);
    }
}

/// An Input Object type definition.
///
/// Inputs, such as arguments, may sometimes be nested and accept objects that must adhere to the
/// shape of an Input Object definition. This is often used to represent more complex inputs.
/// [Reference](https://spec.graphql.org/October2021/#sec-Input-Objects)
#[derive(Debug, Clone, PartialEq)]
pub struct SchemaInputObject<'a> {
    pub name: &'a str,
    pub fields: HashMap<&'a str, SchemaInputField<'a>, DefaultHashBuilder, &'a bumpalo::Bump>,
}

impl<'a> SchemaInputObject<'a> {
    #[inline]
    pub fn new(ctx: &'a ASTContext, name: &'a str) -> Self {
        SchemaInputObject {
            name,
            fields: HashMap::new_in(&ctx.arena),
        }
    }

    pub fn add_field(&mut self, _ctx: &'a ASTContext, field: SchemaInputField<'a>) {
        self.fields.insert(field.name, field);
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct SchemaInputField<'a> {
    pub name: &'a str,
    pub input_type: &'a TypeRef<'a>,
}

impl<'a> SchemaInputField<'a> {
    #[inline]
    pub fn new(name: &'a str, input_type: &'a TypeRef<'a>) -> Self {
        SchemaInputField { name, input_type }
    }
}

/// A named type enum that represents all possible GraphQL definition types.
///
/// [Reference](https://spec.graphql.org/October2021/#sec-Types)
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum SchemaType<'a> {
    InputObject(&'a SchemaInputObject<'a>),
    Object(&'a SchemaObject<'a>),
    Union(&'a SchemaUnion<'a>),
    Interface(&'a SchemaInterface<'a>),
    Scalar(&'a SchemaScalar<'a>),
    Enum(&'a SchemaEnum<'a>),
}

impl<'a> SchemaType<'a> {
    #[inline]
    pub fn name(&self) -> &'a str {
        match self {
            SchemaType::InputObject(x) => x.name,
            SchemaType::Object(x) => x.name,
            SchemaType::Union(x) => x.name,
            SchemaType::Interface(x) => x.name,
            SchemaType::Scalar(x) => x.name,
            SchemaType::Enum(x) => x.name,
        }
    }

    pub fn object(&self) -> Option<&'a SchemaObject<'a>> {
        match self {
            SchemaType::Object(x) => Some(x),
            _ => None,
        }
    }

    pub fn input_object(&self) -> Option<&'a SchemaInputObject<'a>> {
        match self {
            SchemaType::InputObject(x) => Some(x),
            _ => None,
        }
    }

    pub fn interface(&self) -> Option<&'a SchemaInterface<'a>> {
        match self {
            SchemaType::Interface(x) => Some(x),
            _ => None,
        }
    }

    pub fn union_type(&self) -> Option<&'a SchemaUnion<'a>> {
        match self {
            SchemaType::Union(x) => Some(x),
            _ => None,
        }
    }

    pub fn input_type(&self) -> Option<InputType<'a>> {
        match self {
            SchemaType::Scalar(x) => Some(InputType::Scalar(x)),
            SchemaType::Enum(x) => Some(InputType::Enum(x)),
            SchemaType::InputObject(x) => Some(InputType::InputObject(x)),
            _ => None,
        }
    }

    pub fn output_type(&self) -> Option<OutputType<'a>> {
        match self {
            SchemaType::Object(x) => Some(OutputType::Object(x)),
            SchemaType::Union(x) => Some(OutputType::Union(x)),
            SchemaType::Interface(x) => Some(OutputType::Interface(x)),
            SchemaType::Scalar(x) => Some(OutputType::Scalar(x)),
            SchemaType::Enum(x) => Some(OutputType::Enum(x)),
            _ => None,
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
pub enum OwnedSchemaType<'a> {
    InputObject(SchemaInputObject<'a>),
    Object(SchemaObject<'a>),
    Union(SchemaUnion<'a>),
    Interface(SchemaInterface<'a>),
    Scalar(SchemaScalar<'a>),
    Enum(SchemaEnum<'a>),
}

impl<'a> OwnedSchemaType<'a> {
    #[inline]
    pub fn name(&self) -> &'a str {
        match self {
            OwnedSchemaType::InputObject(x) => x.name,
            OwnedSchemaType::Object(x) => x.name,
            OwnedSchemaType::Union(x) => x.name,
            OwnedSchemaType::Interface(x) => x.name,
            OwnedSchemaType::Scalar(x) => x.name,
            OwnedSchemaType::Enum(x) => x.name,
        }
    }

    pub fn object(&'a self) -> Option<&'a SchemaObject<'a>> {
        match self {
            OwnedSchemaType::Object(x) => Some(x),
            _ => None,
        }
    }

    pub fn input_object(&'a self) -> Option<&'a SchemaInputObject<'a>> {
        match self {
            OwnedSchemaType::InputObject(x) => Some(x),
            _ => None,
        }
    }

    pub fn interface(&'a self) -> Option<&'a SchemaInterface<'a>> {
        match self {
            OwnedSchemaType::Interface(x) => Some(x),
            _ => None,
        }
    }

    pub fn union_type(&'a self) -> Option<&'a SchemaUnion<'a>> {
        match self {
            OwnedSchemaType::Union(x) => Some(x),
            _ => None,
        }
    }

    pub fn input_type(&'a self) -> Option<InputType<'a>> {
        match self {
            OwnedSchemaType::Scalar(x) => Some(InputType::Scalar(x)),
            OwnedSchemaType::Enum(x) => Some(InputType::Enum(x)),
            OwnedSchemaType::InputObject(x) => Some(InputType::InputObject(x)),
            _ => None,
        }
    }

    pub fn output_type(&'a self) -> Option<OutputType<'a>> {
        match self {
            OwnedSchemaType::Object(x) => Some(OutputType::Object(x)),
            OwnedSchemaType::Union(x) => Some(OutputType::Union(x)),
            OwnedSchemaType::Interface(x) => Some(OutputType::Interface(x)),
            OwnedSchemaType::Scalar(x) => Some(OutputType::Scalar(x)),
            OwnedSchemaType::Enum(x) => Some(OutputType::Enum(x)),
            _ => None,
        }
    }
}

impl<'a> From<&'a SchemaObject<'a>> for SchemaType<'a> {
    #[inline]
    fn from(schema_object: &'a SchemaObject<'a>) -> Self {
        SchemaType::Object(schema_object)
    }
}

impl<'a> From<&'a SchemaUnion<'a>> for SchemaType<'a> {
    #[inline]
    fn from(schema_union: &'a SchemaUnion<'a>) -> Self {
        SchemaType::Union(schema_union)
    }
}

impl<'a> From<&'a SchemaInterface<'a>> for SchemaType<'a> {
    #[inline]
    fn from(schema_interface: &'a SchemaInterface<'a>) -> Self {
        SchemaType::Interface(schema_interface)
    }
}

impl<'a> From<OutputType<'a>> for SchemaType<'a> {
    #[inline]
    fn from(type_ref: OutputType<'a>) -> Self {
        match type_ref {
            OutputType::Object(x) => SchemaType::Object(x),
            OutputType::Union(x) => SchemaType::Union(x),
            OutputType::Interface(x) => SchemaType::Interface(x),
            OutputType::Scalar(x) => SchemaType::Scalar(x),
            OutputType::Enum(x) => SchemaType::Enum(x),
        }
    }
}

impl<'a> From<InputType<'a>> for SchemaType<'a> {
    #[inline]
    fn from(type_ref: InputType<'a>) -> Self {
        match type_ref {
            InputType::InputObject(x) => SchemaType::InputObject(x),
            InputType::Scalar(x) => SchemaType::Scalar(x),
            InputType::Enum(x) => SchemaType::Enum(x),
        }
    }
}

/// An output type enum that represents all possible GraphQL definition types that a field may
/// return.
///
/// [Reference](https://spec.graphql.org/October2021/#sec-Input-and-Output-Types)
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum OutputType<'a> {
    Object(&'a SchemaObject<'a>),
    Union(&'a SchemaUnion<'a>),
    Interface(&'a SchemaInterface<'a>),
    Scalar(&'a SchemaScalar<'a>),
    Enum(&'a SchemaEnum<'a>),
}

impl<'a> OutputType<'a> {
    #[inline]
    pub fn name(&self) -> &str {
        match self {
            OutputType::Object(x) => x.name,
            OutputType::Union(x) => x.name,
            OutputType::Interface(x) => x.name,
            OutputType::Scalar(x) => x.name,
            OutputType::Enum(x) => x.name,
        }
    }

    #[inline]
    pub fn into_schema_type(&self) -> SchemaType<'a> {
        match self {
            OutputType::Object(x) => SchemaType::Object(x),
            OutputType::Union(x) => SchemaType::Union(x),
            OutputType::Interface(x) => SchemaType::Interface(x),
            OutputType::Scalar(x) => SchemaType::Scalar(x),
            OutputType::Enum(x) => SchemaType::Enum(x),
        }
    }
}

/// An input type enum that represents all possible GraphQL definition types that an argument or
/// input object field may accept.
///
/// [Reference](https://spec.graphql.org/October2021/#sec-Input-and-Output-Types)
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum InputType<'a> {
    InputObject(&'a SchemaInputObject<'a>),
    Scalar(&'a SchemaScalar<'a>),
    Enum(&'a SchemaEnum<'a>),
}

impl<'a> InputType<'a> {
    #[inline]
    pub fn name(&self) -> &str {
        match self {
            InputType::InputObject(o) => o.name,
            InputType::Scalar(s) => s.name,
            InputType::Enum(e) => e.name,
        }
    }

    #[inline]
    pub fn named_type(&self) -> SchemaType<'a> {
        match self {
            InputType::InputObject(x) => SchemaType::InputObject(x),
            InputType::Scalar(x) => SchemaType::Scalar(x),
            InputType::Enum(x) => SchemaType::Enum(x),
        }
    }
}

#[derive(Clone, Copy)]
pub enum TypeRef<'a> {
    Type(&'a str),
    ListType(&'a TypeRef<'a>),
    NonNullType(&'a TypeRef<'a>),
}

impl<'a> TypeRef<'a> {
    #[inline]
    pub fn of_type(&self, schema: &'a Schema<'a>) -> &'a SchemaType<'a> {
        match self {
            TypeRef::Type(of_type) => {
                let schema_type = schema
                    .get_type(of_type)
                    .expect("Referenced type should exist in the schema.");
                schema_type
            }
            TypeRef::ListType(of_type) => of_type.of_type(schema),
            TypeRef::NonNullType(of_type) => of_type.of_type(schema),
        }
    }
}

/// This implementation is necessary to circuit break circular types.
/// Without this impl, `Debug` would print on and on, overflowing the stack as it's bouncing between types over and over.
impl<'a> std::fmt::Debug for TypeRef<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Type(arg0) => f.debug_tuple("OutputTypeRef").field(&arg0).finish(),
            Self::ListType(arg0) => f.debug_tuple("ListType").field(arg0).finish(),
            Self::NonNullType(arg0) => f.debug_tuple("NonNullType").field(arg0).finish(),
        }
    }
}

/// This implementation is necessary to circuit break circular types.
/// Without this impl, `PartialEq` would never stop comparing types referencing each other.
/// We achieve this by only ever comparing type names, which is all we need for comparing references.
impl<'a> PartialEq for TypeRef<'a> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Type(left), Self::Type(right)) => left == right,
            (Self::ListType(left), Self::ListType(right)) => left == right,
            (Self::NonNullType(left), Self::NonNullType(right)) => left == right,
            _ => false,
        }
    }
}

/// By default, Toolshed Maps also compare insertion order. This utility compares only entries, not ordering.
fn maps_are_equal<'a, V>(
    left: HashMap<&'a str, V, DefaultHashBuilder, &'a bumpalo::Bump>,
    right: HashMap<&'a str, V, DefaultHashBuilder, &'a bumpalo::Bump>,
) -> bool
where
    V: PartialEq + Copy,
{
    let length_matches = left.len() == right.len();

    // If the length matches and all entries in `left` are contained in `right` and equal, we can consider the maps equal.
    length_matches
        && left.iter().all(|(k, left_val)| {
            right
                .get(k)
                .map(|right_val| left_val == right_val)
                .unwrap_or(false)
        })
}

/// Helper trait to generalize comparisons (see `eq_named_lists`).
pub trait Named {
    fn name(&self) -> &str;
}

impl<'a> Named for &'a SchemaInterface<'a> {
    fn name(&self) -> &str {
        self.name
    }
}

impl<'a> Named for &'a SchemaObject<'a> {
    fn name(&self) -> &str {
        self.name
    }
}

impl<'a> Named for &'a SchemaUnion<'a> {
    fn name(&self) -> &str {
        self.name
    }
}

pub fn schema_type_to_type<'arena>(
    ctx: &'arena ASTContext,
    schema: &'arena Schema,
    schema_type: TypeRef<'arena>,
) -> Type<'arena> {
    match schema_type {
        t @ TypeRef::Type(_) => match t.of_type(schema).input_type().expect("input schema type") {
            InputType::InputObject(SchemaInputObject { name, .. })
            | InputType::Scalar(SchemaScalar { name, .. })
            | InputType::Enum(SchemaEnum { name, .. }) => Type::NamedType(NamedType { name }),
        },
        TypeRef::ListType(t) => Type::ListType(ctx.alloc(schema_type_to_type(ctx, schema, *t))),
        TypeRef::NonNullType(t) => {
            Type::NonNullType(ctx.alloc(schema_type_to_type(ctx, schema, *t)))
        }
    }
}