oxirs-gql 0.2.4

GraphQL façade for OxiRS with automatic schema generation from RDF ontologies
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
//! Apollo Federation v2 Support
//!
//! Provides full Apollo Federation v2 compatibility for building distributed GraphQL
//! architectures with subgraphs. Implements all standard Federation directives,
//! entity resolution, and service introspection.
//!
//! ## Features
//!
//! - **Federation Directives**: @key, @external, @requires, @provides, @shareable, @override
//! - **Entity Resolution**: Automatic _entities query with reference resolver
//! - **Service Introspection**: _service query for schema composition
//! - **Automatic Schema Generation**: Convert RDF ontologies to Federation schemas
//! - **Type Extensions**: Support for extending types across subgraphs
//! - **Composition Hints**: Metadata for optimal query planning

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt::Write as FmtWrite;

/// Apollo Federation version
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FederationVersion {
    V1,
    V2,
}

/// Federation directive definitions
#[derive(Debug, Clone)]
pub struct FederationDirective {
    pub name: String,
    pub description: String,
    pub locations: Vec<DirectiveLocation>,
    pub arguments: Vec<DirectiveArgument>,
}

/// Directive location
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DirectiveLocation {
    Object,
    FieldDefinition,
    Interface,
    Scalar,
    Enum,
    Union,
    InputObject,
}

/// Directive argument
#[derive(Debug, Clone)]
pub struct DirectiveArgument {
    pub name: String,
    pub arg_type: String,
    pub description: Option<String>,
    pub default_value: Option<String>,
}

/// Entity key configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityKey {
    /// Fields that uniquely identify this entity
    pub fields: Vec<String>,
    /// Whether this key can be resolvable (default: true)
    pub resolvable: bool,
}

impl EntityKey {
    pub fn new(fields: Vec<String>) -> Self {
        Self {
            fields,
            resolvable: true,
        }
    }

    pub fn with_resolvable(mut self, resolvable: bool) -> Self {
        self.resolvable = resolvable;
        self
    }

    /// Convert to GraphQL directive syntax
    pub fn to_directive_string(&self) -> String {
        let fields_str = self.fields.join(" ");
        if self.resolvable {
            format!("@key(fields: \"{}\")", fields_str)
        } else {
            format!("@key(fields: \"{}\", resolvable: false)", fields_str)
        }
    }
}

/// External field configuration
#[derive(Debug, Clone)]
pub struct ExternalField {
    pub field_name: String,
    pub type_name: String,
}

/// Requires directive configuration
#[derive(Debug, Clone)]
pub struct RequiresField {
    pub field_name: String,
    pub required_fields: Vec<String>,
}

/// Provides directive configuration
#[derive(Debug, Clone)]
pub struct ProvidesField {
    pub field_name: String,
    pub provided_fields: Vec<String>,
}

/// Federation schema builder
#[derive(Debug)]
pub struct FederationSchemaBuilder {
    /// Base schema content
    _schema_content: String,
    /// Federation version
    version: FederationVersion,
    /// Entity types with their keys
    entities: HashMap<String, Vec<EntityKey>>,
    /// External fields
    external_fields: HashMap<String, Vec<ExternalField>>,
    /// Requires directives
    requires_fields: HashMap<String, Vec<RequiresField>>,
    /// Provides directives
    provides_fields: HashMap<String, Vec<ProvidesField>>,
    /// Shareable types (Federation v2)
    shareable_types: HashSet<String>,
    /// Override fields (Federation v2)
    override_fields: HashMap<String, String>, // field_name -> from_subgraph
}

impl FederationSchemaBuilder {
    /// Create a new Federation schema builder
    pub fn new(version: FederationVersion) -> Self {
        Self {
            _schema_content: String::new(),
            version,
            entities: HashMap::new(),
            external_fields: HashMap::new(),
            requires_fields: HashMap::new(),
            provides_fields: HashMap::new(),
            shareable_types: HashSet::new(),
            override_fields: HashMap::new(),
        }
    }

    /// Add an entity with its key fields
    pub fn add_entity(mut self, type_name: impl Into<String>, key: EntityKey) -> Self {
        let type_name = type_name.into();
        self.entities
            .entry(type_name)
            .or_insert_with(Vec::new)
            .push(key);
        self
    }

