apollo-compiler 1.31.1

A compiler for the GraphQL query language.
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
use crate::ast;
use crate::ast::DirectiveLocation;
use crate::ast::Type;
use crate::coordinate::SchemaCoordinate;
use crate::coordinate::TypeAttributeCoordinate;
use crate::diagnostic::CliReport;
use crate::executable;
use crate::parser::SourceSpan;
use crate::Name;
use crate::Node;
use std::fmt;
use thiserror::Error;

/// Structured data about a diagnostic.
#[derive(Debug, Error, Clone, Hash, PartialEq, Eq)]
#[non_exhaustive]
pub(crate) enum DiagnosticData {
    #[error("the variable `${name}` is declared multiple times")]
    UniqueVariable {
        name: Name,
        original_definition: Option<SourceSpan>,
        redefined_definition: Option<SourceSpan>,
    },
    #[error("the argument `{name}` is provided multiple times")]
    UniqueArgument {
        name: Name,
        original_definition: Option<SourceSpan>,
        redefined_definition: Option<SourceSpan>,
    },
    #[error("the value `{name}` is defined multiple times")]
    UniqueInputValue {
        name: Name,
        original_definition: Option<SourceSpan>,
        redefined_definition: Option<SourceSpan>,
    },
    #[error("the argument `{name}` is not supported by `{coordinate}`")]
    UndefinedArgument {
        name: Name,
        coordinate: SchemaCoordinate,
        definition_location: Option<SourceSpan>,
    },
    #[error("cannot find type `{name}` in this document")]
    UndefinedDefinition {
        /// Name of the type not in scope
        name: Name,
    },
    #[error("cannot find directive `@{name}` in this document")]
    UndefinedDirective {
        /// Name of the missing directive
        name: Name,
    },
    #[error("variable `${name}` is not defined")]
    UndefinedVariable {
        /// Name of the variable not in scope
        name: Name,
    },
    #[error("cannot find fragment `{name}` in this document")]
    UndefinedFragment {
        /// Name of the fragment not in scope
        name: Name,
    },
    #[error("value `{value}` does not exist on `{definition}`")]
    UndefinedEnumValue {
        /// Value of the enum value that doesn't exist
        value: Name,
        /// Name of the enum
        definition: Name,
        definition_location: Option<SourceSpan>,
    },
    #[error("field `{value}` does not exist on `{definition}`")]
    UndefinedInputValue {
        /// Value of the input object field that doesn't exist
        value: Name,
        /// Name of the input object type
        definition: Name,
        definition_location: Option<SourceSpan>,
    },
    #[error("type `{name}` does not satisfy interface `{interface}`: missing field `{field}`")]
    MissingInterfaceField {
        name: Name,
        /// Location of the `implements XYZ` of the interface
        implements_location: Option<SourceSpan>,
        interface: Name,
        field: Name,
        /// Location of the definition of the field in the interface
        field_location: Option<SourceSpan>,
    },
    #[error("the required argument `{coordinate}` is not provided")]
    RequiredArgument {
        name: Name,
        coordinate: SchemaCoordinate,
        expected_type: Node<Type>,
        definition_location: Option<SourceSpan>,
    },
    #[error("the required field `{coordinate}` is not provided")]
    RequiredField {
        name: Name,
        coordinate: TypeAttributeCoordinate,
        expected_type: Node<Type>,
        definition_location: Option<SourceSpan>,
    },
    #[error(
        "interface `{interface}` declares that it implements `{via_interface}`, but to do so it must also implement `{missing_interface}`"
    )]
    TransitiveImplementedInterfaces {
        /// Name of the interface definition
        interface: Name,
        /// Super interface that declares the implementation
        via_interface: Name,
        /// Source location where the super interface declares the implementation
        transitive_interface_location: Option<SourceSpan>,
        /// Interface that should be implemented
        missing_interface: Name,
    },
    #[error("`{name}` field must return an output type")]
    OutputType {
        /// Field name.
        name: Name,
        /// The kind of type that the field is declared with.
        describe_type: &'static str,
        type_location: Option<SourceSpan>,
    },
    #[error("`{name}` field must be of an input type")]
    InputType {
        /// Field name.
        name: Name,
        /// The kind of type that the field is declared with.
        describe_type: &'static str,
        type_location: Option<SourceSpan>,
    },
    #[error("`${name}` variable must be of an input type")]
    VariableInputType {
        /// Variable name.
        name: Name,
        ty: Node<Type>,
        /// The kind of type that the variable is declared with.
        describe_type: &'static str,
    },
    #[error("missing query root operation type in schema definition")]
    QueryRootOperationType,
    #[error("unused variable: `${name}`")]
    UnusedVariable { name: Name },
    #[error("`{name}` field must return an object type")]
    RootOperationObjectType {
        /// Name of the root operation type
        name: Name,
        /// Category of the type
        describe_type: &'static str,
    },
    #[error("union member `{name}` must be an object type")]
    UnionMemberObjectType {
        /// Name of the type in the union
        name: Name,
        /// Category of the type
        describe_type: &'static str,
    },
    #[error("{name} directive is not supported for {location} location")]
    UnsupportedLocation {
        /// Name of the directive
        name: Name,
        /// The location where the directive is attempted to be used
        location: DirectiveLocation,
        /// Locations that *are* valid for this directive
        valid_locations: Vec<DirectiveLocation>,
        /// The source location where the directive that's being used was defined.
        definition_location: Option<SourceSpan>,
    },
    #[error("expected value of type {ty}, found {}", .value.describe())]
    UnsupportedValueType {
        /// The actual value provided
        value: Node<ast::Value>,
        /// Expected concrete type
        ty: Node<Type>,
        definition_location: Option<SourceSpan>,
    },
    #[error("int cannot represent non 32-bit signed integer value")]
    IntCoercionError {
        /// The int value that cannot be coerced
        value: String,
    },
    #[error("float cannot represent non-finite 64-bit floating point value")]
    FloatCoercionError {
        /// The float value that cannot be coerced
        value: String,
    },
    #[error("non-repeatable directive {name} can only be used once per location")]
    UniqueDirective {
        /// Name of the non-unique directive.
        name: Name,
        original_application: Option<SourceSpan>,
    },
    #[error("interface, union and object types must have a subselection set")]
    MissingSubselection {
        coordinate: TypeAttributeCoordinate,
        output_type: Name,
        describe_type: &'static str,
    },
    #[error(
        "{} must have a composite type in its type condition",
        fragment_name_or_inline(name)
    )]
    InvalidFragmentTarget {
        /// Name of the fragment, None if an inline fragment.
        name: Option<Name>,
        /// Name of the type on which the fragment is declared
        ty: Name,
    },
    #[error(
        "{} with type condition `{type_condition}` cannot be applied to `{type_name}`",
        fragment_name_or_inline(name)
    )]
    InvalidFragmentSpread {
        /// Fragment name or None if it's an inline fragment
        name: Option<Name>,
        /// Type name the fragment is being applied to
        type_name: Name,
        type_condition: Name,
        /// Source location where the fragment is defined
        fragment_location: Option<SourceSpan>,
        /// Source location of the type the fragment is being applied to.
        type_location: Option<SourceSpan>,
    },
    #[error("fragment `{name}` must be used in an operation")]
    UnusedFragment {
        /// Name of the fragment
        name: Name,
    },
    #[error(
        "variable `${variable}` of type `{variable_type}` cannot be used for argument `{argument}` of type `{argument_type}`"
    )]
    DisallowedVariableUsage {
        /// Name of the variable being used in an argument
        variable: Name,
        variable_type: Type,
        variable_location: Option<SourceSpan>,
        /// Name of the argument where variable is used
        argument: Name,
        argument_type: Type,
        argument_location: Option<SourceSpan>,
    },
    #[error("`{name}` directive definition cannot reference itself")]
    RecursiveDirectiveDefinition {
        name: Name,
        trace: Vec<Node<ast::Directive>>,
    },
    #[error("interface {name} cannot implement itself")]
    RecursiveInterfaceDefinition { name: Name },
    #[error("`{name}` input object cannot reference itself")]
    RecursiveInputObjectDefinition {
        name: Name,
        trace: Vec<Node<ast::InputValueDefinition>>,
    },
    #[error("`{name}` fragment cannot reference itself")]
    RecursiveFragmentDefinition {
        /// Source location of just the "fragment FragName" part.
        head_location: Option<SourceSpan>,
        name: Name,
        trace: Vec<Node<executable::FragmentSpread>>,
    },
    #[error("`{name}` contains too much nesting")]
    DeeplyNestedType {
        name: Name,
        describe_type: &'static str,
    },
    #[error("too much recursion")]
    RecursionError {},
    #[error("`{type_name}` has no fields")]
    EmptyFieldSet {
        type_name: Name,
        type_location: Option<SourceSpan>,
        extensions_locations: Vec<Option<SourceSpan>>,
    },
    #[error("`{type_name}` has no enum values")]
    EmptyValueSet {
        type_name: Name,
        type_location: Option<SourceSpan>,
        extensions_locations: Vec<Option<SourceSpan>>,
    },
    #[error("`{type_name}` has no member types")]
    EmptyMemberSet {
        type_name: Name,
        type_location: Option<SourceSpan>,
        extensions_locations: Vec<Option<SourceSpan>>,
    },
    #[error("`{type_name}` has no input values")]
    EmptyInputValueSet {
        type_name: Name,
        type_location: Option<SourceSpan>,
        extensions_locations: Vec<Option<SourceSpan>>,
    },
    #[error(
        "{describe} cannot be named `{name}` as names starting with two underscores are reserved"
    )]
    ReservedName { name: Name, describe: &'static str },
}

