panproto-protocols 0.74.1

Built-in protocol definitions for panproto
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
//! GraphQL protocol definition.
//!
//! GraphQL uses a constrained multigraph with interfaces:
//! `colimit(ThGraph, ThConstraint, ThMulti, ThInterface)`.
//! Instance theory: `ThWType`.
//!
//! Vertex kinds: type, field, interface, union, enum, scalar, input, enum-value, subscription.
//! Edge kinds: field-of, implements, member-of, type-of.

use std::collections::HashMap;
use std::hash::BuildHasher;

use panproto_gat::Theory;
use panproto_schema::{EdgeRule, Protocol, Schema, SchemaBuilder};

use crate::error::ProtocolError;
use crate::theories;

/// Returns the GraphQL protocol definition.
///
/// Schema theory: `colimit(ThGraph, ThConstraint, ThMulti, ThInterface)`.
/// Instance theory: `ThWType`.
#[must_use]
pub fn protocol() -> Protocol {
    Protocol {
        name: "graphql".into(),
        schema_theory: "ThGraphQLSchema".into(),
        instance_theory: "ThGraphQLInstance".into(),
        edge_rules: edge_rules(),
        obj_kinds: vec![
            "type".into(),
            "interface".into(),
            "input".into(),
            "field".into(),
            "union".into(),
            "enum".into(),
            "scalar".into(),
            "enum-value".into(),
            "subscription".into(),
        ],
        constraint_sorts: vec!["non_null".into(), "list".into(), "deprecated".into()],
        has_order: true,
        has_coproducts: true,
        has_recursion: true,
        nominal_identity: true,
        ..Protocol::default()
    }
}

/// Register the component GATs for GraphQL with a theory registry.
pub fn register_theories<S: BuildHasher>(registry: &mut HashMap<String, Theory, S>) {
    theories::register_typed_graph_wtype(registry, "ThGraphQLSchema", "ThGraphQLInstance");
}

