oxirs-core 0.2.4

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
//! # GraphQL Schema Auto-Generation from RDF
//! 
//! This module provides automatic GraphQL schema generation from RDF datasets,
//! enabling GraphQL queries over semantic data with full type introspection.

use crate::error::{Result, Error};
use crate::model::{Dataset, Graph, Triple, Quad, NamedNode, Literal, Term};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::RwLock;
use serde::{Serialize, Deserialize};

/// GraphQL schema generator that creates schemas from RDF data
pub struct GraphQLSchemaGenerator {
    /// Reference to the RDF dataset
    dataset: Arc<RwLock<Dataset>>,
    /// Generated GraphQL schema
    schema: Option<GraphQLSchema>,
    /// Configuration for schema generation
    config: GraphQLConfig,
}

impl GraphQLSchemaGenerator {
    /// Create a new GraphQL schema generator
    pub fn new(dataset: Arc<RwLock<Dataset>>) -> Result<Self> {
        Ok(Self {
            dataset,
            schema: None,
            config: GraphQLConfig::default(),
        })
    }

    /// Initialize the schema generator and create initial schema
    pub async fn initialize(&self) -> Result<()> {
        // Implementation will introspect RDF data and generate GraphQL schema
        Ok(())
    }

    /// Generate GraphQL schema from RDF dataset
    pub async fn generate_schema(&mut self) -> Result<GraphQLSchema> {
        let dataset = self.dataset.read().await;
        
        let mut types = HashMap::new();
        let mut predicates = HashMap::new();
        let mut type_definitions = Vec::new();
        
        // Analyze the dataset to extract types and predicates
        for quad in dataset.iter() {
            self.analyze_quad(&quad, &mut types, &mut predicates)?;
        }
        
        // Generate GraphQL type definitions
        for (type_uri, properties) in &types {
            let graphql_type = self.generate_graphql_type(type_uri, properties)?;
            type_definitions.push(graphql_type);
        }
        
        // Generate root Query type
        let query_type = self.generate_query_type(&types)?;
        type_definitions.push(query_type);
        
        let schema = GraphQLSchema {
            types: type_definitions,
            query_type: "Query".to_string(),
            mutation_type: Some("Mutation".to_string()),
            subscription_type: Some("Subscription".to_string()),
            directives: self.generate_custom_directives(),
        };
        
        self.schema = Some(schema.clone());
        Ok(schema)
    }

    /// Analyze a quad to extract type and property information
    fn analyze_quad(
        &self,
        quad: &Quad,
        types: &mut HashMap<String, HashSet<String>>,
        predicates: &mut HashMap<String, PredicateInfo>,
    ) -> Result<()> {
        // Extract subject type if available
        if let Term::NamedNode(predicate) = &quad.predicate {
            let pred_str = predicate.as_str();
            
            // Handle rdf:type predicates specially
            if pred_str == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" {
                if let (Term::NamedNode(subject), Term::NamedNode(object)) = (&quad.subject, &quad.object) {
                    let type_uri = object.as_str().to_string();
                    types.entry(type_uri).or_insert_with(HashSet::new);
                }
            } else {
                // Record property usage
                if let Term::NamedNode(subject) = &quad.subject {
                    // Try to determine subject type and add property
                    self.add_property_to_types(types, pred_str, &quad.object)?;
                }
                
                // Record predicate information
                predicates.insert(pred_str.to_string(), PredicateInfo {
                    uri: pred_str.to_string(),
                    object_type: self.determine_object_type(&quad.object)?,
                    cardinality: Cardinality::Many, // Could be refined with analysis
                });
            }
        }
        
