kotoba-server 0.1.16

Complete HTTP server and frontend integration system for Kotoba graph database
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
//! GraphQL API for Kotoba Schema Management
//!
//! This module provides a GraphQL interface for managing graph schemas,
//! including CRUD operations and validation.

use async_graphql::*;
use kotoba_schema::prelude::*;
use std::sync::Arc;
use std::collections::HashMap;

/// GraphQL context for schema operations
pub struct SchemaContext {
    pub schema_manager: Arc<SchemaManager>,
}

impl SchemaContext {
    pub fn new(schema_manager: Arc<SchemaManager>) -> Self {
        Self { schema_manager }
    }
}

/// GraphQL schema definition
pub type KotobaSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;

/// Query root for GraphQL schema
#[derive(Default)]
pub struct QueryRoot;

#[Object]
impl QueryRoot {
    /// Get all registered schemas
    async fn schemas(&self, ctx: &Context<'_>) -> Result<Vec<GraphSchemaGQL>> {
        let context = ctx.data::<SchemaContext>()?;
        let schema_ids = context.schema_manager.list_schemas()?;

        let mut schemas = Vec::new();
        for schema_id in schema_ids {
            if let Ok(Some(schema)) = context.schema_manager.get_schema(&schema_id) {
                schemas.push(schema.into());
            }
        }

        Ok(schemas)
    }

    /// Get a specific schema by ID
    async fn schema(&self, ctx: &Context<'_>, id: String) -> Result<Option<GraphSchemaGQL>> {
        let context = ctx.data::<SchemaContext>()?;
        match context.schema_manager.get_schema(&id)? {
            Some(schema) => Ok(Some(schema.into())),
            None => Ok(None),
        }
    }

    /// Get schema statistics
    async fn schema_stats(&self, ctx: &Context<'_>, id: String) -> Result<Option<SchemaStatisticsGQL>> {
        let context = ctx.data::<SchemaContext>()?;
        match context.schema_manager.get_schema_statistics(&id)? {
            Some(stats) => Ok(Some(stats.into())),
            None => Ok(None),
        }
    }

    /// Validate graph data against a schema
    async fn validate_graph(
        &self,
        ctx: &Context<'_>,
        schema_id: String,
        graph_data: String,
    ) -> Result<ValidationResultGQL> {
        let context = ctx.data::<SchemaContext>()?;

        // Parse JSON graph data
        let graph_json: serde_json::Value = serde_json::from_str(&graph_data)
            .map_err(|e| Error::new(format!("Invalid JSON: {}", e)))?;

        let result = context.schema_manager.validate_graph_data(&schema_id, &graph_json)?;
        Ok(result.into())
    }

    /// List all available schemas
    async fn schema_list(&self, ctx: &Context<'_>) -> Result<Vec<String>> {
        let context = ctx.data::<SchemaContext>()?;
        context.schema_manager.list_schemas()
            .map_err(|e| Error::new(format!("Failed to list schemas: {}", e)))
    }

    /// Get schema metadata
    async fn schema_metadata(&self, ctx: &Context<'_>, id: String) -> Result<Option<HashMap<String, ValueGQL>>> {
        let context = ctx.data::<SchemaContext>()?;
        match context.schema_manager.get_schema_metadata(&id)? {
            Some(metadata) => {
                let gql_metadata: HashMap<String, ValueGQL> = metadata.into_iter()
                    .map(|(k, v)| (k, v.into()))
                    .collect();
                Ok(Some(gql_metadata))
            },
            None => Ok(None),
        }
    }

    /// Health check for the schema system
    async fn schema_health(&self) -> Result<String> {
        // Simple health check
        Ok("Schema system is healthy".to_string())
    }
}

/// Mutation root for GraphQL schema
#[derive(Default)]
pub struct MutationRoot;

#[Object]
impl MutationRoot {
    /// Create a new schema
    async fn create_schema(
        &self,
        ctx: &Context<'_>,
        id: String,
        name: String,
        version: String,
        description: Option<String>,
    ) -> Result<GraphSchemaGQL> {
        let context = ctx.data::<SchemaContext>()?;

        let schema = GraphSchema::new(id, name, version);
        let mut schema_with_desc = schema;
        if let Some(desc) = description {
            schema_with_desc.description = Some(desc);
        }

        context.schema_manager.register_schema(schema_with_desc.clone())?;
        Ok(schema_with_desc.into())
    }