    /// Mark a field as external
    pub fn add_external_field(
        mut self,
        type_name: impl Into<String>,
        field_name: impl Into<String>,
    ) -> Self {
        let external = ExternalField {
            field_name: field_name.into(),
            type_name: type_name.into(),
        };
        self.external_fields
            .entry(external.type_name.clone())
            .or_insert_with(Vec::new)
            .push(external);
        self
    }

    /// Add a @requires directive
    pub fn add_requires(
        mut self,
        type_name: impl Into<String>,
        field_name: impl Into<String>,
        required_fields: Vec<String>,
    ) -> Self {
        let requires = RequiresField {
            field_name: field_name.into(),
            required_fields,
        };
        self.requires_fields
            .entry(type_name.into())
            .or_insert_with(Vec::new)
            .push(requires);
        self
    }

    /// Add a @provides directive
    pub fn add_provides(
        mut self,
        type_name: impl Into<String>,
        field_name: impl Into<String>,
        provided_fields: Vec<String>,
    ) -> Self {
        let provides = ProvidesField {
            field_name: field_name.into(),
            provided_fields,
        };
        self.provides_fields
            .entry(type_name.into())
            .or_insert_with(Vec::new)
            .push(provides);
        self
    }

    /// Mark a type as shareable (Federation v2)
    pub fn add_shareable(mut self, type_name: impl Into<String>) -> Self {
        if self.version == FederationVersion::V2 {
            self.shareable_types.insert(type_name.into());
        }
        self
    }

    /// Add an @override directive (Federation v2)
    pub fn add_override(
        mut self,
        field_name: impl Into<String>,
        from_subgraph: impl Into<String>,
    ) -> Self {
        if self.version == FederationVersion::V2 {
            self.override_fields
                .insert(field_name.into(), from_subgraph.into());
        }
        self
    }

    /// Build the Federation schema
    pub fn build(self) -> Result<FederationSchema> {
        let mut sdl = String::new();

        // Add Federation schema extension
        self.write_federation_schema_extension(&mut sdl)?;

        // Add entity definitions
        for (type_name, keys) in &self.entities {
            self.write_entity_type(&mut sdl, type_name, keys)?;
        }

        // Add _entities and _service queries
        self.write_federation_queries(&mut sdl)?;

        Ok(FederationSchema {
            sdl,
            version: self.version,
            entities: self.entities.keys().cloned().collect(),
        })
    }

    /// Write Federation schema extension
    fn write_federation_schema_extension(&self, sdl: &mut String) -> Result<()> {
        match self.version {
            FederationVersion::V1 => {
                writeln!(sdl, "extend schema @link(url: \"https://specs.apollo.dev/federation/v1.0\")")?;
            }
            FederationVersion::V2 => {
                writeln!(sdl, "extend schema")?;
                writeln!(
                    sdl,
                    "  @link(url: \"https://specs.apollo.dev/federation/v2.0\","
                )?;
                writeln!(sdl, "        import: [\"@key\", \"@external\", \"@requires\", \"@provides\", \"@shareable\", \"@override\"])")?;
            }
        }
        writeln!(sdl)?;
        Ok(())
    }

    /// Write entity type definition
    fn write_entity_type(
        &self,
        sdl: &mut String,
        type_name: &str,
        keys: &[EntityKey],
    ) -> Result<()> {
        // Write key directives
        for key in keys {
            writeln!(sdl, "type {} {}", type_name, key.to_directive_string())?;
        }

        // Add shareable if applicable
        if self.shareable_types.contains(type_name) {
            writeln!(sdl, "  @shareable")?;
        }

        writeln!(sdl, " {{")?;

        // Write fields (placeholder - would be filled from actual schema)
        writeln!(sdl, "  id: ID!")?;

        writeln!(sdl, "}}")?;
        writeln!(sdl)?;
        Ok(())
    }