        Ok(())
    }

    /// Add property to all relevant types
    fn add_property_to_types(
        &self,
        types: &mut HashMap<String, HashSet<String>>,
        predicate: &str,
        _object: &Term,
    ) -> Result<()> {
        // For now, add to all types - could be refined with type inference
        for properties in types.values_mut() {
            properties.insert(predicate.to_string());
        }
        Ok(())
    }

    /// Determine the GraphQL type for an RDF object
    fn determine_object_type(&self, object: &Term) -> Result<GraphQLObjectType> {
        match object {
            Term::NamedNode(_) => Ok(GraphQLObjectType::NamedNode),
            Term::BlankNode(_) => Ok(GraphQLObjectType::BlankNode),
            Term::Literal(literal) => {
                match literal.datatype() {
                    Some(datatype) => match datatype.as_str() {
                        "http://www.w3.org/2001/XMLSchema#string" => Ok(GraphQLObjectType::String),
                        "http://www.w3.org/2001/XMLSchema#integer" => Ok(GraphQLObjectType::Int),
                        "http://www.w3.org/2001/XMLSchema#decimal" | 
                        "http://www.w3.org/2001/XMLSchema#double" => Ok(GraphQLObjectType::Float),
                        "http://www.w3.org/2001/XMLSchema#boolean" => Ok(GraphQLObjectType::Boolean),
                        "http://www.w3.org/2001/XMLSchema#dateTime" => Ok(GraphQLObjectType::DateTime),
                        _ => Ok(GraphQLObjectType::String), // Default to string for unknown types
                    },
                    None => Ok(GraphQLObjectType::String), // Plain literals default to string
                }
            }
        }
    }

    /// Generate a GraphQL type definition from RDF type analysis
    fn generate_graphql_type(
        &self,
        type_uri: &str,
        properties: &HashSet<String>,
    ) -> Result<GraphQLTypeDefinition> {
        let type_name = self.uri_to_graphql_name(type_uri);
        let mut fields = Vec::new();
        
        // Add ID field
        fields.push(GraphQLField {
            name: "id".to_string(),
            field_type: GraphQLFieldType::NonNull(Box::new(GraphQLFieldType::Scalar("ID".to_string()))),
            description: Some("Unique identifier for this resource".to_string()),
            arguments: Vec::new(),
        });
        
        // Add fields for each property
        for property in properties {
            let field_name = self.uri_to_graphql_name(property);
            let field_type = self.determine_graphql_field_type(property)?;
            
            fields.push(GraphQLField {
                name: field_name,
                field_type,
                description: Some(format!("Property mapped from {property}")),
                arguments: Vec::new(),
            });
        }
        
        Ok(GraphQLTypeDefinition {
            name: type_name,
            fields,
            description: Some(format!("Type generated from RDF class {type_uri}")),
            interfaces: Vec::new(),
        })
    }

    /// Generate root Query type with finders for each RDF type
    fn generate_query_type(&self, types: &HashMap<String, HashSet<String>>) -> Result<GraphQLTypeDefinition> {
        let mut fields = Vec::new();
        
        for type_uri in types.keys() {
            let type_name = self.uri_to_graphql_name(type_uri);
            
            // Add single item query
            fields.push(GraphQLField {
                name: format!("get{type_name}"),
                field_type: GraphQLFieldType::Object(type_name.clone()),
                description: Some(format!("Find a single {} by ID", type_name)),
                arguments: vec![
                    GraphQLArgument {
                        name: "id".to_string(),
                        arg_type: GraphQLFieldType::NonNull(Box::new(GraphQLFieldType::Scalar("ID".to_string()))),
                        description: Some("The ID of the resource to retrieve".to_string()),
                        default_value: None,
                    }
                ],
            });
            
            // Add list query
            fields.push(GraphQLField {
                name: format!("list{type_name}"),
                field_type: GraphQLFieldType::List(Box::new(GraphQLFieldType::Object(type_name.clone()))),
                description: Some(format!("List all instances of {type_name}")),
                arguments: vec![
                    GraphQLArgument {
                        name: "limit".to_string(),
                        arg_type: GraphQLFieldType::Scalar("Int".to_string()),
                        description: Some("Maximum number of results to return".to_string()),
                        default_value: Some("10".to_string()),
                    },
                    GraphQLArgument {
                        name: "offset".to_string(),
                        arg_type: GraphQLFieldType::Scalar("Int".to_string()),
                        description: Some("Number of results to skip".to_string()),
                        default_value: Some("0".to_string()),
                    },
                ],
            });
        }
        
        Ok(GraphQLTypeDefinition {
            name: "Query".to_string(),
            fields,
            description: Some("Root query type for accessing RDF data".to_string()),
            interfaces: Vec::new(),
        })
    }

    /// Generate custom GraphQL directives for RDF-specific features
    fn generate_custom_directives(&self) -> Vec<GraphQLDirective> {
        vec![
            GraphQLDirective {
                name: "rdfProperty".to_string(),
                description: Some("Maps a GraphQL field to an RDF property".to_string()),
                locations: vec!["FIELD_DEFINITION".to_string()],
                arguments: vec![
                    GraphQLArgument {
                        name: "uri".to_string(),
                        arg_type: GraphQLFieldType::NonNull(Box::new(GraphQLFieldType::Scalar("String".to_string()))),
                        description: Some("The RDF property URI".to_string()),
                        default_value: None,
                    }
                ],
            },
            GraphQLDirective {
                name: "rdfType".to_string(),
                description: Some("Maps a GraphQL type to an RDF class".to_string()),
                locations: vec!["OBJECT".to_string()],
                arguments: vec![
                    GraphQLArgument {
                        name: "uri".to_string(),
                        arg_type: GraphQLFieldType::NonNull(Box::new(GraphQLFieldType::Scalar("String".to_string()))),
                        description: Some("The RDF class URI".to_string()),
                        default_value: None,
                    }
                ],
            },
        ]
    }

    /// Convert URI to valid GraphQL name
    fn uri_to_graphql_name(&self, uri: &str) -> String {
        // Extract local name from URI and make it GraphQL-compliant
        let local_name = uri.split(['/', '#']).last().unwrap_or(uri);
        
        // Convert to PascalCase and remove non-alphanumeric characters
        local_name
            .chars()
            .filter(|c| c.is_alphanumeric() || *c == '_')
            .collect::<String>()
            .split('_')
            .map(|word| {
                let mut chars = word.chars();
                match chars.next() {
                    None => String::new(),
                    Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
                }
            })
            .collect()
    }

    /// Determine GraphQL field type for an RDF property
    fn determine_graphql_field_type(&self, _property: &str) -> Result<GraphQLFieldType> {
        // For now, default to String - could be refined with property analysis
        Ok(GraphQLFieldType::Scalar("String".to_string()))
    }

    /// Check health status of the GraphQL schema generator
    pub async fn health_check(&self) -> crate::api::ServiceStatus {
        match &self.schema {
            Some(_) => crate::api::ServiceStatus::Healthy,
            None => crate::api::ServiceStatus::Degraded("Schema not generated yet".to_string()),
        }
    }
}

