this-rs 0.0.9

Framework for building complex multi-entity REST and GraphQL APIs with many relationships
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
# GraphQL Implementation Architecture

This document explains the technical implementation of GraphQL exposure in `this-rs`, including the dynamic schema generation and custom executor.

## ๐Ÿ“ Overview

The GraphQL implementation is **completely modular** and separate from the core framework:

```
src/server/exposure/
โ”œโ”€โ”€ rest/
โ”‚   โ””โ”€โ”€ mod.rs         # REST API exposure
โ””โ”€โ”€ graphql/
    โ”œโ”€โ”€ mod.rs         # GraphQL exposure entry point
    โ”œโ”€โ”€ schema.rs      # Legacy async-graphql schema (unused)
    โ”œโ”€โ”€ schema_generator.rs  # Dynamic SDL schema generator
    โ”œโ”€โ”€ dynamic_schema.rs    # Legacy dynamic schema (unused)
    โ””โ”€โ”€ executor/
        โ”œโ”€โ”€ mod.rs     # Executor module entry
        โ”œโ”€โ”€ core.rs    # Main executor orchestration
        โ”œโ”€โ”€ query_executor.rs    # Query resolution
        โ”œโ”€โ”€ mutation_executor.rs # CRUD mutations
        โ”œโ”€โ”€ link_mutations.rs    # Link-specific mutations
        โ”œโ”€โ”€ field_resolver.rs    # Field and relation resolution
        โ””โ”€โ”€ utils.rs   # Utility functions
```

## ๐Ÿ—๏ธ Architecture Components

### 1. GraphQLExposure

**Location**: `src/server/exposure/graphql/mod.rs`

The entry point that builds the GraphQL router. It's completely separate from REST exposure.

```rust
pub struct GraphQLExposure;

impl GraphQLExposure {
    pub fn build_router(host: Arc<ServerHost>) -> Result<Router> {
        Router::new()
            .route("/graphql", post(graphql_handler_custom))
            .route("/graphql/playground", get(graphql_playground))
            .route("/graphql/schema", get(graphql_dynamic_schema))
            .layer(Extension(host))
    }
}
```

**Endpoints**:
- `POST /graphql` - GraphQL query/mutation endpoint
- `GET /graphql/playground` - Interactive GraphQL playground
- `GET /graphql/schema` - SDL schema export

### 2. SchemaGenerator

**Location**: `src/server/exposure/graphql/schema_generator.rs`

Dynamically generates GraphQL SDL (Schema Definition Language) from:
- Registered entities in `ServerHost`
- Field discovery via `EntityFetcher::get_sample_entity()` or `list_as_json()`
- Relations from `links.yaml` configuration

**Key Methods**:
- `generate_sdl()` - Orchestrates full schema generation
- `generate_entity_type()` - Generates type definition for an entity
- `generate_query_root()` - Generates Query type with all entity queries
- `generate_mutation_root()` - Generates Mutation type with CRUD and link operations
- `get_relations_for()` - Extracts relations for an entity from config

**Example Output**:
```graphql
type Order {
  id: ID!
  number: String!
  customerName: String!
  amount: Float!
  invoices: [Invoice!]!  # From links.yaml
}

type Query {
  order(id: ID!): Order
  orders(limit: Int, offset: Int): [Order!]!
}

type Mutation {
  createOrder(data: JSON!): Order!
  updateOrder(id: ID!, data: JSON!): Order!
  deleteOrder(id: ID!): Boolean!
  createInvoiceForOrder(parentId: ID!, data: JSON!): Invoice!
}
```

### 3. GraphQLExecutor

**Location**: `src/server/exposure/graphql/executor/core.rs`

A **custom GraphQL executor** that:
- Parses incoming GraphQL queries using `graphql-parser`
- Executes queries against the dynamic schema
- Resolves fields dynamically using `EntityFetcher` and `EntityCreator`
- Handles relations via `LinkService`

**Why Custom?**: `async-graphql` requires compile-time type definitions. Our dynamic schema requires runtime execution, so we implemented a custom executor.

**Structure**:
```rust
pub struct GraphQLExecutor {
    host: Arc<ServerHost>,
    schema_sdl: String,  // Generated SDL (currently unused but stored)
}

impl GraphQLExecutor {
    pub async fn execute(&self, query: &str, variables: Option<HashMap<String, Value>>) -> Result<Value>;
    async fn execute_document(&self, doc: &Document, variables: HashMap<String, Value>) -> Result<Value>;
    async fn execute_query(&self, selections: &[Selection], variables: &HashMap<String, Value>) -> Result<Value>;
    async fn execute_mutation(&self, selections: &[Selection], variables: &HashMap<String, Value>) -> Result<Value>;
}
```

### 4. Query Executor

**Location**: `src/server/exposure/graphql/executor/query_executor.rs`

Resolves GraphQL queries:

