postrust-graphql 0.2.1

GraphQL support for Postrust
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
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
//! Mutation input types for inserts and updates.
//!
//! Provides input type generation for GraphQL mutations based on table metadata.

use crate::types::{pg_type_to_graphql, GraphQLType};
use postrust_core::schema_cache::{Column, Table};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Represents a field in an insert input type.
#[derive(Debug, Clone)]
pub struct InsertField {
    /// Field name
    pub name: String,
    /// GraphQL type
    pub graphql_type: GraphQLType,
    /// Whether the field is required (no default value and not nullable)
    pub required: bool,
    /// Field description
    pub description: Option<String>,
}

impl InsertField {
    /// Create an InsertField from a column.
    pub fn from_column(column: &Column) -> Self {
        let graphql_type = pg_type_to_graphql(&column.nominal_type);

        // A field is required if:
        // 1. It's not nullable AND
        // 2. It has no default value AND
        // 3. It's not a primary key with serial/auto-increment default
        let has_auto_default = column.default.as_ref().map_or(false, |d| {
            d.contains("nextval") || d.contains("gen_random_uuid")
        });

        let required = !column.nullable && column.default.is_none() && !has_auto_default;

        Self {
            name: column.name.clone(),
            description: column.description.clone(),
            graphql_type,
            required,
        }
    }

    /// Get the GraphQL type string for input.
    pub fn type_string(&self) -> String {
        let base = format!("{}", self.graphql_type);
        if self.required {
            format!("{}!", base)
        } else {
            base
        }
    }
}

/// Represents a field in an update input type.
#[derive(Debug, Clone)]
pub struct UpdateField {
    /// Field name
    pub name: String,
    /// GraphQL type
    pub graphql_type: GraphQLType,
    /// Field description
    pub description: Option<String>,
    /// Whether this is a primary key (cannot be updated)
    pub is_pk: bool,
}

impl UpdateField {
    /// Create an UpdateField from a column.
    pub fn from_column(column: &Column) -> Self {
        let graphql_type = pg_type_to_graphql(&column.nominal_type);

        Self {
            name: column.name.clone(),
            description: column.description.clone(),
            graphql_type,
            is_pk: column.is_pk,
        }
    }

    /// Get the GraphQL type string for input (always nullable for updates).
    pub fn type_string(&self) -> String {
        format!("{}", self.graphql_type)
    }

    /// Check if this field can be updated (non-PK fields only).
    pub fn is_updatable(&self) -> bool {
        !self.is_pk
    }
}

/// Represents an insert input type for a table.
#[derive(Debug, Clone)]
pub struct InsertInput {
    /// Table being inserted into
    pub table_name: String,
    /// GraphQL type name (e.g., "UsersInsertInput")
    pub type_name: String,
    /// Fields that can be inserted
    pub fields: Vec<InsertField>,
}

impl InsertInput {
    /// Create an InsertInput from a table.
    pub fn from_table(table: &Table) -> Self {
        let type_name = format!("{}InsertInput", to_pascal_case(&table.name));

        let fields = table
            .columns
            .values()
            .map(InsertField::from_column)
            .collect();

        Self {
            table_name: table.name.clone(),
            type_name,
            fields,
        }
    }

    /// Get required fields.
    pub fn required_fields(&self) -> Vec<&InsertField> {
        self.fields.iter().filter(|f| f.required).collect()
    }

    /// Get optional fields.
    pub fn optional_fields(&self) -> Vec<&InsertField> {
        self.fields.iter().filter(|f| !f.required).collect()
    }

    /// Check if the table has any required fields.
    pub fn has_required_fields(&self) -> bool {
        self.fields.iter().any(|f| f.required)
    }
}

/// Represents an update input type for a table.
#[derive(Debug, Clone)]
pub struct UpdateInput {
    /// Table being updated
    pub table_name: String,
    /// GraphQL type name (e.g., "UsersSetInput")
    pub type_name: String,
    /// Fields that can be updated
    pub fields: Vec<UpdateField>,
}

impl UpdateInput {
    /// Create an UpdateInput from a table.
    pub fn from_table(table: &Table) -> Self {
        let type_name = format!("{}SetInput", to_pascal_case(&table.name));

        let fields = table
            .columns
            .values()
            .filter(|c| !c.is_pk) // Exclude primary keys from update
            .map(UpdateField::from_column)
            .collect();

        Self {
            table_name: table.name.clone(),
            type_name,
            fields,
        }
    }

    /// Get updatable fields.
    pub fn updatable_fields(&self) -> Vec<&UpdateField> {
        self.fields.iter().filter(|f| f.is_updatable()).collect()
    }
}

/// A dynamic input value that can hold different types.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum InputValue {
    /// Null value
    Null,
    /// Boolean value
    Bool(bool),
    /// Integer value
    Int(i64),
    /// Float value
    Float(f64),
    /// String value
    String(String),
    /// JSON object value
    Object(HashMap<String, InputValue>),
    /// JSON array value
    Array(Vec<InputValue>),
}

impl InputValue {
    /// Check if this is null.
    pub fn is_null(&self) -> bool {
        matches!(self, Self::Null)
    }