    /// Write Federation queries (_entities and _service)
    fn write_federation_queries(&self, sdl: &mut String) -> Result<()> {
        writeln!(sdl, "# Federation queries")?;
        writeln!(sdl, "extend type Query {{")?;

        // _entities query
        writeln!(sdl, "  _entities(representations: [_Any!]!): [_Entity]!")?;

        // _service query
        writeln!(sdl, "  _service: _Service!")?;

        writeln!(sdl, "}}")?;
        writeln!(sdl)?;

        // Add Federation scalar and types
        writeln!(sdl, "scalar _Any")?;
        writeln!(sdl, "scalar _FieldSet")?;
        writeln!(sdl)?;

        writeln!(sdl, "union _Entity =")?;
        let entity_names: Vec<_> = self.entities.keys().collect();
        for (i, entity) in entity_names.iter().enumerate() {
            if i > 0 {
                write!(sdl, " | ")?;
            }
            write!(sdl, "{}", entity)?;
        }
        writeln!(sdl)?;
        writeln!(sdl)?;

        writeln!(sdl, "type _Service {{")?;
        writeln!(sdl, "  sdl: String!")?;
        writeln!(sdl, "}}")?;
        writeln!(sdl)?;

        Ok(())
    }
}

/// Generated Federation schema
#[derive(Debug, Clone)]
pub struct FederationSchema {
    /// Schema Definition Language (SDL)
    pub sdl: String,
    /// Federation version
    pub version: FederationVersion,
    /// Entity type names
    pub entities: Vec<String>,
}

impl FederationSchema {
    /// Get the SDL string
    pub fn to_sdl(&self) -> &str {
        &self.sdl
    }

    /// Check if a type is an entity
    pub fn is_entity(&self, type_name: &str) -> bool {
        self.entities.iter().any(|e| e == type_name)
    }
}

/// Federation entity resolver
pub trait EntityResolver: Send + Sync {
    /// Resolve an entity from its representation
    fn resolve_entity(
        &self,
        type_name: &str,
        representation: HashMap<String, serde_json::Value>,
    ) -> Result<Option<serde_json::Value>>;
}

/// Federation service configuration
#[derive(Debug, Clone)]
pub struct FederationServiceConfig {
    /// Service name
    pub name: String,
    /// Service version
    pub version: String,
    /// Enable entity caching
    pub enable_entity_cache: bool,
    /// Cache TTL in seconds
    pub cache_ttl: u64,
}

impl Default for FederationServiceConfig {
    fn default() -> Self {
        Self {
            name: "oxirs-federation-service".to_string(),
            version: "1.0.0".to_string(),
            enable_entity_cache: true,
            cache_ttl: 300, // 5 minutes
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_entity_key_directive() {
        let key = EntityKey::new(vec!["id".to_string()]);
        assert_eq!(key.to_directive_string(), "@key(fields: \"id\")");

        let key = EntityKey::new(vec!["id".to_string(), "email".to_string()]);
        assert_eq!(key.to_directive_string(), "@key(fields: \"id email\")");
    }

    #[test]
    fn test_entity_key_non_resolvable() {
        let key = EntityKey::new(vec!["id".to_string()]).with_resolvable(false);
        assert_eq!(
            key.to_directive_string(),
            "@key(fields: \"id\", resolvable: false)"
        );
    }

    #[test]
    fn test_federation_schema_builder() {
        let schema = FederationSchemaBuilder::new(FederationVersion::V2)
            .add_entity("User", EntityKey::new(vec!["id".to_string()]))
            .add_entity("Product", EntityKey::new(vec!["sku".to_string()]))
            .build()
            .expect("should succeed");

        assert_eq!(schema.version, FederationVersion::V2);
        assert_eq!(schema.entities.len(), 2);
        assert!(schema.is_entity("User"));
        assert!(schema.is_entity("Product"));
        assert!(!schema.is_entity("Order"));
    }

    #[test]
    fn test_federation_schema_sdl_generation() {
        let schema = FederationSchemaBuilder::new(FederationVersion::V2)
            .add_entity("User", EntityKey::new(vec!["id".to_string()]))
            .build()
            .expect("should succeed");

        let sdl = schema.to_sdl();
        assert!(sdl.contains("@link"));
        assert!(sdl.contains("federation/v2.0"));
        assert!(sdl.contains("_entities"));
        assert!(sdl.contains("_service"));
        assert!(sdl.contains("type User"));
    }
}