impl DiagnosticData {
    pub(crate) fn report(&self, main_location: Option<SourceSpan>, report: &mut CliReport) {
        match self {
            DiagnosticData::UniqueVariable {
                name,
                original_definition,
                redefined_definition,
            } => {
                report.with_label_opt(
                    *original_definition,
                    format_args!("previous definition of `${name}` here"),
                );
                report.with_label_opt(
                    *redefined_definition,
                    format_args!("`${name}` defined again here"),
                );
            }
            DiagnosticData::UniqueArgument {
                name,
                original_definition,
                redefined_definition,
            } => {
                report.with_label_opt(
                    *original_definition,
                    format_args!("previously provided `{name}` here"),
                );
                report.with_label_opt(
                    *redefined_definition,
                    format_args!("`{name}` provided again here"),
                );
                report.with_help(format_args!(
                    "`{name}` argument must only be provided once."
                ));
            }
            DiagnosticData::UniqueInputValue {
                name,
                original_definition,
                redefined_definition,
            } => {
                report.with_label_opt(
                    *original_definition,
                    format_args!("previous definition of `{name}` here"),
                );
                report.with_label_opt(
                    *redefined_definition,
                    format_args!("`{name}` defined again here"),
                );
                report.with_help(format_args!(
                    "`{name}` must only be defined once in this argument list or input object definition."
                ));
            }
            DiagnosticData::UndefinedArgument {
                coordinate,
                definition_location,
                ..
            } => {
                report.with_label_opt(main_location, "argument by this name not found");
                report.with_label_opt(
                    *definition_location,
                    format_args!("{coordinate} defined here"),
                );
            }
            DiagnosticData::RequiredArgument {
                name,
                coordinate: _,
                expected_type: _,
                definition_location,
            } => {
                report.with_label_opt(
                    main_location,
                    format_args!("missing value for argument `{name}`"),
                );
                report.with_label_opt(*definition_location, "argument defined here");
            }
            DiagnosticData::RequiredField {
                name,
                coordinate: _,
                expected_type: _,
                definition_location,
            } => {
                report.with_label_opt(
                    main_location,
                    format_args!("missing value for field `{name}`"),
                );
                report.with_label_opt(*definition_location, "field defined here");
            }
            DiagnosticData::UndefinedDefinition { .. } => {
                report.with_label_opt(main_location, "not found in this scope");
            }
            DiagnosticData::UndefinedDirective { .. } => {
                report.with_label_opt(main_location, "directive not defined");
            }
            DiagnosticData::UndefinedVariable { .. } => {
                report.with_label_opt(main_location, "not found in this scope");
            }
            DiagnosticData::UndefinedFragment { name } => {
                report.with_label_opt(
                    main_location,
                    format_args!("fragment `{name}` is not defined"),
                );
            }
            DiagnosticData::UndefinedEnumValue {
                value: _,
                definition,
                definition_location,
            } => {
                report.with_label_opt(
                    main_location,
                    format_args!("value does not exist on `{definition}` enum"),
                );
                report.with_label_opt(*definition_location, "enum defined here");
            }
            DiagnosticData::UndefinedInputValue {
                value: _,
                definition,
                definition_location,
            } => {
                report.with_label_opt(
                    main_location,
                    format_args!("value does not exist on `{definition}` input object"),
                );
                report.with_label_opt(*definition_location, "input object defined here");
            }
            DiagnosticData::RecursiveDirectiveDefinition { name, trace } => {
                report.with_label_opt(main_location, "recursive directive definition");
                label_recursive_trace(report, trace, name, |directive| &directive.name);
            }
            DiagnosticData::RecursiveInterfaceDefinition { name } => {
                report.with_label_opt(
                    main_location,
                    format_args!("interface {name} cannot implement itself"),
                );
            }
            DiagnosticData::RecursiveInputObjectDefinition { name, trace } => {
                report.with_label_opt(main_location, "cyclical input object definition");
                label_recursive_trace(report, trace, name, |reference| &reference.name);
            }
            DiagnosticData::RecursiveFragmentDefinition {
                head_location,
                name,
                trace,
            } => {
                report.with_label_opt(
                    head_location.or(main_location),
                    "recursive fragment definition",
                );
                label_recursive_trace(report, trace, name, |reference| &reference.fragment_name);
            }
            DiagnosticData::DeeplyNestedType { describe_type, .. } => {
                report.with_label_opt(
                    main_location,
                    format_args!(
                        "references a very long chain of {describe_type}s in its definition"
                    ),
                );
            }
            DiagnosticData::MissingInterfaceField {
                name: _,
                implements_location,
                interface,
                field,
                field_location,
            } => {
                report.with_label_opt(
                    main_location,
                    format_args!("add `{field}` field to this type"),
                );
                report.with_label_opt(
                    *implements_location,
                    format_args!("implementation of interface {interface} declared here"),
                );
                report.with_label_opt(
                    *field_location,
                    format_args!("`{interface}.{field}` originally defined here"),
                );
                report.with_help(
                    "An object or interface must declare all fields required by the interfaces it implements",
                )
            }
            DiagnosticData::TransitiveImplementedInterfaces {
                interface: _,
                via_interface,
                transitive_interface_location,
                missing_interface,
            } => {
                report.with_label_opt(
                    *transitive_interface_location,
                    format!(
                        "implementation of {missing_interface} declared by {via_interface} here"
                    ),
                );
                report.with_label_opt(
                    main_location,
                    format_args!("{missing_interface} must also be implemented here"),
                );
            }
            DiagnosticData::UnusedVariable { .. } => {
                report.with_label_opt(main_location, "variable is never used");
            }
            DiagnosticData::UnusedFragment { name } => {
                report.with_label_opt(main_location, format_args!("`{name}` is defined here"));
                report.with_help(format_args!(
                    "fragment `{name}` must be used in an operation"
                ));
            }
            DiagnosticData::RootOperationObjectType {
                name: _,
                describe_type,
            } => {
                report.with_label_opt(main_location, format_args!("this is {describe_type}"));
                report.with_help("Root operation type must be an object type.");
            }
            DiagnosticData::UnionMemberObjectType {
                name: _,
                describe_type,
            } => {
                report.with_label_opt(main_location, format_args!("this is {describe_type}"));
                report.with_help("Union members must be object types.");
            }
            DiagnosticData::OutputType {
                name,
                describe_type,
                type_location,
            } => {
                report.with_label_opt(
                    type_location.or(main_location),
                    format_args!("this is {describe_type}"),
                );
                report.with_help(format!("Scalars, Objects, Interfaces, Unions and Enums are output types. Change `{name}` field to return one of these output types."));
            }
            DiagnosticData::InputType {
                name,
                describe_type,
                type_location,
            } => {
                report.with_label_opt(
                    type_location.or(main_location),
                    format_args!("this is {describe_type}"),
                );
                report.with_help(format!("Scalars, Enums, and Input Objects are input types. Change `{name}` field to take one of these input types."));
            }
            DiagnosticData::VariableInputType {
                name: _,
                ty,
                describe_type,
            } => {
                report.with_label_opt(
                    ty.location().or(main_location),
                    format_args!("this is {describe_type}"),
                );
                report.with_help("objects, unions, and interfaces cannot be used because variables can only be of input type");
            }
            DiagnosticData::QueryRootOperationType => {
                report.with_label_opt(
                    main_location,
                    "`query` root operation type must be defined here",
                );
            }
            DiagnosticData::UnsupportedLocation {
                name: _,
                location,
                valid_locations,
                definition_location,
            } => {
                report.with_label_opt(
                    main_location,
                    format_args!("directive cannot be used on {location}"),
                );
                report.with_label_opt(*definition_location, "directive defined here");
                report.with_help(format!(
                    "the directive must be used in a location that the service has declared support for: {}",
                    CommaSeparated(valid_locations),
                ));
            }
            DiagnosticData::UnsupportedValueType {
                value,
                ty,
                definition_location,
            } => {
                report.with_label_opt(
                    main_location,
                    format_args!("provided value is {}", value.describe()),
                );
                report.with_label_opt(
                    *definition_location,
                    format_args!("expected type declared here as {ty}"),
                );
            }
            DiagnosticData::IntCoercionError { .. } => {
                report.with_label_opt(main_location, "cannot be coerced to a 32-bit integer");
            }
            DiagnosticData::FloatCoercionError { .. } => {
                report.with_label_opt(main_location, "cannot be coerced to a finite 64-bit float");
            }
            DiagnosticData::UniqueDirective {
                name,
                original_application,
            } => {
                report.with_label_opt(
                    *original_application,
                    format_args!("directive `@{name}` first called here"),
                );
                report.with_label_opt(
                    main_location,
                    format_args!("directive `@{name}` called again here"),
                );
            }
            DiagnosticData::MissingSubselection {
                coordinate,
                output_type,
                describe_type,
            } => {
                report.with_label_opt(
                    main_location,
                    format_args!(
                        "`{coordinate}` is {describe_type} `{output_type}` and must select fields"
                    ),
                );
            }
            DiagnosticData::InvalidFragmentTarget { name: _, ty } => {
                report.with_label_opt(
                    main_location,
                    format!("fragment declares unsupported type condition `{ty}`"),
                );
                report.with_help("fragments cannot be defined on enums, scalars and input objects");
            }
            DiagnosticData::InvalidFragmentSpread {
                name,
                type_name: _,
                type_condition,
                fragment_location,
                type_location,
            } => {
                if let Some(name) = name {
                    report.with_label_opt(
                        main_location,
                        format_args!("fragment `{name}` cannot be applied"),
                    );
                    // Only for named fragments: for inline fragments the type condition is right
                    // there
                    report.with_label_opt(
                        *fragment_location,
                        format_args!(
                            "fragment declared with type condition `{type_condition}` here"
                        ),
                    );
                } else {
                    report.with_label_opt(main_location, "inline fragment cannot be applied");
                }
                report.with_label_opt(
                    *type_location,
                    format!("type condition `{type_condition}` is not assignable to this type"),
                );
            }
            DiagnosticData::DisallowedVariableUsage {
                variable,
                variable_type,
                variable_location,
                ..
            } => {
                report.with_label_opt(
                    *variable_location,
                    format_args!(
                        "variable `${variable}` of type `{variable_type}` is declared here"
                    ),
                );
                report.with_label_opt(
                    main_location,
                    format_args!("variable `${variable}` used here"),
                );
            }
            DiagnosticData::RecursionError {} => {}
            DiagnosticData::EmptyFieldSet {
                type_name,
                type_location,
                extensions_locations,
            } => {
                Self::report_empty_type(
                    report,
                    type_name,
                    type_location,
                    extensions_locations,
                    "fields",
                );
            }
            DiagnosticData::EmptyValueSet {
                type_name,
                type_location,
                extensions_locations,
            } => {
                Self::report_empty_type(
                    report,
                    type_name,
                    type_location,
                    extensions_locations,
                    "enum values",
                );
            }
            DiagnosticData::EmptyMemberSet {
                type_name,
                type_location,
                extensions_locations,
            } => {
                Self::report_empty_type(
                    report,
                    type_name,
                    type_location,
                    extensions_locations,
                    "union member types",
                );
            }
            DiagnosticData::EmptyInputValueSet {
                type_name,
                type_location,
                extensions_locations,
            } => {
                Self::report_empty_type(
                    report,
                    type_name,
                    type_location,
                    extensions_locations,
                    "input values",
                );
            }
            DiagnosticData::ReservedName { name, .. } => {
                report.with_label_opt(name.location(), "Pick a different name here");
            }
        }
    }