    /// Update an existing schema
    async fn update_schema(
        &self,
        ctx: &Context<'_>,
        id: String,
        name: Option<String>,
        version: Option<String>,
        description: Option<String>,
    ) -> Result<GraphSchemaGQL> {
        let context = ctx.data::<SchemaContext>()?;

        // Get existing schema
        let mut existing_schema = context.schema_manager.get_schema(&id)?
            .ok_or_else(|| Error::new(format!("Schema '{}' not found", id)))?;

        // Update fields
        if let Some(name) = name {
            existing_schema.name = name;
        }
        if let Some(version) = version {
            existing_schema.version = version;
        }
        if let Some(description) = description {
            existing_schema.description = Some(description);
        }

        context.schema_manager.update_schema(existing_schema.clone())?;
        Ok(existing_schema.into())
    }

    /// Delete a schema
    async fn delete_schema(&self, ctx: &Context<'_>, id: String) -> Result<bool> {
        let context = ctx.data::<SchemaContext>()?;
        context.schema_manager.delete_schema(&id)?;
        Ok(true)
    }

    /// Add a vertex type to a schema
    async fn add_vertex_type(
        &self,
        ctx: &Context<'_>,
        schema_id: String,
        vertex_type: VertexTypeInput,
    ) -> Result<GraphSchemaGQL> {
        let context = ctx.data::<SchemaContext>()?;

        let mut schema = context.schema_manager.get_schema(&schema_id)?
            .ok_or_else(|| Error::new(format!("Schema '{}' not found", schema_id)))?;

        let vertex_schema: VertexTypeSchema = vertex_type.into();
        schema.add_vertex_type(vertex_schema);

        context.schema_manager.update_schema(schema.clone())?;
        Ok(schema.into())
    }

    /// Add an edge type to a schema
    async fn add_edge_type(
        &self,
        ctx: &Context<'_>,
        schema_id: String,
        edge_type: EdgeTypeInput,
    ) -> Result<GraphSchemaGQL> {
        let context = ctx.data::<SchemaContext>()?;

        let mut schema = context.schema_manager.get_schema(&schema_id)?
            .ok_or_else(|| Error::new(format!("Schema '{}' not found", schema_id)))?;

        let edge_schema: EdgeTypeSchema = edge_type.into();
        schema.add_edge_type(edge_schema);

        context.schema_manager.update_schema(schema.clone())?;
        Ok(schema.into())
    }

    /// Clone a schema
    async fn clone_schema(
        &self,
        ctx: &Context<'_>,
        source_id: String,
        target_id: String,
    ) -> Result<GraphSchemaGQL> {
        let context = ctx.data::<SchemaContext>()?;
        context.schema_manager.clone_schema(&source_id, &target_id)?;

        let cloned_schema = context.schema_manager.get_schema(&target_id)?
            .ok_or_else(|| Error::new("Failed to retrieve cloned schema".to_string()))?;

        Ok(cloned_schema.into())
    }

    /// Update schema metadata
    async fn update_schema_metadata(
        &self,
        ctx: &Context<'_>,
        schema_id: String,
        metadata: HashMap<String, ValueGQL>,
    ) -> Result<bool> {
        let context = ctx.data::<SchemaContext>()?;

        let gql_metadata: HashMap<String, Value> = metadata.into_iter()
            .map(|(k, v)| (k, v.into()))
            .collect();

        context.schema_manager.update_schema_metadata(&schema_id, gql_metadata)?;
        Ok(true)
    }

