brake 2026.8.0

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
use std::collections::{BTreeMap, BTreeSet};

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

use super::{Contract, Endpoint, EndpointKey, Field, Parameter, Payload, Span, TypeRef};

#[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> {
    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 document = tree.document();

    let mut registry = TypeRegistry {
        input_defs: BTreeMap::new(),
        object_defs: BTreeMap::new(),
        enum_defs: BTreeMap::new(),
    };
    let mut query_root = "Query".to_owned();
    let mut mutation_root = "Mutation".to_owned();
    let mut subscription_root = "Subscription".to_owned();

    for definition in document.definitions() {
        match definition {
            cst::Definition::SchemaDefinition(schema) => {
                for root in schema.root_operation_type_definitions() {
                    let Some(op_type) = root.operation_type() else {
                        continue;
                    };
                    let Some(named_type) = root.named_type() else {
                        continue;
                    };
                    let Some(type_name) = extract_named_type_name(&named_type) else {
                        continue;
                    };
                    if op_type.query_token().is_some() {
                        query_root = type_name;
                    } else if op_type.mutation_token().is_some() {
                        mutation_root = type_name;
                    } else if op_type.subscription_token().is_some() {
                        subscription_root = type_name;
                    }
                }
            }
            cst::Definition::EnumTypeDefinition(enum_def) => {
                if let Some(name) = extract_name(enum_def.name()) {
                    registry.enum_defs.entry(name).or_default().extend(
                        enum_def
                            .enum_values_definition()
                            .into_iter()
                            .flat_map(|values| values.enum_value_definitions())
                            .filter_map(|value| value.enum_value())
                            .filter_map(extract_enum_value),
                    );
                }
            }
            cst::Definition::EnumTypeExtension(enum_ext) => {
                if let Some(name) = extract_name(enum_ext.name()) {
                    registry.enum_defs.entry(name).or_default().extend(
                        enum_ext
                            .enum_values_definition()
                            .into_iter()
                            .flat_map(|values| values.enum_value_definitions())
                            .filter_map(|value| value.enum_value())
                            .filter_map(extract_enum_value),
                    );
                }
            }
            cst::Definition::InputObjectTypeDefinition(input_def) => {
                if let Some(name) = extract_name(input_def.name()) {
                    registry.input_defs.entry(name).or_default().extend(
                        input_fields_from_definition(input_def.input_fields_definition()),
                    );
                }
            }
            cst::Definition::InputObjectTypeExtension(input_ext) => {
                if let Some(name) = extract_name(input_ext.name()) {
                    registry.input_defs.entry(name).or_default().extend(
                        input_fields_from_definition(input_ext.input_fields_definition()),
                    );
                }
            }
            cst::Definition::ObjectTypeDefinition(object_def) => {
                if let Some(name) = extract_name(object_def.name()) {
                    registry.object_defs.entry(name).or_default().extend(
                        output_fields_from_definition(object_def.fields_definition()),
                    );
                }
            }
            cst::Definition::ObjectTypeExtension(object_ext) => {
                if let Some(name) = extract_name(object_ext.name()) {
                    registry.object_defs.entry(name).or_default().extend(
                        output_fields_from_definition(object_ext.fields_definition()),
                    );
                }
            }
            _ => {}
        }
    }

    let mut contract = Contract::empty();
    add_root_endpoints(
        &mut contract,
        source,
        "GET",
        "query",
        &query_root,
        &registry,
    );
    add_root_endpoints(
        &mut contract,
        source,
        "POST",
        "mutation",
        &mutation_root,
        &registry,
    );
    add_root_endpoints(
        &mut contract,
        source,
        "SUBSCRIBE",
        "subscription",
        &subscription_root,
        &registry,
    );

    Ok(contract)
}

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

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

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