- **Plural queries** (`orders`, `invoices`): Calls `EntityFetcher::list_as_json()` with pagination
- **Singular queries** (`order`, `invoice`): Calls `EntityFetcher::fetch_as_json()` with UUID

```rust
pub async fn resolve_query_field(
    host: &Arc<ServerHost>,
    field: &Field<'_, String>,
) -> Result<Value> {
    // Check if plural query
    if let Some(entity_type) = get_entity_type_from_plural(host, field_name) {
        let entities = fetcher.list_as_json(limit, offset).await?;
        // Resolve sub-fields...
    }
    
    // Check if singular query
    if let Some(entity_type) = get_entity_type_from_singular(host, field_name) {
        let entity = fetcher.fetch_as_json(&uuid).await?;
        // Resolve sub-fields...
    }
}
```

### 5. Mutation Executor

**Location**: `src/server/exposure/graphql/executor/mutation_executor.rs`

Handles all CRUD mutations:

- `create{Entity}` - Calls `EntityCreator::create_from_json()`
- `update{Entity}` - Calls `EntityCreator::update_from_json()`
- `delete{Entity}` - Calls `EntityCreator::delete()`

Dispatches to specialized modules for link mutations.

### 6. Link Mutations

**Location**: `src/server/exposure/graphql/executor/link_mutations.rs`

Specialized mutations for link management:

- `createLink` - Generic link creation
- `deleteLink` - Generic link deletion
- `create{Target}For{Source}` - Create entity + link (e.g., `createInvoiceForOrder`)
- `link{Target}To{Source}` - Link existing entities (e.g., `linkPaymentToInvoice`)
- `unlink{Target}From{Source}` - Remove link (e.g., `unlinkPaymentFromInvoice`)

**Convention**: Mutation names follow patterns:
- `create{Target}For{Source}` โ†’ Creates target, links to source
- `link{Source}To{Target}` โ†’ Links source to target
- `unlink{Source}From{Target}` โ†’ Removes link from source to target

### 7. Field Resolver

**Location**: `src/server/exposure/graphql/executor/field_resolver.rs`

Resolves entity fields and relations:

- **Direct fields**: Extracts from JSON entity data
- **Relations**: Uses `LinkService` to find links, then `EntityFetcher` to resolve entities
- **Recursion**: Uses `BoxFuture` to handle nested selections recursively

**Key Functions**:
```rust
pub async fn resolve_entity_fields(
    host: &Arc<ServerHost>,
    entity: Value,
    selections: &[Selection],
    entity_type: &str,
) -> Result<Value>

async fn resolve_relation_field_inner(
    host: &Arc<ServerHost>,
    entity: &serde_json::Map<String, Value>,
    field: &Field,
    entity_type: &str,
) -> Result<Option<Value>>
```

**Relation Resolution Logic**:
1. Check if field name matches `forward_route_name` in links config โ†’ Forward relation
2. Check if field name matches `reverse_route_name` in links config โ†’ Reverse relation
3. Use `LinkService::find_by_source()` or `find_by_target()` to get links
4. Fetch linked entities via `EntityFetcher::fetch_as_json()`
5. Recursively resolve nested selections

## ๐Ÿ”„ Execution Flow

### Query Execution

```
1. HTTP Request โ†’ POST /graphql
   โ†“
2. graphql_handler_custom() โ†’ Creates GraphQLExecutor
   โ†“
3. GraphQLExecutor::execute()
   โ†“ Parse query with graphql-parser
   โ†“
4. execute_document() โ†’ Detect operation type
   โ†“
5. execute_query() โ†’ For each field
   โ†“
6. query_executor::resolve_query_field()
   โ†“ Identify entity type (plural/singular)
   โ†“
7. EntityFetcher::list_as_json() or fetch_as_json()
   โ†“
8. field_resolver::resolve_entity_fields()
   โ†“ For each selection
   โ†“
9a. Direct field โ†’ Extract from JSON
9b. Relation field โ†’ resolve_relation_field_inner()
   โ†“
10. LinkService::find_by_source() / find_by_target()
    โ†“
11. EntityFetcher::fetch_as_json() for each linked entity
    โ†“
12. Recursive resolve_entity_fields() for nested selections
    โ†“
13. Return resolved JSON
```

### Mutation Execution

```
1. HTTP Request โ†’ POST /graphql (mutation)
   โ†“
2. GraphQLExecutor::execute()
   โ†“
3. execute_mutation() โ†’ For each field
   โ†“
4. mutation_executor::resolve_mutation_field()
   โ†“ Dispatch by mutation name pattern
   โ†“
5a. CRUD mutation โ†’ mutation_executor::create/update/delete_entity_mutation()
    โ†“ EntityCreator::create_from_json() / update_from_json() / delete()
    
5b. Link mutation โ†’ link_mutations::*_mutation()
    โ†“ LinkService::create() / delete()
    โ†“ LinkService::find_by_source() (for unlink)
    
6. field_resolver::resolve_entity_fields() โ†’ Resolve sub-selections
   โ†“
7. Return resolved entity/link
```