    fn report_empty_type(
        report: &mut CliReport,
        type_name: &Name,
        type_location: &Option<SourceSpan>,
        extensions_locations: &[Option<SourceSpan>],
        describe_missing_kind: &str,
    ) {
        report.with_label_opt(
            *type_location,
            format_args!("{type_name} type defined here"),
        );
        extensions_locations.iter().for_each(|location| {
            report.with_label_opt(
                *location,
                format_args!("{type_name} extension defined here"),
            );
        });
        let and_extensions_message = if !extensions_locations.is_empty() {
            " or its type extensions"
        } else {
            ""
        };
        report.with_help(format!(
            "Define one or more {describe_missing_kind} on `{type_name}`{and_extensions_message}."
        ));
    }
}

fn label_recursive_trace<T>(
    report: &mut CliReport,
    trace: &[Node<T>],
    original_name: &str,
    get_name: impl Fn(&T) -> &str,
) {
    if let Some((cyclical_application, path)) = trace.split_first() {
        let mut prev_name = original_name;
        for node in path.iter().rev() {
            let name = get_name(node);
            report.with_label_opt(
                node.location(),
                format!("`{prev_name}` references `{name}` here..."),
            );
            prev_name = name;
        }

        report.with_label_opt(
            cyclical_application.location(),
            format!("`{prev_name}` circularly references `{original_name}` here"),
        );
    }
}

struct CommaSeparated<'a, It>(&'a It);
impl<'a, T, It> fmt::Display for CommaSeparated<'a, It>
where
    T: fmt::Display,
    &'a It: IntoIterator<Item = T>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut it = self.0.into_iter();
        if let Some(element) = it.next() {
            element.fmt(f)?;
        }
        for element in it {
            f.write_str(", ")?;
            element.fmt(f)?;
        }
        Ok(())
    }
}

/// Formatter that describes a name, or describes an anonymous element if there is no name.
pub(crate) struct NameOrAnon<'a, T> {
    pub name: Option<&'a T>,
    pub if_some_prefix: &'a str,
    pub if_none: &'a str,
}
impl<T> fmt::Display for NameOrAnon<'_, T>
where
    T: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.name {
            Some(name) => write!(f, "{} `{}`", self.if_some_prefix, name),
            None => f.write_str(self.if_none),
        }
    }
}

fn fragment_name_or_inline<T>(name: &'_ Option<T>) -> NameOrAnon<'_, T> {
    NameOrAnon {
        name: name.as_ref(),
        if_some_prefix: "fragment",
        if_none: "inline fragment",
    }
}