brake 2026.11.3

A brake on breaking API changes. Checks OpenAPI, protobuf and GraphQL contracts for backward compatibility, hermetically.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
//! GraphQL SDL → `Contract`.
//!
//! Nullability is load-bearing in GraphQL and is carried all the way down: a
//! `String!` field relaxed to `String` breaks every consumer whose generated
//! type is non-optional, and that difference is invisible if `NonNull` is
//! unwrapped and discarded. Unions and interfaces are modelled too — removing
//! a union member is a canonical GraphQL break.

use std::collections::{BTreeMap, BTreeSet};

use apollo_parser::{Parser, cst, cst::CstNode};
use thiserror::Error;

use super::{
    Constraints, Contract, Endpoint, EndpointKey, Field, MEDIA_ANY, Parameter, Payload, Span,
    TypeRef, Unmodelled, UnmodelledKind,
};

#[derive(Debug, Error)]
pub enum GraphqlError {
    #[error("contract source `{contract_source}` is not valid UTF-8: {error}")]
    InvalidUtf8 {
        contract_source: String,
        error: std::str::Utf8Error,
    },
    #[error("failed to parse graphql schema `{contract_source}`: {details}")]
    Parse {
        contract_source: String,
        details: String,
    },
}

pub fn ingest(source: &str, bytes: &[u8]) -> Result<Contract, GraphqlError> {
    ingest_with_resolver(source, bytes, &crate::contract::SingleDocumentResolver)
}

pub fn ingest_with_resolver(
    source: &str,
    bytes: &[u8],
    _resolver: &dyn crate::contract::DocumentResolver,
) -> Result<Contract, GraphqlError> {
    let input = std::str::from_utf8(bytes).map_err(|error| GraphqlError::InvalidUtf8 {
        contract_source: source.to_owned(),
        error,
    })?;
    let tree = Parser::new(input).parse();
    let errors = tree.errors().map(ToString::to_string).collect::<Vec<_>>();
    if !errors.is_empty() {
        return Err(GraphqlError::Parse {
            contract_source: source.to_owned(),
            details: errors.join("; "),
        });
    }

    let lines = LineIndex::new(input);
    let mut registry = Registry::default();
    let mut roots = Roots::default();

    for definition in tree.document().definitions() {
        match definition {
            cst::Definition::SchemaDefinition(schema) => roots.absorb(schema),
            cst::Definition::EnumTypeDefinition(node) => {
                if let Some(name) = extract_name(node.name()) {
                    registry
                        .enums
                        .entry(name)
                        .or_default()
                        .extend(enum_values(node.enum_values_definition()));
                }
            }
            cst::Definition::EnumTypeExtension(node) => {
                if let Some(name) = extract_name(node.name()) {
                    registry
                        .enums
                        .entry(name)
                        .or_default()
                        .extend(enum_values(node.enum_values_definition()));
                }
            }
            cst::Definition::InputObjectTypeDefinition(node) => {
                if let Some(name) = extract_name(node.name()) {
                    registry
                        .inputs
                        .entry(name.clone())
                        .or_default()
                        .extend(input_fields(
                            node.input_fields_definition(),
                            &lines,
                            source,
                            &name,
                        ));
                }
            }
            cst::Definition::InputObjectTypeExtension(node) => {
                if let Some(name) = extract_name(node.name()) {
                    registry
                        .inputs
                        .entry(name.clone())
                        .or_default()
                        .extend(input_fields(
                            node.input_fields_definition(),
                            &lines,
                            source,
                            &name,
                        ));
                }
            }
            cst::Definition::ObjectTypeDefinition(node) => {
                if let Some(name) = extract_name(node.name()) {
                    registry
                        .objects
                        .entry(name.clone())
                        .or_default()
                        .extend(output_fields(
                            node.fields_definition(),
                            &lines,
                            source,
                            &name,
                        ));
                }
            }
            cst::Definition::ObjectTypeExtension(node) => {
                if let Some(name) = extract_name(node.name()) {
                    registry
                        .objects
                        .entry(name.clone())
                        .or_default()
                        .extend(output_fields(
                            node.fields_definition(),
                            &lines,
                            source,
                            &name,
                        ));
                }
            }
            // An interface behaves as an object for compatibility purposes:
            // removing a field from it removes that field from every
            // implementor's selection set.
            cst::Definition::InterfaceTypeDefinition(node) => {
                if let Some(name) = extract_name(node.name()) {
                    registry
                        .objects
                        .entry(name.clone())
                        .or_default()
                        .extend(output_fields(
                            node.fields_definition(),
                            &lines,
                            source,
                            &name,
                        ));
                }
            }
            cst::Definition::InterfaceTypeExtension(node) => {
                if let Some(name) = extract_name(node.name()) {
                    registry
                        .objects
                        .entry(name.clone())
                        .or_default()
                        .extend(output_fields(
                            node.fields_definition(),
                            &lines,
                            source,
                            &name,
                        ));
                }
            }
            cst::Definition::UnionTypeDefinition(node) => {
                if let Some(name) = extract_name(node.name()) {
                    registry
                        .unions
                        .entry(name)
                        .or_default()
                        .extend(union_members(node.union_member_types()));
                }
            }
            cst::Definition::UnionTypeExtension(node) => {
                if let Some(name) = extract_name(node.name()) {
                    registry
                        .unions
                        .entry(name)
                        .or_default()
                        .extend(union_members(node.union_member_types()));
                }
            }
            cst::Definition::ScalarTypeDefinition(node) => {
                if let Some(name) = extract_name(node.name()) {
                    registry.custom_scalars.insert(name);
                }
            }
            _ => {}
        }
    }

    let mut contract = Contract::empty();
    for (method, segment, root) in [
        ("QUERY", "query", roots.query.clone()),
        ("MUTATION", "mutation", roots.mutation.clone()),
        ("SUBSCRIPTION", "subscription", roots.subscription.clone()),
    ] {
        add_root_endpoints(&mut contract, source, method, segment, &root, &mut registry);
    }

    contract.unmodelled = std::mem::take(&mut registry.unmodelled);
    contract
        .unmodelled
        .sort_by(|a, b| a.pointer.cmp(&b.pointer).then_with(|| a.kind.cmp(&b.kind)));
    contract.unmodelled.dedup();
    Ok(contract)
}