/// Parse a GraphQL SDL string into a [`Schema`].
///
/// Uses a two-pass approach to resolve forward references:
/// - **Pass 1**: Parse all type, interface, input, enum, union, scalar, and
///   subscription declarations, creating vertices and field-of edges.
/// - **Pass 2**: Resolve cross-type references -- implements edges, type-of
///   edges for fields, and member-of edges for union members.
///
/// Also handles `extend type`, `schema { ... }`, and `directive` declarations.
///
/// # Errors
///
/// Returns [`ProtocolError`] if parsing or schema construction fails.
#[allow(clippy::too_many_lines)]
pub fn parse_sdl(sdl: &str) -> Result<Schema, ProtocolError> {
    let proto = protocol();

    // -----------------------------------------------------------------------
    // Pass 1: Collect all declarations (vertices + field-of edges).
    // We also record deferred cross-references to resolve in pass 2.
    // -----------------------------------------------------------------------
    let lines: Vec<&str> = sdl.lines().collect();
    let mut i = 0;

    let mut deferred: Vec<Deferred> = Vec::new();
    // Track all vertex IDs so we can look them up in pass 2.
    let mut vertex_ids: std::collections::HashSet<String> = std::collections::HashSet::new();

    let mut builder = SchemaBuilder::new(&proto);

    while i < lines.len() {
        let trimmed = lines[i].trim();

        if trimmed.starts_with("extend type ") {
            let (new_builder, new_i, new_deferred) =
                parse_extend_type(builder, &lines, i, &mut vertex_ids)?;
            builder = new_builder;
            deferred.extend(new_deferred);
            i = new_i;
        } else if trimmed.starts_with("type ") {
            // Determine kind: "type" or "subscription" for type Subscription.
            let name = trimmed
                .strip_prefix("type ")
                .and_then(|s| s.split(|c: char| c == '{' || c.is_whitespace()).next())
                .unwrap_or("")
                .trim();
            let kind = if name == "Subscription" {
                "subscription"
            } else {
                "type"
            };
            let (new_builder, new_i, new_deferred) =
                parse_type_def(builder, &lines, i, kind, &mut vertex_ids)?;
            builder = new_builder;
            deferred.extend(new_deferred);
            i = new_i;
        } else if trimmed.starts_with("interface ") {
            let (new_builder, new_i, new_deferred) =
                parse_type_def(builder, &lines, i, "interface", &mut vertex_ids)?;
            builder = new_builder;
            deferred.extend(new_deferred);
            i = new_i;
        } else if trimmed.starts_with("input ") {
            let (new_builder, new_i, new_deferred) =
                parse_type_def(builder, &lines, i, "input", &mut vertex_ids)?;
            builder = new_builder;
            deferred.extend(new_deferred);
            i = new_i;
        } else if trimmed.starts_with("enum ") {
            let (new_builder, new_i) = parse_enum_def(builder, &lines, i, &mut vertex_ids)?;
            builder = new_builder;
            i = new_i;
        } else if trimmed.starts_with("union ") {
            let (new_builder, new_deferred) = parse_union_def(builder, trimmed, &mut vertex_ids)?;
            builder = new_builder;
            deferred.extend(new_deferred);
            i += 1;
        } else if trimmed.starts_with("scalar ") {
            let name = trimmed.strip_prefix("scalar ").unwrap_or("").trim();
            if !name.is_empty() {
                builder = builder.vertex(name, "scalar", None)?;
                vertex_ids.insert(name.to_owned());
            }
            i += 1;
        } else if trimmed.starts_with("schema ") || trimmed == "schema" || trimmed == "schema {" {
            // Parse schema definition: `schema { query: Query, mutation: Mutation }`.
            // Skip it gracefully (the root types are already parsed as type declarations).
            i += 1;
            while i < lines.len() {
                let line = lines[i].trim();
                if line == "}" || line.starts_with('}') {
                    i += 1;
                    break;
                }
                i += 1;
            }
        } else if trimmed.starts_with("directive ") {
            // Directive definitions: skip gracefully, consuming any multi-line block.
            i += 1;
            if trimmed.contains('{') && !trimmed.contains('}') {
                while i < lines.len() {
                    let line = lines[i].trim();
                    if line == "}" || line.starts_with('}') {
                        i += 1;
                        break;
                    }
                    i += 1;
                }
            }
        } else {
            i += 1;
        }
    }

    // -----------------------------------------------------------------------
    // Pass 2: Resolve deferred cross-references.
    // -----------------------------------------------------------------------
    for d in deferred {
        match d.kind {
            DeferredKind::Implements { type_name, iface } => {
                if vertex_ids.contains(&iface) {
                    builder = builder.edge(&type_name, &iface, "implements", None)?;
                }
            }
            DeferredKind::TypeOf {
                field_id,
                type_name,
            } => {
                if vertex_ids.contains(&type_name) {
                    builder = builder.edge(&field_id, &type_name, "type-of", None)?;
                }
            }
            DeferredKind::MemberOf { union_name, member } => {
                if vertex_ids.contains(&member) {
                    builder = builder.edge(&union_name, &member, "member-of", Some(&member))?;
                }
            }
        }
    }

    let schema = builder.build()?;
    Ok(schema)
}

/// Deferred cross-type reference to resolve in pass 2.
struct Deferred {
    kind: DeferredKind,
}

/// The kind of deferred reference.
enum DeferredKind {
    /// `type Foo implements Bar` -- add "implements" edge from Foo to Bar.
    Implements {
        /// The type implementing the interface.
        type_name: String,
        /// The interface being implemented.
        iface: String,
    },
    /// Field `Parent.field` has type `TypeName` -- add "type-of" edge.
    TypeOf {
        /// The field vertex ID (e.g. `"Query.user"`).
        field_id: String,
        /// The referenced type name.
        type_name: String,
    },
    /// `union Foo = A | B | C` -- add "member-of" edge from union to member.
    MemberOf {
        /// The union vertex name.
        union_name: String,
        /// The member type name.
        member: String,
    },
}