    /// Validate and apply schema changes
    async fn validate_schema_changes(
        &self,
        ctx: &Context<'_>,
        schema_id: String,
        changes: Vec<SchemaChange>,
    ) -> Result<ValidationResultGQL> {
        let context = ctx.data::<SchemaContext>()?;

        // Get current schema
        let mut schema = context.schema_manager.get_schema(&schema_id)?
            .ok_or_else(|| Error::new(format!("Schema '{}' not found", schema_id)))?;

        // Apply changes
        for change in changes {
            match change {
                SchemaChange::AddVertexType(vertex_input) => {
                    let vertex_schema: VertexTypeSchema = vertex_input.into();
                    schema.add_vertex_type(vertex_schema);
                },
                SchemaChange::AddEdgeType(edge_input) => {
                    let edge_schema: EdgeTypeSchema = edge_input.into();
                    schema.add_edge_type(edge_schema);
                },
                SchemaChange::UpdateName(new_name) => {
                    schema.name = new_name;
                },
                SchemaChange::UpdateVersion(new_version) => {
                    schema.version = new_version;
                },
                SchemaChange::UpdateDescription(new_desc) => {
                    schema.description = Some(new_desc);
                },
            }
        }

        // Validate the updated schema
        let validation = schema.validate_schema();

        // If valid, save the changes
        if validation.is_valid {
            context.schema_manager.update_schema(schema)?;
        }

        Ok(validation.into())
    }
}

// GraphQL type definitions

/// GraphQL representation of GraphSchema
#[derive(SimpleObject)]
pub struct GraphSchemaGQL {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
    pub version: String,
    pub vertex_types: Vec<VertexTypeGQL>,
    pub edge_types: Vec<EdgeTypeGQL>,
    pub constraints: Vec<String>, // Simplified representation
}

/// GraphQL representation of VertexTypeSchema
#[derive(SimpleObject)]
pub struct VertexTypeGQL {
    pub name: String,
    pub description: Option<String>,
    pub required_properties: Vec<String>,
    pub properties: Vec<PropertyGQL>,
    pub inherits: Vec<String>,
}

/// GraphQL representation of EdgeTypeSchema
#[derive(SimpleObject)]
pub struct EdgeTypeGQL {
    pub name: String,
    pub description: Option<String>,
    pub source_types: Vec<String>,
    pub target_types: Vec<String>,
    pub required_properties: Vec<String>,
    pub properties: Vec<PropertyGQL>,
    pub directed: bool,
}

/// GraphQL representation of PropertySchema
#[derive(SimpleObject)]
pub struct PropertyGQL {
    pub name: String,
    pub property_type: String,
    pub description: Option<String>,
    pub required: bool,
}

/// GraphQL representation of SchemaStatistics
#[derive(SimpleObject)]
pub struct SchemaStatisticsGQL {
    pub vertex_types: usize,
    pub edge_types: usize,
    pub constraints: usize,
    pub total_properties: usize,
}

/// GraphQL representation of ValidationResult
#[derive(SimpleObject)]
pub struct ValidationResultGQL {
    pub is_valid: bool,
    pub errors: Vec<ValidationErrorGQL>,
    pub warnings: Vec<String>,
}

/// GraphQL representation of ValidationError
#[derive(SimpleObject)]
pub struct ValidationErrorGQL {
    pub error_type: String,
    pub message: String,
    pub element_id: Option<String>,
    pub property: Option<String>,
}

/// GraphQL representation of Value
#[derive(InputObject, SimpleObject)]
#[graphql(input_name = "ValueInput")]
pub struct ValueGQL {
    #[graphql(flatten)]
    pub value: ValueTypeGQL,
}

/// GraphQL representation of different value types
#[derive(InputObject, SimpleObject)]
#[graphql(input_name = "ValueTypeInput")]
pub struct ValueTypeGQL {
    pub string_value: Option<String>,
    pub int_value: Option<i64>,
    pub float_value: Option<f64>,
    pub bool_value: Option<bool>,
    pub array_value: Option<Vec<ValueGQL>>,
    pub object_value: Option<HashMap<String, ValueGQL>>,
}

// Input types for mutations

/// Input for vertex type creation
#[derive(InputObject)]
pub struct VertexTypeInput {
    pub name: String,
    pub description: Option<String>,
    pub required_properties: Vec<String>,
    pub properties: Vec<PropertyInput>,
    pub inherits: Vec<String>,
}

/// Input for edge type creation
#[derive(InputObject)]
pub struct EdgeTypeInput {
    pub name: String,
    pub description: Option<String>,
    pub source_types: Vec<String>,
    pub target_types: Vec<String>,
    pub required_properties: Vec<String>,
    pub properties: Vec<PropertyInput>,
    pub directed: bool,
}