## ๐ŸŽฏ Design Decisions

### Why Custom Executor Instead of async-graphql?

**Problem**: `async-graphql` requires compile-time type definitions. Our entities are defined at runtime.

**Solution**: Custom executor that:
- Parses queries at runtime using `graphql-parser`
- Resolves fields dynamically using runtime services
- Works with any entity type without code generation

**Trade-offs**:
- โœ… 100% dynamic, no code generation needed
- โœ… Works with any entity automatically
- โŒ More complex than using async-graphql
- โŒ Manual query parsing and execution

### Why JSON for Mutation Data?

**Decision**: Use `JSON!` scalar for mutation `data` argument instead of strongly-typed input types.

**Rationale**:
- Entities are defined at runtime
- Cannot generate input types at compile time
- JSON provides flexibility for any entity structure
- Matches REST API patterns

**Trade-offs**:
- โœ… Maximum flexibility
- โœ… No code generation needed
- โŒ Less type safety in GraphQL schema
- โŒ No autocomplete for data structure

### Why Separate Executor Modules?

**Decision**: Split executor into 6 modules (core, query, mutation, links, fields, utils).

**Rationale**:
- Original `executor.rs` was 751 lines
- Better maintainability and testability
- Clear separation of concerns
- Easier to add features

**Structure**:
- `core.rs` (~122 lines) - Orchestration
- `query_executor.rs` (~96 lines) - Query resolution
- `mutation_executor.rs` (~149 lines) - CRUD mutations
- `link_mutations.rs` (~241 lines) - Link operations
- `field_resolver.rs` (~177 lines) - Field/relation resolution
- `utils.rs` (~132 lines) - Utilities

## ๐Ÿ”ง Extension Points

### Adding New Query Types

1. Add query to `SchemaGenerator::generate_query_root()`
2. Add resolver in `query_executor.rs`

### Adding New Mutation Types

1. Add mutation to `SchemaGenerator::generate_mutation_root()`
2. Add handler in `mutation_executor.rs` or `link_mutations.rs`

### Adding New Field Resolvers

1. Extend `field_resolver.rs` with new resolution logic
2. Update `resolve_entity_fields_impl()` to handle new field types

## ๐Ÿ“Š Performance Considerations

### Schema Generation

**Current**: Schema is generated on each request to `/graphql/schema`.

**Future Optimization**: Cache generated SDL and invalidate when entities change.

### Query Execution

**Current**: Executor created per request.

**Future Optimization**: Cache executor instance (schema doesn't change at runtime).

### Field Resolution

**Current**: Sequential fetching of related entities.

**Future Optimization**: Batch fetching using DataLoader pattern.

### Nested Queries

**Current**: Recursive resolution may fetch same entity multiple times.

**Future Optimization**: Add query depth limit and entity fetching cache.

## ๐Ÿงช Testing

Each executor module can be tested independently:

```rust
#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_utils_pluralize() {
        assert_eq!(utils::pluralize("order"), "orders");
        assert_eq!(utils::pluralize("company"), "companies");
    }
    
    #[tokio::test]
    async fn test_query_resolution() {
        let host = create_test_host();
        let field = parse_query_field("orders");
        let result = query_executor::resolve_query_field(&host, &field).await?;
        // Assert result...
    }
}
```

## ๐Ÿ”ฎ Future Enhancements

### Planned Features

1. **Schema Caching**: Cache generated SDL
2. **Executor Caching**: Reuse executor instance
3. **DataLoader**: Batch entity fetching
4. **Query Complexity Analysis**: Prevent expensive queries
5. **Field-Level Authorization**: Integrate with auth system
6. **Subscriptions**: WebSocket support for real-time updates
7. **Directives Support**: `@deprecated`, `@skip`, `@include`
8. **Input Type Generation**: Strongly-typed input types (if possible)

### Technical Debt

1. **Legacy Files**: `schema.rs` and `dynamic_schema.rs` are unused (can be removed)
2. **Error Handling**: More structured GraphQL errors
3. **Performance**: Add benchmarks and optimize hot paths
4. **Documentation**: Add inline documentation for complex logic

## ๐Ÿ“š Related Files

- [GraphQL Guide]../guides/GRAPHQL.md - User guide
- [Architecture Overview]./ARCHITECTURE.md - Overall framework architecture
- [Server Builder]./SERVER_BUILDER_IMPLEMENTATION.md - Server construction
- [GraphQL Example]../../examples/microservice/README_GRAPHQL.md - Complete example

## ๐ŸŽฏ Summary

The GraphQL implementation is:

- โœ… **100% Dynamic** - No compile-time code generation
- โœ… **Modular** - Clean separation of concerns
- โœ… **Extensible** - Easy to add new features
- โœ… **Type-Safe** - Uses Rust types internally
- โœ… **Performant** - Efficient execution with room for optimization

**Key Innovation**: Custom executor that enables truly dynamic GraphQL without sacrificing type safety or developer experience.