    /// Try to get as string.
    pub fn as_string(&self) -> Option<&str> {
        match self {
            Self::String(s) => Some(s),
            _ => None,
        }
    }

    /// Try to get as i64.
    pub fn as_int(&self) -> Option<i64> {
        match self {
            Self::Int(i) => Some(*i),
            _ => None,
        }
    }

    /// Try to get as f64.
    pub fn as_float(&self) -> Option<f64> {
        match self {
            Self::Float(f) => Some(*f),
            Self::Int(i) => Some(*i as f64),
            _ => None,
        }
    }

    /// Try to get as bool.
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Self::Bool(b) => Some(*b),
            _ => None,
        }
    }

    /// Convert to SQL string representation.
    pub fn to_sql_string(&self) -> String {
        match self {
            Self::Null => "NULL".to_string(),
            Self::Bool(b) => if *b { "true" } else { "false" }.to_string(),
            Self::Int(i) => i.to_string(),
            Self::Float(f) => f.to_string(),
            Self::String(s) => s.clone(),
            Self::Object(o) => serde_json::to_string(o).unwrap_or_default(),
            Self::Array(a) => serde_json::to_string(a).unwrap_or_default(),
        }
    }
}

/// Helper to convert snake_case to PascalCase.
fn to_pascal_case(s: &str) -> String {
    s.split('_')
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
                None => String::new(),
            }
        })
        .collect()
}

/// Check if a table is insertable based on its permissions.
pub fn is_insertable(table: &Table) -> bool {
    table.insertable
}

/// Check if a table is updatable based on its permissions.
pub fn is_updatable(table: &Table) -> bool {
    table.updatable
}

/// Check if a table is deletable based on its permissions.
pub fn is_deletable(table: &Table) -> bool {
    table.deletable
}

#[cfg(test)]
mod tests {
    use super::*;
    use indexmap::IndexMap;
    use pretty_assertions::assert_eq;

    fn create_test_table() -> Table {
        let mut columns = IndexMap::new();
        columns.insert(
            "id".into(),
            Column {
                name: "id".into(),
                description: Some("Primary key".into()),
                nullable: false,
                data_type: "integer".into(),
                nominal_type: "int4".into(),
                max_len: None,
                default: Some("nextval('users_id_seq')".into()),
                enum_values: vec![],
                is_pk: true,
                position: 1,
            },
        );
        columns.insert(
            "name".into(),
            Column {
                name: "name".into(),
                description: Some("User name".into()),
                nullable: false,
                data_type: "text".into(),
                nominal_type: "text".into(),
                max_len: None,
                default: None,
                enum_values: vec![],
                is_pk: false,
                position: 2,
            },
        );
        columns.insert(
            "email".into(),
            Column {
                name: "email".into(),
                description: None,
                nullable: true,
                data_type: "text".into(),
                nominal_type: "text".into(),
                max_len: None,
                default: None,
                enum_values: vec![],
                is_pk: false,
                position: 3,
            },
        );
        columns.insert(
            "created_at".into(),
            Column {
                name: "created_at".into(),
                description: None,
                nullable: false,
                data_type: "timestamptz".into(),
                nominal_type: "timestamptz".into(),
                max_len: None,
                default: Some("now()".into()),
                enum_values: vec![],
                is_pk: false,
                position: 4,
            },
        );

        Table {
            schema: "public".into(),
            name: "users".into(),
            description: Some("User accounts".into()),
            is_view: false,
            insertable: true,
            updatable: true,
            deletable: true,
            pk_cols: vec!["id".into()],
            columns,
        }
    }

    fn create_readonly_table() -> Table {
        let mut table = create_test_table();
        table.insertable = false;
        table.updatable = false;
        table.deletable = false;
        table
    }

    // ============================================================================
    // InsertField Tests
    // ============================================================================

    #[test]
    fn test_insert_field_required() {
        let table = create_test_table();
        let name_col = table.columns.get("name").unwrap();
        let field = InsertField::from_column(name_col);

        assert_eq!(field.name, "name");
        assert!(field.required); // Not nullable, no default
        assert_eq!(field.type_string(), "String!");
    }

    #[test]
    fn test_insert_field_optional_nullable() {
        let table = create_test_table();
        let email_col = table.columns.get("email").unwrap();
        let field = InsertField::from_column(email_col);

        assert_eq!(field.name, "email");
        assert!(!field.required); // Nullable
        assert_eq!(field.type_string(), "String");
    }

    #[test]
    fn test_insert_field_optional_with_default() {
        let table = create_test_table();
        let created_at_col = table.columns.get("created_at").unwrap();
        let field = InsertField::from_column(created_at_col);

        assert_eq!(field.name, "created_at");
        assert!(!field.required); // Has default
        assert_eq!(field.type_string(), "DateTime");
    }

    #[test]
    fn test_insert_field_auto_pk() {
        let table = create_test_table();
        let id_col = table.columns.get("id").unwrap();
        let field = InsertField::from_column(id_col);

        assert_eq!(field.name, "id");
        assert!(!field.required); // Has auto-increment default
    }

