1use std::collections::HashMap;
4
5#[derive(Debug, Clone)]
7pub struct Document {
8 pub operations: Vec<Operation>,
9}
10
11#[derive(Debug, Clone)]
13pub enum Operation {
14 Query(Query),
15 Mutation(Mutation),
16 Subscription(Subscription),
17 Schema(Schema),
18 Migration(Migration),
19 FragmentDefinition(FragmentDef),
20 Introspection(IntrospectionQuery),
21 Handler(HandlerDef),
22}
23
24#[derive(Debug, Clone)]
26pub struct Query {
27 pub name: Option<String>,
28 pub variable_definitions: Vec<VariableDefinition>,
29 pub directives: Vec<Directive>,
30 pub selection_set: Vec<Field>,
31 pub variables_values: HashMap<String, Value>,
32}
33
34#[derive(Debug, Clone)]
36pub struct Mutation {
37 pub name: Option<String>,
38 pub variable_definitions: Vec<VariableDefinition>,
39 pub directives: Vec<Directive>,
40 pub operations: Vec<MutationOperation>,
41 pub variables_values: HashMap<String, Value>,
42}
43
44#[derive(Debug, Clone)]
46pub struct Subscription {
47 pub name: Option<String>,
48 pub variable_definitions: Vec<VariableDefinition>,
49 pub directives: Vec<Directive>,
50 pub selection_set: Vec<Field>,
51 pub variables_values: HashMap<String, Value>,
52}
53
54#[derive(Debug, Clone)]
55pub struct Schema {
56 pub operations: Vec<SchemaOp>,
57}
58
59#[derive(Debug, Clone)]
60pub enum SchemaOp {
61 DefineCollection {
62 name: String,
63 if_not_exists: bool,
64 fields: Vec<FieldDef>,
65 directives: Vec<Directive>,
66 },
67 AlterCollection {
68 name: String,
69 actions: Vec<AlterAction>,
70 },
71 DropCollection {
72 name: String,
73 if_exists: bool,
74 },
75}
76
77#[derive(Debug, Clone)]
78pub enum AlterAction {
79 AddField(FieldDef),
80 DropField(String),
81 RenameField { from: String, to: String },
82 ModifyField(FieldDef),
83}
84
85#[derive(Debug, Clone)]
86pub struct Migration {
87 pub steps: Vec<MigrationStep>,
88}
89
90#[derive(Debug, Clone)]
91pub struct MigrationStep {
92 pub version: String,
93 pub actions: Vec<MigrationAction>,
94}
95
96#[derive(Debug, Clone)]
97pub enum MigrationAction {
98 Schema(SchemaOp),
99 DataMigration(DataMigration),
100}
101
102#[derive(Debug, Clone)]
103pub struct DataMigration {
104 pub collection: String,
105 pub transforms: Vec<DataTransform>,
106}
107
108#[derive(Debug, Clone)]
109pub struct DataTransform {
110 pub field: String,
111 pub expression: String, pub filter: Option<Filter>,
113}
114
115#[derive(Debug, Clone)]
117pub struct HandlerDef {
118 pub name: String,
119 pub trigger: HandlerTrigger,
120 pub action: MutationOperation,
121}
122
123#[derive(Debug, Clone, PartialEq)]
125pub enum HandlerTrigger {
126 JobCompleted,
128 JobFailed,
130 Insert { collection: Option<String> },
132 Update { collection: Option<String> },
134 Delete { collection: Option<String> },
136 Custom(String),
138}
139
140#[derive(Debug, Clone)]
142pub struct FieldDef {
143 pub name: String,
144 pub field_type: TypeAnnotation,
145 pub directives: Vec<Directive>,
146}
147
148#[derive(Debug, Clone)]
150pub struct VariableDefinition {
151 pub name: String,
152 pub var_type: TypeAnnotation,
153 pub default_value: Option<Value>,
154}
155
156#[derive(Debug, Clone)]
158pub struct TypeAnnotation {
159 pub name: String,
160 pub is_array: bool,
161 pub is_required: bool,
162}
163
164#[derive(Debug, Clone)]
166pub struct Directive {
167 pub name: String,
168 pub arguments: Vec<Argument>,
169}
170
171#[derive(Debug, Clone)]
173pub struct Field {
174 pub alias: Option<String>,
175 pub name: String,
176 pub arguments: Vec<Argument>,
177 pub directives: Vec<Directive>,
178 pub selection_set: Vec<Field>,
179}
180
181#[derive(Debug, Clone)]
183pub struct Argument {
184 pub name: String,
185 pub value: Value,
186}
187
188#[derive(Debug, Clone)]
190pub struct MutationOperation {
191 pub alias: Option<String>,
192 pub operation: MutationOp,
193 pub directives: Vec<Directive>,
194 pub selection_set: Vec<Field>,
195}
196
197#[derive(Debug, Clone)]
199pub enum MutationOp {
200 Insert {
201 collection: String,
202 data: Value,
203 },
204 InsertMany {
205 collection: String,
206 data: Vec<Value>,
207 },
208 Update {
209 collection: String,
210 filter: Option<Filter>,
211 data: Value,
212 },
213 Upsert {
214 collection: String,
215 filter: Option<Filter>,
216 data: Value,
217 },
218 Delete {
219 collection: String,
220 filter: Option<Filter>,
221 },
222 EnqueueJob {
223 job_type: String,
224 payload: Value,
225 priority: JobPriority,
226 scheduled_at: Option<String>,
227 max_retries: Option<u32>,
228 },
229 Transaction {
230 operations: Vec<MutationOperation>,
231 },
232}
233
234#[derive(Debug, Clone, Copy, PartialEq, Eq)]
236pub enum JobPriority {
237 Low,
238 Normal,
239 High,
240 Critical,
241}
242
243#[derive(Debug, Clone)]
245pub enum Filter {
246 Eq(String, Value),
247 Ne(String, Value),
248 Gt(String, Value),
249 Gte(String, Value),
250 Lt(String, Value),
251 Lte(String, Value),
252 In(String, Value),
253 NotIn(String, Value),
254 Contains(String, Value),
255 StartsWith(String, Value),
256 EndsWith(String, Value),
257 Matches(String, Value),
258 IsNull(String),
259 IsNotNull(String),
260 And(Vec<Filter>),
261 Or(Vec<Filter>),
262 Not(Box<Filter>),
263}
264
265#[derive(Debug, Clone)]
267pub enum Value {
268 Null,
269 Boolean(bool),
270 Int(i64),
271 Float(f64),
272 String(String),
273 Array(Vec<Value>),
274 Object(HashMap<String, Value>),
275 Variable(String),
276 Enum(String),
277}
278
279#[derive(Debug, Clone)]
281pub struct FragmentDef {
282 pub name: String,
283 pub type_condition: String,
284 pub selection_set: Vec<Selection>,
285}
286
287#[derive(Debug, Clone)]
289pub struct IntrospectionQuery {
290 pub arguments: Vec<Argument>,
291 pub fields: Vec<String>,
292}
293
294#[derive(Debug, Clone)]
296pub enum Selection {
297 Field(Field),
298 FragmentSpread(String),
299 InlineFragment(InlineFragment),
300 ComputedField(ComputedField),
301 SpecialSelection(SpecialSelection),
302}
303
304#[derive(Debug, Clone)]
306pub struct InlineFragment {
307 pub type_condition: String,
308 pub selection_set: Vec<Selection>,
309}
310
311#[derive(Debug, Clone)]
313pub struct ComputedField {
314 pub alias: String,
315 pub expression: ComputedExpression,
316}
317
318#[derive(Debug, Clone)]
320pub enum ComputedExpression {
321 TemplateString(String),
322 FunctionCall {
323 name: String,
324 args: Vec<Expression>,
325 },
326 PipeExpression {
327 base: Box<Expression>,
328 operations: Vec<PipeOp>,
329 },
330 SqlExpression(String),
331 AggregateFunction {
332 name: String,
333 field: String,
334 },
335}
336
337#[derive(Debug, Clone)]
339pub struct PipeOp {
340 pub function: String,
341 pub args: Vec<Expression>,
342}
343
344#[derive(Debug, Clone)]
346pub enum Expression {
347 Literal(Value),
348 FieldAccess(Vec<String>),
349 Variable(String),
350 FunctionCall {
351 name: String,
352 args: Vec<Expression>,
353 },
354 Binary {
355 op: BinaryOp,
356 left: Box<Expression>,
357 right: Box<Expression>,
358 },
359 Unary {
360 op: UnaryOp,
361 expr: Box<Expression>,
362 },
363 Ternary {
364 condition: Box<Expression>,
365 then_expr: Box<Expression>,
366 else_expr: Box<Expression>,
367 },
368}
369
370#[derive(Debug, Clone, Copy, PartialEq, Eq)]
372pub enum BinaryOp {
373 Add,
374 Sub,
375 Mul,
376 Div,
377 Mod,
378 Eq,
379 Ne,
380 Gt,
381 Gte,
382 Lt,
383 Lte,
384 And,
385 Or,
386}
387
388#[derive(Debug, Clone, Copy, PartialEq, Eq)]
390pub enum UnaryOp {
391 Not,
392 Neg,
393}
394
395#[derive(Debug, Clone)]
397pub enum SpecialSelection {
398 Aggregate(AggregateSelection),
399 GroupBy(GroupBySelection),
400 Lookup(LookupSelection),
401 PageInfo(PageInfoSelection),
402 Edges(EdgesSelection),
403 Downsample(DownsampleSelection),
404 WindowFunction(WindowFunctionSelection),
405}
406
407#[derive(Debug, Clone)]
409pub struct AggregateSelection {
410 pub fields: Vec<AggregateField>,
411}
412
413#[derive(Debug, Clone)]
415pub struct AggregateField {
416 pub function: String,
417 pub field: Option<String>,
418}
419
420#[derive(Debug, Clone)]
422pub struct GroupBySelection {
423 pub field: Option<String>,
424 pub fields: Option<Vec<String>>,
425 pub interval: Option<String>,
426 pub result_fields: Vec<String>,
427}
428
429#[derive(Debug, Clone)]
431pub struct LookupSelection {
432 pub collection: String,
433 pub local_field: String,
434 pub foreign_field: String,
435 pub filter: Option<Filter>,
436 pub selection_set: Vec<Selection>,
437}
438
439#[derive(Debug, Clone)]
441pub struct PageInfoSelection {
442 pub fields: Vec<String>,
443}
444
445#[derive(Debug, Clone)]
447pub struct EdgesSelection {
448 pub fields: Vec<EdgeField>,
449}
450
451#[derive(Debug, Clone)]
453pub enum EdgeField {
454 Cursor,
455 Node(Vec<Selection>),
456}
457
458#[derive(Debug, Clone)]
460pub struct DownsampleSelection {
461 pub interval: String,
462 pub aggregation: String,
463 pub selection_set: Vec<Selection>,
464}
465
466#[derive(Debug, Clone)]
468pub struct WindowFunctionSelection {
469 pub alias: String,
470 pub field: String,
471 pub function: String,
472 pub window_size: i64,
473}
474
475#[derive(Debug, Clone)]
481pub struct WhereClause {
482 pub filter: Filter,
483}
484
485#[derive(Debug, Clone)]
487pub struct OrderByClause {
488 pub orderings: Vec<Ordering>,
489}
490
491#[derive(Debug, Clone)]
493pub struct Ordering {
494 pub field: String,
495 pub direction: SortDirection,
496}
497
498#[derive(Debug, Clone, Copy, PartialEq, Eq)]
500pub enum SortDirection {
501 Asc,
502 Desc,
503}
504
505#[derive(Debug, Clone)]
507pub struct SearchArgs {
508 pub query: String,
509 pub fields: Vec<String>,
510 pub fuzzy: bool,
511 pub min_score: Option<f64>,
512}
513
514#[derive(Debug, Clone)]
516pub struct ValidateArgs {
517 pub rules: Vec<ValidationRule>,
518}
519
520#[derive(Debug, Clone)]
522pub struct ValidationRule {
523 pub field: String,
524 pub constraints: Vec<ValidationConstraint>,
525}
526
527#[derive(Debug, Clone)]
529pub enum ValidationConstraint {
530 Format(String),
531 Min(f64),
532 Max(f64),
533 MinLength(i64),
534 MaxLength(i64),
535 Pattern(String),
536}