/// Extract the base type name from a GraphQL type expression.
///
/// Strips `!` (non-null) and `[`/`]` (list) wrappers to return the
/// underlying named type. For example, `[String!]!` returns `String`.
fn extract_base_type(type_expr: &str) -> &str {
    let s = type_expr.trim();
    let s = s.trim_end_matches('!');
    let s = s.trim_start_matches('[');
    let s = s.trim_end_matches(']');
    let s = s.trim_end_matches('!');
    s.trim()
}

/// Parse a type/interface/input/subscription definition.
///
/// Returns the updated builder, the next line index, and any deferred
/// cross-type references (implements and type-of edges).
fn parse_type_def(
    mut builder: SchemaBuilder,
    lines: &[&str],
    start: usize,
    kind: &str,
    vertex_ids: &mut std::collections::HashSet<String>,
) -> Result<(SchemaBuilder, usize, Vec<Deferred>), ProtocolError> {
    let trimmed = lines[start].trim();

    // Determine the keyword to strip from the line.
    let keyword = match kind {
        "type" | "subscription" => "type ",
        "interface" => "interface ",
        "input" => "input ",
        _ => return Err(ProtocolError::Parse(format!("unknown kind: {kind}"))),
    };

    let after_keyword = trimmed
        .strip_prefix(keyword)
        .ok_or_else(|| ProtocolError::Parse(format!("expected {keyword}")))?;

    // Handle "type Name implements Iface {"
    let name = after_keyword
        .split(|c: char| c == '{' || c.is_whitespace())
        .next()
        .unwrap_or("")
        .trim();

    if name.is_empty() {
        return Err(ProtocolError::Parse("empty type name".into()));
    }

    builder = builder.vertex(name, kind, None)?;
    vertex_ids.insert(name.to_owned());

    let mut deferred = Vec::new();

    // Extract implements interfaces (e.g. "type Foo implements Bar & Baz {")
    if let Some(impl_idx) = after_keyword.find("implements") {
        let after_impl = &after_keyword[impl_idx + "implements".len()..];
        let before_brace = after_impl.split('{').next().unwrap_or("");
        for iface_name in before_brace.split('&') {
            let iface = iface_name.trim();
            if !iface.is_empty() {
                deferred.push(Deferred {
                    kind: DeferredKind::Implements {
                        type_name: name.to_owned(),
                        iface: iface.to_owned(),
                    },
                });
            }
        }
    }

    let mut i = start + 1;
    while i < lines.len() {
        let line = lines[i].trim();
        if line == "}" || line.starts_with('}') {
            return Ok((builder, i + 1, deferred));
        }

        // Parse field: name: Type or name(args): Type
        if !line.is_empty() && !line.starts_with('#') {
            if let Some(colon_idx) = line.find(':') {
                let field_name = line[..colon_idx].split('(').next().unwrap_or("").trim();

                if !field_name.is_empty() {
                    let field_id = format!("{name}.{field_name}");
                    builder = builder.vertex(&field_id, "field", None)?;
                    vertex_ids.insert(field_id.clone());
                    builder = builder.edge(name, &field_id, "field-of", Some(field_name))?;

                    // Extract field type for deferred type-of edge
                    let type_expr = line[colon_idx + 1..].trim();
                    let base_type = extract_base_type(type_expr);
                    if !base_type.is_empty() {
                        deferred.push(Deferred {
                            kind: DeferredKind::TypeOf {
                                field_id,
                                type_name: base_type.to_owned(),
                            },
                        });
                    }
                }
            }
        }

        i += 1;
    }

    Ok((builder, i, deferred))
}