#[derive(Debug, Clone)]
struct Roots {
    query: String,
    mutation: String,
    subscription: String,
}

impl Default for Roots {
    fn default() -> Self {
        Self {
            query: "Query".to_owned(),
            mutation: "Mutation".to_owned(),
            subscription: "Subscription".to_owned(),
        }
    }
}

impl Roots {
    fn absorb(&mut self, schema: cst::SchemaDefinition) {
        for root in schema.root_operation_type_definitions() {
            let (Some(operation), Some(named)) = (root.operation_type(), root.named_type()) else {
                continue;
            };
            let Some(type_name) = extract_name(named.name()) else {
                continue;
            };
            if operation.query_token().is_some() {
                self.query = type_name;
            } else if operation.mutation_token().is_some() {
                self.mutation = type_name;
            } else if operation.subscription_token().is_some() {
                self.subscription = type_name;
            }
        }
    }
}

#[derive(Debug, Clone)]
struct FieldShape {
    name: String,
    ty: GraphType,
    args: Vec<ArgumentShape>,
    deprecated: bool,
    span: Span,
}

#[derive(Debug, Clone)]
struct ArgumentShape {
    name: String,
    ty: GraphType,
    has_default: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum GraphType {
    Named(String),
    List(Box<GraphType>),
    NonNull(Box<GraphType>),
}

#[derive(Default)]
struct Registry {
    inputs: BTreeMap<String, Vec<FieldShape>>,
    objects: BTreeMap<String, Vec<FieldShape>>,
    enums: BTreeMap<String, BTreeSet<String>>,
    unions: BTreeMap<String, Vec<String>>,
    custom_scalars: BTreeSet<String>,
    unmodelled: Vec<Unmodelled>,
}

impl Registry {
    fn record(&mut self, kind: UnmodelledKind, pointer: &str, span: &Span) -> TypeRef {
        self.unmodelled.push(Unmodelled {
            kind: kind.clone(),
            pointer: pointer.to_owned(),
            span: span.clone(),
        });
        TypeRef::Unknown(kind)
    }
}

fn add_root_endpoints(
    contract: &mut Contract,
    source: &str,
    method: &str,
    segment: &str,
    root_name: &str,
    registry: &mut Registry,
) {
    let Some(fields) = registry.objects.get(root_name).cloned() else {
        return;
    };

    for field in &fields {
        let pointer = format!("/{segment}/{}", field.name);
        let parameters = field
            .args
            .iter()
            .map(|argument| Parameter {
                name: argument.name.clone(),
                location: "arg".to_owned(),
                // An argument with a default is satisfiable without the caller
                // supplying it, so it is not required of them.
                required: is_non_null(&argument.ty) && !argument.has_default,
                deprecated: false,
                ty: build_type(
                    &argument.ty,
                    registry,
                    &mut BTreeSet::new(),
                    &pointer,
                    &field.span,
                ),
                span: field.span.clone(),
            })
            .collect::<Vec<_>>();

        let response_ty = build_type(
            &field.ty,
            registry,
            &mut BTreeSet::new(),
            &pointer,
            &field.span,
        );

        contract.endpoints.insert(
            EndpointKey {
                method: method.to_owned(),
                path: format!("/{segment}/{}", field.name),
            },
            Endpoint {
                operation_id: Some(format!("{root_name}.{}", field.name)),
                deprecated: field.deprecated,
                sunset: None,
                parameters,
                request: None,
                responses: BTreeMap::from([(
                    "200".to_owned(),
                    Payload {
                        media_types: BTreeMap::from([(MEDIA_ANY.to_owned(), response_ty)]),
                        span: Span::new(
                            source,
                            field.span.line,
                            field.span.column,
                            pointer.clone(),
                        ),
                    },
                )]),
                security: Vec::new(),
                span: Span::new(source, field.span.line, field.span.column, pointer),
            },
        );
    }
}

/// Build a `TypeRef`, keeping nullability at every level.
///
/// `NonNull` is not discarded: `[String!]!` and `[String]!` differ in a way a
/// consumer feels, and collapsing them hides a real break.
fn build_type(
    ty: &GraphType,
    registry: &mut Registry,
    visiting: &mut BTreeSet<String>,
    pointer: &str,
    span: &Span,
) -> TypeRef {
    match ty {
        GraphType::NonNull(inner) => {
            set_nullable(build_type(inner, registry, visiting, pointer, span), false)
        }
        GraphType::List(inner) => TypeRef::Array {
            items: Box::new(build_type(inner, registry, visiting, pointer, span)),
            nullable: true,
        },
        GraphType::Named(name) => {
            // Everything named is nullable unless a NonNull wraps it.
            let resolved = named_type(name, registry, visiting, pointer, span);
            set_nullable(resolved, true)
        }
    }
}

fn named_type(
    name: &str,
    registry: &mut Registry,
    visiting: &mut BTreeSet<String>,
    pointer: &str,
    span: &Span,
) -> TypeRef {
    if let Some(values) = registry.enums.get(name).cloned() {
        return TypeRef::Enum {
            values,
            numbers: BTreeMap::new(),
        };
    }

    // A union is a closed set of possible results. Removing a member is a
    // break for a consumer whose selection set names it, and modelling the
    // union as an opaque scalar made that invisible.
    if let Some(members) = registry.unions.get(name).cloned() {
        if !visiting.insert(name.to_owned()) {
            return TypeRef::Cycle(name.to_owned());
        }
        // A union member is discriminated by `__typename`, which is how a
        // consumer's `... on Refund` selection actually works. Modelling it
        // keeps two structurally identical members distinct — without it,
        // `Payment | Invoice` and `Payment | Invoice | Refund` compare equal
        // whenever the members happen to have the same fields.
        let variants = members
            .iter()
            .map(|member| {
                let resolved = named_type(member, registry, visiting, pointer, span);
                match resolved {
                    TypeRef::Object {
                        mut fields,
                        additional,
                        nullable,
                    } => {
                        fields.insert(
                            "__typename".to_owned(),
                            Field {
                                ty: TypeRef::Enum {
                                    values: BTreeSet::from([member.clone()]),
                                    numbers: BTreeMap::new(),
                                },
                                required: true,
                                deprecated: false,
                                number: None,
                                // Synthetic: the discriminant is not written
                                // anywhere in the schema.
                                span: None,
                            },
                        );
                        TypeRef::Object {
                            fields,
                            additional,
                            nullable,
                        }
                    }
                    other => other,
                }
            })
            .collect();
        visiting.remove(name);
        return TypeRef::OneOf {
            variants,
            discriminator: None,
        };
    }

    let defined = registry
        .inputs
        .get(name)
        .or_else(|| registry.objects.get(name))
        .cloned();
    {
        if let Some(fields) = defined {
            if !visiting.insert(name.to_owned()) {
                return TypeRef::Cycle(name.to_owned());
            }
            let mut modelled = BTreeMap::new();
            for field in &fields {
                modelled.insert(
                    field.name.clone(),
                    Field {
                        required: is_non_null(&field.ty),
                        ty: build_type(&field.ty, registry, visiting, pointer, span),
                        deprecated: field.deprecated,
                        number: None,
                        span: Some(field.span.clone()),
                    },
                );
            }
            visiting.remove(name);
            return TypeRef::Object {
                fields: modelled,
                additional: false,
                nullable: true,
            };
        }
    }

    match name {
        "Int" => scalar("integer", None),
        "Float" => scalar("number", None),
        "Boolean" => scalar("boolean", None),
        "String" => scalar("string", None),
        "ID" => scalar("string", Some("ID")),
        custom if registry.custom_scalars.contains(custom) => {
            scalar("string", Some(&format!("graphql:{custom}")))
        }
        // A type that is used but never defined is a schema this ingester was
        // not given all of. Reporting it beats inventing a scalar for it.
        unknown => registry.record(
            UnmodelledKind::ExternalRef(unknown.to_owned()),
            &format!("{pointer}/{unknown}"),
            span,
        ),
    }
}

fn scalar(ty: &str, format: Option<&str>) -> TypeRef {
    TypeRef::Scalar {
        ty: ty.to_owned(),
        format: format.map(ToOwned::to_owned),
        nullable: true,
        constraints: Constraints::default(),
    }
}

fn set_nullable(ty: TypeRef, nullable: bool) -> TypeRef {
    match ty {
        TypeRef::Scalar {
            ty,
            format,
            constraints,
            ..
        } => TypeRef::Scalar {
            ty,
            format,
            nullable,
            constraints,
        },
        TypeRef::Array { items, .. } => TypeRef::Array { items, nullable },
        TypeRef::Object {
            fields, additional, ..
        } => TypeRef::Object {
            fields,
            additional,
            nullable,
        },
        other => other,
    }
}

fn is_non_null(ty: &GraphType) -> bool {
    matches!(ty, GraphType::NonNull(_))
}

fn enum_values(definition: Option<cst::EnumValuesDefinition>) -> BTreeSet<String> {
    definition
        .into_iter()
        .flat_map(|values| values.enum_value_definitions())
        .filter_map(|value| value.enum_value())
        .filter_map(|value| extract_name(value.name()))
        .collect()
}

fn union_members(members: Option<cst::UnionMemberTypes>) -> Vec<String> {
    members
        .into_iter()
        .flat_map(|types| types.named_types())
        .filter_map(|named| extract_name(named.name()))
        .collect()
}

fn input_fields(
    definition: Option<cst::InputFieldsDefinition>,
    lines: &LineIndex,
    source: &str,
    owner: &str,
) -> Vec<FieldShape> {
    definition
        .into_iter()
        .flat_map(|fields| fields.input_value_definitions())
        .filter_map(|value| {
            let name = extract_name(value.name())?;
            let offset: usize = value.syntax().text_range().start().into();
            Some(FieldShape {
                ty: convert_type(value.ty()?)?,
                args: Vec::new(),
                deprecated: has_deprecated_directive(value.directives()),
                span: lines.span(source, offset, &format!("/{owner}/{name}")),
                name,
            })
        })
        .collect()
}

fn output_fields(
    definition: Option<cst::FieldsDefinition>,
    lines: &LineIndex,
    source: &str,
    owner: &str,
) -> Vec<FieldShape> {
    definition
        .into_iter()
        .flat_map(|fields| fields.field_definitions())
        .filter_map(|field| {
            let name = extract_name(field.name())?;
            let offset: usize = field.syntax().text_range().start().into();
            Some(FieldShape {
                ty: convert_type(field.ty()?)?,
                args: field
                    .arguments_definition()
                    .into_iter()
                    .flat_map(|arguments| arguments.input_value_definitions())
                    .filter_map(|argument| {
                        Some(ArgumentShape {
                            name: extract_name(argument.name())?,
                            ty: convert_type(argument.ty()?)?,
                            has_default: argument.default_value().is_some(),
                        })
                    })
                    .collect(),
                deprecated: has_deprecated_directive(field.directives()),
                span: lines.span(source, offset, &format!("/{owner}/{name}")),
                name,
            })
        })
        .collect()
}

fn has_deprecated_directive(directives: Option<cst::Directives>) -> bool {
    directives
        .into_iter()
        .flat_map(|directives| directives.directives())
        .filter_map(|directive| extract_name(directive.name()))
        .any(|name| name == "deprecated")
}

fn convert_type(ty: cst::Type) -> Option<GraphType> {
    match ty {
        cst::Type::NamedType(named) => Some(GraphType::Named(extract_name(named.name())?)),
        cst::Type::ListType(list) => Some(GraphType::List(Box::new(convert_type(list.ty()?)?))),
        cst::Type::NonNullType(non_null) => {
            if let Some(named) = non_null.named_type() {
                return Some(GraphType::NonNull(Box::new(GraphType::Named(
                    extract_name(named.name())?,
                ))));
            }
            let list = non_null.list_type()?;
            Some(GraphType::NonNull(Box::new(GraphType::List(Box::new(
                convert_type(list.ty()?)?,
            )))))
        }
    }
}

fn extract_name(name: Option<cst::Name>) -> Option<String> {
    name.and_then(|node| node.ident_token().map(|token| token.text().to_owned()))
}

/// Byte offset → line and column, so a GraphQL finding points at the field
/// rather than at line 1.
struct LineIndex {
    starts: Vec<usize>,
}

impl LineIndex {
    fn new(input: &str) -> Self {
        let mut starts = vec![0];
        for (offset, byte) in input.bytes().enumerate() {
            if byte == b'\n' {
                starts.push(offset + 1);
            }
        }
        Self { starts }
    }