struct TypeRegistry {
    input_defs: BTreeMap<String, Vec<FieldShape>>,
    object_defs: BTreeMap<String, Vec<FieldShape>>,
    enum_defs: BTreeMap<String, BTreeSet<String>>,
}

fn add_root_endpoints(
    contract: &mut Contract,
    source: &str,
    method: &str,
    root_segment: &str,
    root_name: &str,
    registry: &TypeRegistry,
) {
    let Some(fields) = registry.object_defs.get(root_name) else {
        return;
    };

    for (index, field) in fields.iter().enumerate() {
        let span = Span {
            file: source.to_owned(),
            line: 1,
            column: 1,
            pointer: format!("/{root_segment}/{index}"),
        };
        let operation_id = format!("{root_name}.{}", field.name);
        let path = format!("/{root_segment}/{}", field.name);
        let parameters = field
            .args
            .iter()
            .map(|arg| Parameter {
                name: arg.name.clone(),
                location: "arg".to_owned(),
                required: is_non_null(&arg.ty),
                ty: build_type(unwrap_non_null(&arg.ty), registry, &mut BTreeSet::new()),
            })
            .collect::<Vec<_>>();
        let response_ty = build_type(unwrap_non_null(&field.ty), registry, &mut BTreeSet::new());

        let mut responses = BTreeMap::new();
        responses.insert(
            "200".to_owned(),
            Payload {
                ty: response_ty,
                span: span.clone(),
            },
        );
        contract.endpoints.insert(
            EndpointKey {
                method: method.to_owned(),
                path,
            },
            Endpoint {
                operation_id: Some(operation_id),
                deprecated: false,
                parameters,
                request: None,
                responses,
                security: Vec::new(),
                span,
            },
        );
    }
}

fn build_type(ty: &GraphType, registry: &TypeRegistry, visiting: &mut BTreeSet<String>) -> TypeRef {
    match ty {
        GraphType::List(inner) => TypeRef::Array {
            items: Box::new(build_type(inner, registry, visiting)),
        },
        GraphType::NonNull(inner) => build_type(inner, registry, visiting),
        GraphType::Named(name) => {
            if let Some(values) = registry.enum_defs.get(name) {
                return TypeRef::Enum {
                    values: values.clone(),
                };
            }
            if let Some(fields) = registry.input_defs.get(name) {
                if !visiting.insert(name.clone()) {
                    return TypeRef::Cycle(name.clone());
                }
                let resolved = object_from_fields(fields, registry, visiting);
                visiting.remove(name);
                return resolved;
            }
            if let Some(fields) = registry.object_defs.get(name) {
                if !visiting.insert(name.clone()) {
                    return TypeRef::Cycle(name.clone());
                }
                let resolved = object_from_fields(fields, registry, visiting);
                visiting.remove(name);
                return resolved;
            }
            scalar_for_graphql_name(name, true)
        }
    }
}

fn object_from_fields(
    fields: &[FieldShape],
    registry: &TypeRegistry,
    visiting: &mut BTreeSet<String>,
) -> TypeRef {
    let mut output = BTreeMap::new();
    for field in fields {
        output.insert(
            field.name.clone(),
            Field {
                required: is_non_null(&field.ty),
                ty: build_type(unwrap_non_null(&field.ty), registry, visiting),
            },
        );
    }
    TypeRef::Object {
        fields: output,
        additional: false,
    }
}

fn scalar_for_graphql_name(name: &str, nullable: bool) -> TypeRef {
    match name {
        "Int" => TypeRef::Scalar {
            ty: "integer".to_owned(),
            format: None,
            nullable,
        },
        "Float" => TypeRef::Scalar {
            ty: "number".to_owned(),
            format: None,
            nullable,
        },
        "Boolean" => TypeRef::Scalar {
            ty: "boolean".to_owned(),
            format: None,
            nullable,
        },
        "ID" | "String" => TypeRef::Scalar {
            ty: "string".to_owned(),
            format: None,
            nullable,
        },
        other => TypeRef::Scalar {
            ty: "string".to_owned(),
            format: Some(format!("graphql:{other}")),
            nullable,
        },
    }
}

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