/// Parse `extend type Foo { ... }` by adding fields to the existing type.
fn parse_extend_type(
    mut builder: SchemaBuilder,
    lines: &[&str],
    start: usize,
    vertex_ids: &mut std::collections::HashSet<String>,
) -> Result<(SchemaBuilder, usize, Vec<Deferred>), ProtocolError> {
    let trimmed = lines[start].trim();
    let after = trimmed
        .strip_prefix("extend type ")
        .ok_or_else(|| ProtocolError::Parse("expected extend type".into()))?;

    let name = after
        .split(|c: char| c == '{' || c.is_whitespace())
        .next()
        .unwrap_or("")
        .trim();

    if name.is_empty() {
        return Err(ProtocolError::Parse("empty extend type name".into()));
    }

    // If the type doesn't exist yet, create it.
    if !vertex_ids.contains(name) {
        builder = builder.vertex(name, "type", None)?;
        vertex_ids.insert(name.to_owned());
    }

    let mut deferred = Vec::new();
    let mut i = start + 1;
    while i < lines.len() {
        let line = lines[i].trim();
        if line == "}" || line.starts_with('}') {
            return Ok((builder, i + 1, deferred));
        }

        if !line.is_empty() && !line.starts_with('#') {
            if let Some(colon_idx) = line.find(':') {
                let field_name = line[..colon_idx].split('(').next().unwrap_or("").trim();

                if !field_name.is_empty() {
                    let field_id = format!("{name}.{field_name}");
                    builder = builder.vertex(&field_id, "field", None)?;
                    vertex_ids.insert(field_id.clone());
                    builder = builder.edge(name, &field_id, "field-of", Some(field_name))?;

                    let type_expr = line[colon_idx + 1..].trim();
                    let base_type = extract_base_type(type_expr);
                    if !base_type.is_empty() {
                        deferred.push(Deferred {
                            kind: DeferredKind::TypeOf {
                                field_id,
                                type_name: base_type.to_owned(),
                            },
                        });
                    }
                }
            }
        }

        i += 1;
    }

    Ok((builder, i, deferred))
}

/// Parse an enum definition.
fn parse_enum_def(
    mut builder: SchemaBuilder,
    lines: &[&str],
    start: usize,
    vertex_ids: &mut std::collections::HashSet<String>,
) -> Result<(SchemaBuilder, usize), ProtocolError> {
    let trimmed = lines[start].trim();
    let name = trimmed
        .strip_prefix("enum ")
        .and_then(|s| s.split('{').next())
        .map(str::trim)
        .ok_or_else(|| ProtocolError::Parse("invalid enum declaration".into()))?;

    builder = builder.vertex(name, "enum", None)?;
    vertex_ids.insert(name.to_owned());

    let mut i = start + 1;
    while i < lines.len() {
        let line = lines[i].trim();
        if line == "}" || line.starts_with('}') {
            return Ok((builder, i + 1));
        }
        // Enum values are simple identifiers.
        if !line.is_empty() && !line.starts_with('#') {
            let val_name = line.trim();
            let val_id = format!("{name}.{val_name}");
            builder = builder.vertex(&val_id, "enum-value", None)?;
            vertex_ids.insert(val_id.clone());
            builder = builder.edge(name, &val_id, "member-of", Some(val_name))?;
        }
        i += 1;
    }

    Ok((builder, i))
}

/// Parse a union definition (single line: `union Foo = A | B | C`).
///
/// Returns the updated builder and deferred member-of edges.
fn parse_union_def(
    mut builder: SchemaBuilder,
    line: &str,
    vertex_ids: &mut std::collections::HashSet<String>,
) -> Result<(SchemaBuilder, Vec<Deferred>), ProtocolError> {
    let after = line
        .strip_prefix("union ")
        .ok_or_else(|| ProtocolError::Parse("expected union".into()))?;

    let parts: Vec<&str> = after.splitn(2, '=').collect();
    if parts.len() < 2 {
        return Err(ProtocolError::Parse("invalid union syntax".into()));
    }

    let name = parts[0].trim();
    builder = builder.vertex(name, "union", None)?;
    vertex_ids.insert(name.to_owned());

    let mut deferred = Vec::new();
    for member in parts[1].split('|') {
        let member_name = member.trim();
        if !member_name.is_empty() {
            deferred.push(Deferred {
                kind: DeferredKind::MemberOf {
                    union_name: name.to_owned(),
                    member: member_name.to_owned(),
                },
            });
        }
    }

    Ok((builder, deferred))
}