    fn span(&self, file: &str, offset: usize, pointer: &str) -> Span {
        let line = self.starts.partition_point(|start| *start <= offset);
        let column = offset - self.starts[line.saturating_sub(1)] + 1;
        Span::new(file, line.max(1), column, pointer)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compare::{ChangeKind, compare_contracts};

    fn kinds(base: &str, head: &str) -> Vec<ChangeKind> {
        let base = ingest("api/schema.graphql", base.as_bytes()).expect("base");
        let head = ingest("api/schema.graphql", head.as_bytes()).expect("head");
        compare_contracts(&base, &head)
            .iter()
            .map(|change| change.kind)
            .collect()
    }

    const BASE: &str = r#"
type Query {
  payment(id: ID!): Payment!
  search(q: String!): SearchResult!
}
union SearchResult = Payment | Invoice
type Payment { id: ID! amount: Int! tags: [String!]! }
type Invoice { id: ID! }
"#;

    #[test]
    fn identical_schemas_produce_nothing() {
        assert!(kinds(BASE, BASE).is_empty());
    }

    #[test]
    fn removing_a_union_member_is_a_break() {
        let head = BASE.replace(
            "union SearchResult = Payment | Invoice",
            "union SearchResult = Payment",
        );
        let kinds = kinds(BASE, &head);
        assert!(
            kinds.contains(&ChangeKind::ResponseTypeChanged),
            "a removed union member breaks every consumer selecting it: {kinds:?}"
        );
    }

    #[test]
    fn adding_a_union_member_is_dangerous_not_silent() {
        let head = BASE.replace(
            "union SearchResult = Payment | Invoice",
            "union SearchResult = Payment | Invoice | Refund",
        ) + "\ntype Refund { id: ID! }\n";
        assert!(kinds(BASE, &head).contains(&ChangeKind::ResponseEnumExtended));
    }

    #[test]
    fn relaxing_a_non_null_output_field_is_a_break() {
        let head = BASE.replace(
            "type Payment { id: ID! amount: Int! tags: [String!]! }",
            "type Payment { id: ID! amount: Int tags: [String!]! }",
        );
        let kinds = kinds(BASE, &head);
        assert!(
            kinds.contains(&ChangeKind::ResponseFieldOptional)
                || kinds.contains(&ChangeKind::ResponseTypeChanged),
            "Int! relaxed to Int breaks a non-optional consumer type: {kinds:?}"
        );
    }

    #[test]
    fn relaxing_nullability_inside_a_list_is_a_break() {
        let head = BASE.replace("tags: [String!]!", "tags: [String]!");
        let kinds = kinds(BASE, &head);
        assert!(
            kinds.contains(&ChangeKind::ResponseTypeChanged),
            "[String!]! to [String]! may now yield nulls in the list: {kinds:?}"
        );
    }

    #[test]
    fn requiring_a_previously_optional_argument_is_a_break() {
        let head = BASE.replace("payment(id: ID!)", "payment(id: ID!, mode: String!)");
        assert!(kinds(BASE, &head).contains(&ChangeKind::ParamAddedRequired));
    }

    #[test]
    fn an_argument_with_a_default_is_not_required_of_the_caller() {
        let head = BASE.replace(
            "payment(id: ID!)",
            "payment(id: ID!, mode: String! = \"fast\")",
        );
        let kinds = kinds(BASE, &head);
        assert!(
            !kinds.contains(&ChangeKind::ParamAddedRequired),
            "a defaulted argument does not have to be supplied: {kinds:?}"
        );
    }

    #[test]
    fn removing_a_query_field_is_an_endpoint_removal() {
        let head = "type Query { search(q: String!): String! }";
        assert!(kinds(BASE, head).contains(&ChangeKind::EndpointRemoved));
    }

    #[test]
    fn interface_fields_are_modelled() {
        let base = r#"
type Query { node(id: ID!): Node! }
interface Node { id: ID! label: String! }
"#;
        let head = r#"
type Query { node(id: ID!): Node! }
interface Node { id: ID! }
"#;
        assert!(kinds(base, head).contains(&ChangeKind::ResponseFieldRemoved));
    }

    #[test]
    fn a_deprecated_field_removal_skips_the_hygiene_rule() {
        let base = r#"
type Query {
  legacy: String! @deprecated(reason: "use modern")
  modern: String!
}
"#;
        let head = "type Query { modern: String! }";
        let kinds = kinds(base, head);
        assert!(kinds.contains(&ChangeKind::EndpointRemoved));
        assert!(!kinds.contains(&ChangeKind::RemovedWithoutDeprecation));
    }

    #[test]
    fn spans_point_at_the_field_not_line_one() {
        let contract = ingest("api/schema.graphql", BASE.as_bytes()).expect("ingest");
        let endpoint = &contract.endpoints[&EndpointKey {
            method: "QUERY".to_owned(),
            path: "/query/search".to_owned(),
        }];
        assert_eq!(
            endpoint.span.line, 4,
            "the `search` field is on line 4 of the fixture"
        );
    }

    #[test]
    fn a_custom_scalar_is_modelled_and_an_undefined_type_is_reported() {
        let schema = r#"
scalar DateTime
type Query { at: DateTime! missing: Mystery! }
"#;
        let contract = ingest("api/schema.graphql", schema.as_bytes()).expect("ingest");
        assert!(
            !contract.unmodelled.is_empty(),
            "an undefined type must not be assumed to be a string"
        );
    }

    #[test]
    fn mutations_and_subscriptions_become_endpoints() {
        let schema = r#"
type Query { a: String! }
type Mutation { pay(id: ID!): String! }
type Subscription { paid: String! }
"#;
        let contract = ingest("api/schema.graphql", schema.as_bytes()).expect("ingest");
        let methods = contract
            .endpoints
            .keys()
            .map(|key| key.method.as_str())
            .collect::<BTreeSet<_>>();
        assert!(methods.contains("QUERY"));
        assert!(methods.contains("MUTATION"));
        assert!(methods.contains("SUBSCRIPTION"));
    }

    #[test]
    fn a_custom_schema_block_redirects_the_roots() {
        let schema = r#"
schema { query: RootQuery }
type RootQuery { a: String! }
"#;
        let contract = ingest("api/schema.graphql", schema.as_bytes()).expect("ingest");
        assert!(contract.endpoints.contains_key(&EndpointKey {
            method: "QUERY".to_owned(),
            path: "/query/a".to_owned(),
        }));
    }
}