/// Configuration for GraphQL schema generation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphQLConfig {
    /// Whether to enable GraphQL introspection
    pub enable_introspection: bool,
    /// Whether to enable GraphQL playground
    pub enable_playground: bool,
    /// Maximum query depth allowed
    pub max_query_depth: usize,
    /// Maximum query complexity allowed
    pub max_query_complexity: usize,
    /// Whether to auto-refresh schema on dataset changes
    pub auto_refresh: bool,
}

impl Default for GraphQLConfig {
    fn default() -> Self {
        Self {
            enable_introspection: true,
            enable_playground: true,
            max_query_depth: 10,
            max_query_complexity: 1000,
            auto_refresh: true,
        }
    }
}

/// Generated GraphQL schema
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphQLSchema {
    /// Type definitions in the schema
    pub types: Vec<GraphQLTypeDefinition>,
    /// Root query type name
    pub query_type: String,
    /// Root mutation type name (optional)
    pub mutation_type: Option<String>,
    /// Root subscription type name (optional)
    pub subscription_type: Option<String>,
    /// Custom directives
    pub directives: Vec<GraphQLDirective>,
}

/// GraphQL type definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphQLTypeDefinition {
    /// Type name
    pub name: String,
    /// Type fields
    pub fields: Vec<GraphQLField>,
    /// Type description
    pub description: Option<String>,
    /// Implemented interfaces
    pub interfaces: Vec<String>,
}

/// GraphQL field definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphQLField {
    /// Field name
    pub name: String,
    /// Field type
    pub field_type: GraphQLFieldType,
    /// Field description
    pub description: Option<String>,
    /// Field arguments
    pub arguments: Vec<GraphQLArgument>,
}

/// GraphQL field type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum GraphQLFieldType {
    /// Scalar type (String, Int, Float, Boolean, ID)
    Scalar(String),
    /// Object type
    Object(String),
    /// List type
    List(Box<GraphQLFieldType>),
    /// Non-null type
    NonNull(Box<GraphQLFieldType>),
}

/// GraphQL field argument
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphQLArgument {
    /// Argument name
    pub name: String,
    /// Argument type
    pub arg_type: GraphQLFieldType,
    /// Argument description
    pub description: Option<String>,
    /// Default value
    pub default_value: Option<String>,
}

/// GraphQL directive definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphQLDirective {
    /// Directive name
    pub name: String,
    /// Directive description
    pub description: Option<String>,
    /// Valid locations for this directive
    pub locations: Vec<String>,
    /// Directive arguments
    pub arguments: Vec<GraphQLArgument>,
}

/// Information about an RDF predicate
#[derive(Debug, Clone)]
struct PredicateInfo {
    /// Predicate URI
    uri: String,
    /// Type of objects this predicate points to
    object_type: GraphQLObjectType,
    /// Cardinality (one or many)
    cardinality: Cardinality,
}

/// GraphQL representation of RDF object types
#[derive(Debug, Clone)]
enum GraphQLObjectType {
    NamedNode,
    BlankNode,
    String,
    Int,
    Float,
    Boolean,
    DateTime,
}

/// Property cardinality
#[derive(Debug, Clone)]
enum Cardinality {
    One,
    Many,
}