/// Emit a [`Schema`] as a GraphQL SDL string.
///
/// Reconstructs type, interface, input, enum, union, scalar, and
/// subscription declarations from the schema graph.
///
/// # Errors
///
/// Returns [`ProtocolError::Emit`] if the schema cannot be serialized.
pub fn emit_sdl(schema: &Schema) -> Result<String, ProtocolError> {
    use crate::emit::{IndentWriter, find_roots};

    let structural = &["field-of", "member-of", "implements", "type-of"];
    let roots = find_roots(schema, structural);

    let mut w = IndentWriter::new("  ");

    for root in &roots {
        match root.kind.as_str() {
            "scalar" => {
                w.line(&format!("scalar {}", root.id));
                w.blank();
            }
            "type" | "subscription" | "interface" | "input" => {
                emit_graphql_type_def(schema, root, &mut w);
            }
            "enum" => {
                emit_graphql_enum(schema, root, &mut w);
            }
            "union" => {
                emit_graphql_union(schema, root, &mut w);
            }
            _ => {}
        }
    }

    Ok(w.finish())
}

/// Emit a type/interface/input/subscription definition.
fn emit_graphql_type_def(
    schema: &Schema,
    vertex: &panproto_schema::Vertex,
    w: &mut crate::emit::IndentWriter,
) {
    use crate::emit::children_by_edge;

    let keyword = match vertex.kind.as_str() {
        "interface" => "interface",
        "input" => "input",
        _ => "type",
    };

    // Check for implements edges.
    let implements_edges: Vec<_> = schema
        .outgoing_edges(&vertex.id)
        .iter()
        .filter(|e| e.kind == "implements")
        .collect();

    let implements_str = if implements_edges.is_empty() {
        String::new()
    } else {
        let ifaces: Vec<&str> = implements_edges.iter().map(|e| e.tgt.as_str()).collect();
        format!(" implements {}", ifaces.join(" & "))
    };

    w.line(&format!("{keyword} {}{implements_str} {{", vertex.id));
    w.indent();

    let fields = children_by_edge(schema, &vertex.id, "field-of");
    for (edge, field_vertex) in &fields {
        let field_name = edge.name.as_deref().unwrap_or(&field_vertex.id);

        // Determine the type expression.
        let type_expr = resolve_graphql_type_expr(schema, field_vertex);
        w.line(&format!("{field_name}: {type_expr}"));
    }

    w.dedent();
    w.line("}");
    w.blank();
}

/// Resolve the GraphQL type expression for a field, including `non_null` and list wrappers.
fn resolve_graphql_type_expr(schema: &Schema, field_vertex: &panproto_schema::Vertex) -> String {
    use crate::emit::{children_by_edge, constraint_value};

    // The field's declared type is the target of its single `type-of` edge,
    // if any; a field with none falls back to a scalar named by its kind.
    let base = children_by_edge(schema, &field_vertex.id, "type-of")
        .first()
        .map(|(_, v)| *v)
        .map_or_else(
            || {
                match field_vertex.kind.as_str() {
                    "integer" => "Int",
                    "boolean" => "Boolean",
                    "float" => "Float",
                    _ => "String",
                }
                .to_string()
            },
            |type_vertex| type_vertex.id.to_string(),
        );

    let is_list = constraint_value(schema, &field_vertex.id, "list").is_some_and(|v| v == "true");
    let is_non_null =
        constraint_value(schema, &field_vertex.id, "non_null").is_some_and(|v| v == "true");

    let mut result = base;
    if is_list {
        result = format!("[{result}]");
    }
    if is_non_null {
        result = format!("{result}!");
    }
    result
}

/// Emit an enum definition.
fn emit_graphql_enum(
    schema: &Schema,
    vertex: &panproto_schema::Vertex,
    w: &mut crate::emit::IndentWriter,
) {
    use crate::emit::children_by_edge;

    w.line(&format!("enum {} {{", vertex.id));
    w.indent();

    let members = children_by_edge(schema, &vertex.id, "member-of");
    for (edge, _member_vertex) in &members {
        let val_name = edge.name.as_deref().unwrap_or("UNKNOWN");
        w.line(val_name);
    }

    w.dedent();
    w.line("}");
    w.blank();
}