/// Input for property creation
#[derive(InputObject)]
pub struct PropertyInput {
    pub name: String,
    pub property_type: String,
    pub description: Option<String>,
    pub required: bool,
    pub constraints: Vec<PropertyConstraintInput>,
}

/// Input for property constraints
#[derive(InputObject)]
pub struct PropertyConstraintInput {
    pub constraint_type: String,
    pub value: Option<String>,
}

/// Schema change operations for batch updates
#[derive(InputObject)]
pub enum SchemaChange {
    AddVertexType(VertexTypeInput),
    AddEdgeType(EdgeTypeInput),
    UpdateName(String),
    UpdateVersion(String),
    UpdateDescription(String),
}

// Conversion implementations

impl From<GraphSchema> for GraphSchemaGQL {
    fn from(schema: GraphSchema) -> Self {
        GraphSchemaGQL {
            id: schema.id,
            name: schema.name,
            description: schema.description,
            version: schema.version,
            vertex_types: schema.vertex_types.into_values().map(Into::into).collect(),
            edge_types: schema.edge_types.into_values().map(Into::into).collect(),
            constraints: schema.constraints.iter().map(|c| format!("{:?}", c)).collect(),
        }
    }
}

impl From<VertexTypeSchema> for VertexTypeGQL {
    fn from(vertex: VertexTypeSchema) -> Self {
        VertexTypeGQL {
            name: vertex.name,
            description: vertex.description,
            required_properties: vertex.required_properties,
            properties: vertex.properties.into_values().map(Into::into).collect(),
            inherits: vertex.inherits,
        }
    }
}

impl From<EdgeTypeSchema> for EdgeTypeGQL {
    fn from(edge: EdgeTypeSchema) -> Self {
        EdgeTypeGQL {
            name: edge.name,
            description: edge.description,
            source_types: edge.source_types,
            target_types: edge.target_types,
            required_properties: edge.required_properties,
            properties: edge.properties.into_values().map(Into::into).collect(),
            directed: edge.directed,
        }
    }
}

impl From<PropertySchema> for PropertyGQL {
    fn from(prop: PropertySchema) -> Self {
        PropertyGQL {
            name: prop.name,
            property_type: format!("{:?}", prop.property_type),
            description: prop.description,
            required: prop.required,
        }
    }
}

impl From<SchemaStatistics> for SchemaStatisticsGQL {
    fn from(stats: SchemaStatistics) -> Self {
        SchemaStatisticsGQL {
            vertex_types: stats.vertex_types,
            edge_types: stats.edge_types,
            constraints: stats.constraints,
            total_properties: stats.total_properties,
        }
    }
}

impl From<ValidationResult> for ValidationResultGQL {
    fn from(result: ValidationResult) -> Self {
        ValidationResultGQL {
            is_valid: result.is_valid,
            errors: result.errors.into_iter().map(Into::into).collect(),
            warnings: result.warnings,
        }
    }
}

impl From<ValidationError> for ValidationErrorGQL {
    fn from(error: ValidationError) -> Self {
        ValidationErrorGQL {
            error_type: format!("{:?}", error.error_type),
            message: error.message,
            element_id: error.element_id,
            property: error.property,
        }
    }
}

impl From<Value> for ValueGQL {
    fn from(value: Value) -> Self {
        match value {
            Value::String(s) => ValueGQL {
                value: ValueTypeGQL {
                    string_value: Some(s),
                    int_value: None,
                    float_value: None,
                    bool_value: None,
                    array_value: None,
                    object_value: None,
                }
            },
            Value::Integer(i) => ValueGQL {
                value: ValueTypeGQL {
                    string_value: None,
                    int_value: Some(i),
                    float_value: None,
                    bool_value: None,
                    array_value: None,
                    object_value: None,
                }
            },
            Value::Float(f) => ValueGQL {
                value: ValueTypeGQL {
                    string_value: None,
                    int_value: None,
                    float_value: Some(f),
                    bool_value: None,
                    array_value: None,
                    object_value: None,
                }
            },
            Value::Boolean(b) => ValueGQL {
                value: ValueTypeGQL {
                    string_value: None,
                    int_value: None,
                    float_value: None,
                    bool_value: Some(b),
                    array_value: None,
                    object_value: None,
                }
            },
            Value::Array(arr) => ValueGQL {
                value: ValueTypeGQL {
                    string_value: None,
                    int_value: None,
                    float_value: None,
                    bool_value: None,
                    array_value: Some(arr.into_iter().map(Into::into).collect()),
                    object_value: None,
                }
            },
            Value::Object(obj) => ValueGQL {
                value: ValueTypeGQL {
                    string_value: None,
                    int_value: None,
                    float_value: None,
                    bool_value: None,
                    array_value: None,
                    object_value: Some(obj.into_iter().map(|(k, v)| (k, v.into())).collect()),
                }
            },
        }
    }
}