fn extract_named_type_name(named: &cst::NamedType) -> Option<String> {
    extract_name(named.name())
}

fn extract_enum_value(value: cst::EnumValue) -> Option<String> {
    value
        .name()
        .and_then(|name| name.ident_token().map(|token| token.text().to_owned()))
}

fn input_fields_from_definition(definition: Option<cst::InputFieldsDefinition>) -> Vec<FieldShape> {
    definition
        .into_iter()
        .flat_map(|fields| fields.input_value_definitions())
        .filter_map(|value| {
            let name = extract_name(value.name())?;
            let ty = value.ty().map(graph_type_from_cst)?;
            Some(FieldShape {
                name,
                ty,
                args: Vec::new(),
            })
        })
        .collect()
}

fn output_fields_from_definition(definition: Option<cst::FieldsDefinition>) -> Vec<FieldShape> {
    definition
        .into_iter()
        .flat_map(|fields| fields.field_definitions())
        .filter_map(|field| {
            let name = extract_name(field.name())?;
            let ty = field.ty().map(graph_type_from_cst)?;
            let args = field
                .arguments_definition()
                .into_iter()
                .flat_map(|arg_defs| arg_defs.input_value_definitions())
                .filter_map(|arg| {
                    let arg_name = extract_name(arg.name())?;
                    let arg_ty = arg.ty().map(graph_type_from_cst)?;
                    Some(ArgumentShape {
                        name: arg_name,
                        ty: arg_ty,
                    })
                })
                .collect::<Vec<_>>();
            Some(FieldShape { name, ty, args })
        })
        .collect()
}

fn graph_type_from_cst(ty: cst::Type) -> GraphType {
    match ty {
        cst::Type::NamedType(named) => GraphType::Named(
            extract_named_type_name(&named).unwrap_or_else(|| "Unknown".to_owned()),
        ),
        cst::Type::ListType(list) => list
            .ty()
            .map(graph_type_from_cst)
            .map(Box::new)
            .map(GraphType::List)
            .unwrap_or_else(|| GraphType::Named("Unknown".to_owned())),
        cst::Type::NonNullType(non_null) => {
            if let Some(named) = non_null.named_type() {
                return GraphType::NonNull(Box::new(GraphType::Named(
                    extract_named_type_name(&named).unwrap_or_else(|| "Unknown".to_owned()),
                )));
            }
            if let Some(list) = non_null.list_type() {
                let inner = list
                    .ty()
                    .map(graph_type_from_cst)
                    .unwrap_or_else(|| GraphType::Named("Unknown".to_owned()));
                return GraphType::NonNull(Box::new(GraphType::List(Box::new(inner))));
            }
            GraphType::NonNull(Box::new(GraphType::Named("Unknown".to_owned())))
        }
    }
}

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

fn unwrap_non_null(ty: &GraphType) -> &GraphType {
    if let GraphType::NonNull(inner) = ty {
        inner
    } else {
        ty
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn graphql_ingest_extracts_query_fields() {
        let source = br#"
            type Query {
                payment(id: ID!): Payment!
            }

            type Payment {
                id: ID!
                status: String!
            }
        "#;

        let contract = ingest("api/schema.graphql", source).expect("ingest");
        assert_eq!(contract.endpoints.len(), 1);
        let endpoint = contract
            .endpoints
            .get(&EndpointKey {
                method: "GET".to_owned(),
                path: "/query/payment".to_owned(),
            })
            .expect("query endpoint");
        assert_eq!(endpoint.operation_id.as_deref(), Some("Query.payment"));
        assert_eq!(endpoint.parameters.len(), 1);
        assert!(endpoint.responses.contains_key("200"));
        assert!(contract.unmodelled.is_empty());
    }
}