/// Emit a union definition.
fn emit_graphql_union(
    schema: &Schema,
    vertex: &panproto_schema::Vertex,
    w: &mut crate::emit::IndentWriter,
) {
    use crate::emit::children_by_edge;

    let members = children_by_edge(schema, &vertex.id, "member-of");
    let member_names: Vec<&str> = members
        .iter()
        .filter_map(|(edge, _)| edge.name.as_deref())
        .collect();

    w.line(&format!(
        "union {} = {}",
        vertex.id,
        member_names.join(" | ")
    ));
    w.blank();
}

/// Well-formedness rules for GraphQL edges.
fn edge_rules() -> Vec<EdgeRule> {
    vec![
        EdgeRule {
            edge_kind: "field-of".into(),
            src_kinds: vec![
                "type".into(),
                "interface".into(),
                "input".into(),
                "subscription".into(),
            ],
            tgt_kinds: vec!["field".into()],
        },
        EdgeRule {
            edge_kind: "implements".into(),
            src_kinds: vec!["type".into(), "subscription".into()],
            tgt_kinds: vec!["interface".into()],
        },
        EdgeRule {
            edge_kind: "member-of".into(),
            src_kinds: vec!["union".into(), "enum".into()],
            tgt_kinds: vec![],
        },
        EdgeRule {
            edge_kind: "type-of".into(),
            src_kinds: vec!["field".into()],
            tgt_kinds: vec![],
        },
    ]
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn protocol_creates_valid_definition() {
        let p = protocol();
        assert_eq!(p.name, "graphql");
        assert_eq!(p.schema_theory, "ThGraphQLSchema");
    }

    #[test]
    fn register_theories_works() {
        let mut registry = HashMap::new();
        register_theories(&mut registry);
        assert!(registry.contains_key("ThGraphQLSchema"));
        assert!(registry.contains_key("ThGraphQLInstance"));
    }

    #[test]
    #[allow(clippy::too_many_lines)]
    fn parse_sdl_with_forward_references() {
        let sdl = r"
scalar DateTime

interface Node {
  id: ID
}

type User implements Node {
  id: ID
  name: String
  posts: [Post!]!
}

type Post implements Node {
  id: ID
  title: String
  author: User
  createdAt: DateTime
}

input CreatePostInput {
  title: String
  authorId: ID
}

enum Status {
  DRAFT
  PUBLISHED
  ARCHIVED
}

union SearchResult = User | Post
";

        let schema = parse_sdl(sdl).unwrap_or_else(|e| panic!("parse_sdl should succeed: {e}"));

        // Verify vertices
        assert!(
            schema.vertices.contains_key("Node"),
            "should have Node interface"
        );
        assert!(
            schema.vertices.contains_key("User"),
            "should have User type"
        );
        assert!(
            schema.vertices.contains_key("Post"),
            "should have Post type"
        );
        assert!(
            schema.vertices.contains_key("DateTime"),
            "should have DateTime scalar"
        );
        assert!(
            schema.vertices.contains_key("CreatePostInput"),
            "should have CreatePostInput input"
        );
        assert!(
            schema.vertices.contains_key("Status"),
            "should have Status enum"
        );
        assert!(
            schema.vertices.contains_key("SearchResult"),
            "should have SearchResult union"
        );

        // Verify field vertices
        assert!(
            schema.vertices.contains_key("User.name"),
            "should have User.name field"
        );
        assert!(
            schema.vertices.contains_key("Post.title"),
            "should have Post.title field"
        );

        // Verify implements edges (forward reference: User -> Node)
        let user_implements: Vec<_> = schema
            .outgoing_edges("User")
            .iter()
            .filter(|e| e.kind == "implements")
            .collect();
        assert_eq!(
            user_implements.len(),
            1,
            "User should implement one interface"
        );
        assert_eq!(user_implements[0].tgt, "Node", "User should implement Node");

        assert_eq!(
            schema
                .outgoing_edges("Post")
                .iter()
                .filter(|e| e.kind == "implements")
                .count(),
            1,
            "Post should implement one interface"
        );

        // Verify type-of edges (forward reference: Post.author -> User)
        let author_type_of: Vec<_> = schema
            .outgoing_edges("Post.author")
            .iter()
            .filter(|e| e.kind == "type-of")
            .collect();
        assert_eq!(
            author_type_of.len(),
            1,
            "Post.author should have a type-of edge"
        );
        assert_eq!(
            author_type_of[0].tgt, "User",
            "Post.author should reference User"
        );

        // Verify type-of edge for Post.createdAt -> DateTime (scalar ref)
        let created_type_of: Vec<_> = schema
            .outgoing_edges("Post.createdAt")
            .iter()
            .filter(|e| e.kind == "type-of")
            .collect();
        assert_eq!(
            created_type_of.len(),
            1,
            "Post.createdAt should have a type-of edge"
        );
        assert_eq!(
            created_type_of[0].tgt, "DateTime",
            "Post.createdAt should reference DateTime"
        );

        // Verify union member-of edges
        let union_members: Vec<_> = schema
            .outgoing_edges("SearchResult")
            .iter()
            .filter(|e| e.kind == "member-of")
            .collect();
        assert_eq!(
            union_members.len(),
            2,
            "SearchResult should have 2 member-of edges"
        );
        let member_targets: std::collections::HashSet<&str> =
            union_members.iter().map(|e| e.tgt.as_str()).collect();
        assert!(
            member_targets.contains("User"),
            "SearchResult should include User"
        );
        assert!(
            member_targets.contains("Post"),
            "SearchResult should include Post"
        );

        // Verify enum values use "enum-value" kind
        let status_members: Vec<_> = schema
            .outgoing_edges("Status")
            .iter()
            .filter(|e| e.kind == "member-of")
            .collect();
        assert_eq!(status_members.len(), 3, "Status should have 3 enum values");
        for member in &status_members {
            let vertex = schema.vertices.get(&member.tgt).expect("vertex exists");
            assert_eq!(
                vertex.kind, "enum-value",
                "enum values should use enum-value kind"
            );
        }
    }

    #[test]
    fn emit_sdl_roundtrip() {
        let sdl = r"
type User {
  id: ID
  name: String
}

enum Status {
  ACTIVE
  INACTIVE
}
";
        let schema1 = parse_sdl(sdl).expect("first parse should succeed");
        let emitted = emit_sdl(&schema1).expect("emit should succeed");
        let schema2 = parse_sdl(&emitted).expect("re-parse should succeed");

        assert_eq!(
            schema1.vertex_count(),
            schema2.vertex_count(),
            "vertex counts should match after round-trip"
        );
        assert_eq!(
            schema1.edge_count(),
            schema2.edge_count(),
            "edge counts should match after round-trip"
        );
    }

    #[test]
    fn parse_extend_type() {
        let sdl = r"
type Query {
  users: [User]
}

extend type Query {
  posts: [Post]
}

type User {
  id: ID
}

type Post {
  id: ID
}
";
        let schema = parse_sdl(sdl).expect("should parse");
        assert!(schema.has_vertex("Query.users"));
        assert!(schema.has_vertex("Query.posts"));
    }

    #[test]
    fn parse_schema_definition() {
        let sdl = r"
schema {
  query: Query
  mutation: Mutation
}

type Query {
  hello: String
}

type Mutation {
  update: String
}
";
        let schema = parse_sdl(sdl).expect("should parse");
        assert!(schema.has_vertex("Query"));
        assert!(schema.has_vertex("Mutation"));
    }

    #[test]
    fn parse_directive_definition() {
        let sdl = r"
directive @deprecated(reason: String) on FIELD_DEFINITION | ENUM_VALUE

type Foo {
  bar: String
}
";
        let schema = parse_sdl(sdl).expect("should parse");
        assert!(schema.has_vertex("Foo"));
    }

    #[test]
    fn parse_subscription_type() {
        let sdl = r"
type Subscription {
  messageAdded: String
}
";
        let schema = parse_sdl(sdl).expect("should parse");
        assert!(schema.has_vertex("Subscription"));
        assert_eq!(
            schema.vertices.get("Subscription").unwrap().kind,
            "subscription"
        );
        assert!(schema.has_vertex("Subscription.messageAdded"));
    }
}