impl From<ValueGQL> for Value {
    fn from(value: ValueGQL) -> Self {
        if let Some(s) = value.value.string_value {
            Value::String(s)
        } else if let Some(i) = value.value.int_value {
            Value::Integer(i)
        } else if let Some(f) = value.value.float_value {
            Value::Float(f)
        } else if let Some(b) = value.value.bool_value {
            Value::Boolean(b)
        } else if let Some(arr) = value.value.array_value {
            Value::Array(arr.into_iter().map(Into::into).collect())
        } else if let Some(obj) = value.value.object_value {
            Value::Object(obj.into_iter().map(|(k, v)| (k, v.into())).collect())
        } else {
            Value::String("".to_string()) // Default fallback
        }
    }
}

impl From<VertexTypeInput> for VertexTypeSchema {
    fn from(input: VertexTypeInput) -> Self {
        let properties: HashMap<String, PropertySchema> = input.properties
            .into_iter()
            .map(|p| {
                let property_type = match p.property_type.as_str() {
                    "String" => PropertyType::String,
                    "Integer" => PropertyType::Integer,
                    "Float" => PropertyType::Float,
                    "Boolean" => PropertyType::Boolean,
                    "DateTime" => PropertyType::DateTime,
                    "Json" => PropertyType::Json,
                    _ => PropertyType::String, // Default fallback
                };

                let property_schema = PropertySchema {
                    name: p.name,
                    property_type,
                    description: p.description,
                    required: p.required,
                    default_value: None,
                    constraints: vec![], // TODO: Convert from input
                };

                (p.name, property_schema)
            })
            .collect();

        VertexTypeSchema {
            name: input.name,
            description: input.description,
            required_properties: input.required_properties,
            properties,
            inherits: input.inherits,
            constraints: vec![],
        }
    }
}

impl From<EdgeTypeInput> for EdgeTypeSchema {
    fn from(input: EdgeTypeInput) -> Self {
        let properties: HashMap<String, PropertySchema> = input.properties
            .into_iter()
            .map(|p| {
                let property_type = match p.property_type.as_str() {
                    "String" => PropertyType::String,
                    "Integer" => PropertyType::Integer,
                    "Float" => PropertyType::Float,
                    "Boolean" => PropertyType::Boolean,
                    "DateTime" => PropertyType::DateTime,
                    "Json" => PropertyType::Json,
                    _ => PropertyType::String, // Default fallback
                };

                let property_schema = PropertySchema {
                    name: p.name,
                    property_type,
                    description: p.description,
                    required: p.required,
                    default_value: None,
                    constraints: vec![], // TODO: Convert from input
                };

                (p.name, property_schema)
            })
            .collect();

        EdgeTypeSchema {
            name: input.name,
            description: input.description,
            source_types: input.source_types,
            target_types: input.target_types,
            required_properties: input.required_properties,
            properties,
            directed: input.directed,
            constraints: vec![],
        }
    }
}

/// Create GraphQL schema
pub fn create_schema(schema_manager: Arc<SchemaManager>) -> KotobaSchema {
    let context = SchemaContext::new(schema_manager);

    Schema::build(QueryRoot, MutationRoot, EmptySubscription)
        .data(context)
        .finish()
}

/// GraphQL handler for HTTP requests
pub async fn graphql_handler(
    schema: &KotobaSchema,
    request_body: String,
) -> Result<String> {
    let request: async_graphql::Request = serde_json::from_str(&request_body)
        .map_err(|e| Error::new(format!("Invalid GraphQL request: {}", e)))?;

    let response = schema.execute(request).await;
    let json_response = serde_json::to_string(&response)
        .map_err(|e| Error::new(format!("Failed to serialize response: {}", e)))?;

    Ok(json_response)
}