    // ============================================================================
    // UpdateField Tests
    // ============================================================================

    #[test]
    fn test_update_field_non_pk() {
        let table = create_test_table();
        let name_col = table.columns.get("name").unwrap();
        let field = UpdateField::from_column(name_col);

        assert_eq!(field.name, "name");
        assert!(!field.is_pk);
        assert!(field.is_updatable());
        assert_eq!(field.type_string(), "String"); // All update fields are nullable
    }

    #[test]
    fn test_update_field_pk() {
        let table = create_test_table();
        let id_col = table.columns.get("id").unwrap();
        let field = UpdateField::from_column(id_col);

        assert_eq!(field.name, "id");
        assert!(field.is_pk);
        assert!(!field.is_updatable());
    }

    // ============================================================================
    // InsertInput Tests
    // ============================================================================

    #[test]
    fn test_insert_input_from_table() {
        let table = create_test_table();
        let input = InsertInput::from_table(&table);

        assert_eq!(input.table_name, "users");
        assert_eq!(input.type_name, "UsersInsertInput");
        assert_eq!(input.fields.len(), 4);
    }

    #[test]
    fn test_insert_input_required_fields() {
        let table = create_test_table();
        let input = InsertInput::from_table(&table);

        let required = input.required_fields();
        assert_eq!(required.len(), 1); // Only "name" is required
        assert_eq!(required[0].name, "name");
    }

    #[test]
    fn test_insert_input_optional_fields() {
        let table = create_test_table();
        let input = InsertInput::from_table(&table);

        let optional = input.optional_fields();
        assert_eq!(optional.len(), 3); // id, email, created_at
    }

    #[test]
    fn test_insert_input_has_required_fields() {
        let table = create_test_table();
        let input = InsertInput::from_table(&table);

        assert!(input.has_required_fields());
    }

    // ============================================================================
    // UpdateInput Tests
    // ============================================================================

    #[test]
    fn test_update_input_from_table() {
        let table = create_test_table();
        let input = UpdateInput::from_table(&table);

        assert_eq!(input.table_name, "users");
        assert_eq!(input.type_name, "UsersSetInput");
        assert_eq!(input.fields.len(), 3); // Excludes PK
    }

    #[test]
    fn test_update_input_excludes_pk() {
        let table = create_test_table();
        let input = UpdateInput::from_table(&table);

        let field_names: Vec<_> = input.fields.iter().map(|f| f.name.as_str()).collect();
        assert!(!field_names.contains(&"id"));
    }

    #[test]
    fn test_update_input_updatable_fields() {
        let table = create_test_table();
        let input = UpdateInput::from_table(&table);

        let updatable = input.updatable_fields();
        assert_eq!(updatable.len(), 3);
    }

    // ============================================================================
    // InputValue Tests
    // ============================================================================

    #[test]
    fn test_input_value_null() {
        let value = InputValue::Null;
        assert!(value.is_null());
        assert_eq!(value.to_sql_string(), "NULL");
    }

    #[test]
    fn test_input_value_bool() {
        let value = InputValue::Bool(true);
        assert_eq!(value.as_bool(), Some(true));
        assert_eq!(value.to_sql_string(), "true");

        let value = InputValue::Bool(false);
        assert_eq!(value.to_sql_string(), "false");
    }

    #[test]
    fn test_input_value_int() {
        let value = InputValue::Int(42);
        assert_eq!(value.as_int(), Some(42));
        assert_eq!(value.as_float(), Some(42.0)); // Can coerce to float
        assert_eq!(value.to_sql_string(), "42");
    }

    #[test]
    fn test_input_value_float() {
        let value = InputValue::Float(3.14);
        assert_eq!(value.as_float(), Some(3.14));
        assert_eq!(value.to_sql_string(), "3.14");
    }

    #[test]
    fn test_input_value_string() {
        let value = InputValue::String("hello".to_string());
        assert_eq!(value.as_string(), Some("hello"));
        assert_eq!(value.to_sql_string(), "hello");
    }

    // ============================================================================
    // Table Permission Tests
    // ============================================================================

    #[test]
    fn test_is_insertable() {
        let table = create_test_table();
        assert!(is_insertable(&table));

        let readonly = create_readonly_table();
        assert!(!is_insertable(&readonly));
    }

    #[test]
    fn test_is_updatable() {
        let table = create_test_table();
        assert!(is_updatable(&table));

        let readonly = create_readonly_table();
        assert!(!is_updatable(&readonly));
    }

    #[test]
    fn test_is_deletable() {
        let table = create_test_table();
        assert!(is_deletable(&table));

        let readonly = create_readonly_table();
        assert!(!is_deletable(&readonly));
    }

    // ============================================================================
    // PascalCase Tests
    // ============================================================================

    #[test]
    fn test_to_pascal_case() {
        assert_eq!(to_pascal_case("users"), "Users");
        assert_eq!(to_pascal_case("user_accounts"), "UserAccounts");
        assert_eq!(to_pascal_case("my_table_name"), "MyTableName");
    }
}