protograph_core/
ast.rs

1use std::collections::HashMap;
2
3#[derive(Debug, Clone, Default)]
4pub struct ProtographSchema {
5    pub types: HashMap<String, EntityType>,
6    pub input_types: HashMap<String, InputType>,
7    pub enums: HashMap<String, EnumType>,
8    pub query_fields: Vec<QueryField>,
9    pub mutation_fields: Vec<MutationField>,
10}
11
12#[derive(Debug, Clone)]
13pub struct EntityType {
14    pub name: String,
15    pub is_entity: bool,
16    pub is_private: bool,
17    pub fields: Vec<Field>,
18}
19
20#[derive(Debug, Clone)]
21pub struct InputType {
22    pub name: String,
23    pub fields: Vec<InputField>,
24}
25
26#[derive(Debug, Clone)]
27pub struct EnumType {
28    pub name: String,
29    pub values: Vec<String>,
30}
31
32#[derive(Debug, Clone)]
33pub struct Field {
34    pub name: String,
35    pub field_type: FieldType,
36    pub is_private: bool,
37    pub relationship: Option<Relationship>,
38}
39
40#[derive(Debug, Clone)]
41pub struct InputField {
42    pub name: String,
43    pub field_type: FieldType,
44}
45
46#[derive(Debug, Clone)]
47pub enum FieldType {
48    Named(String),
49    NonNull(Box<FieldType>),
50    List(Box<FieldType>),
51}
52
53impl FieldType {
54    pub fn base_type(&self) -> &str {
55        match self {
56            FieldType::Named(name) => name,
57            FieldType::NonNull(inner) => inner.base_type(),
58            FieldType::List(inner) => inner.base_type(),
59        }
60    }
61
62    pub fn is_list(&self) -> bool {
63        match self {
64            FieldType::List(_) => true,
65            FieldType::NonNull(inner) => inner.is_list(),
66            _ => false,
67        }
68    }
69
70    pub fn is_non_null(&self) -> bool {
71        matches!(self, FieldType::NonNull(_))
72    }
73}
74
75#[derive(Debug, Clone)]
76pub enum Relationship {
77    BelongsTo { foreign_key: String },
78    HasMany { foreign_key: String },
79    ManyToMany { junction_table: String, foreign_key: String },
80}
81
82#[derive(Debug, Clone)]
83pub struct QueryField {
84    pub name: String,
85    pub arguments: Vec<InputField>,
86    pub return_type: FieldType,
87}
88
89#[derive(Debug, Clone)]
90pub struct MutationField {
91    pub name: String,
92    pub arguments: Vec<InputField>,
93    pub return_type: FieldType,
94}