fraiseql-cli 2.3.2

CLI tools for FraiseQL v2 - Schema compilation and development utilities
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
//! GraphQL → Protobuf type mapping and `.proto` file generation.

use std::collections::BTreeSet;

use fraiseql_core::{
    db::dialect::RowViewColumnType,
    schema::{CompiledSchema, FieldDefinition, FieldType},
};

/// Map a GraphQL type name to a Protobuf type name.
///
/// Returns the protobuf scalar or well-known type for the given GraphQL type.
/// Unknown types fall back to `"string"`.
///
/// # Examples
///
/// ```
/// use fraiseql_cli::codegen::proto_gen::graphql_to_proto_type;
///
/// assert_eq!(graphql_to_proto_type("String"), "string");
/// assert_eq!(graphql_to_proto_type("Int"), "int32");
/// assert_eq!(graphql_to_proto_type("DateTime"), "google.protobuf.Timestamp");
/// ```
#[must_use]
pub fn graphql_to_proto_type(graphql_type: &str) -> &'static str {
    match graphql_type {
        "String" => "string",
        "Int" => "int32",
        "Float" => "double",
        "Boolean" => "bool",
        "ID" => "string",
        "DateTime" => "google.protobuf.Timestamp",
        "Date" => "string",
        "BigInt" => "int64",
        "JSON" => "google.protobuf.Struct",
        _ => "string", // Custom scalars fall back to string
    }
}

/// Map a GraphQL type name to a [`RowViewColumnType`] for SQL view generation.
///
/// Used by the row-shaped view DDL generator to determine the target SQL type
/// for each field extracted from the JSON column.
///
/// # Examples
///
/// ```
/// use fraiseql_cli::codegen::proto_gen::graphql_to_row_view_type;
/// use fraiseql_core::db::dialect::RowViewColumnType;
///
/// assert_eq!(graphql_to_row_view_type("String"), RowViewColumnType::Text);
/// assert_eq!(graphql_to_row_view_type("Int"), RowViewColumnType::Int32);
/// ```
#[must_use]
pub fn graphql_to_row_view_type(graphql_type: &str) -> RowViewColumnType {
    match graphql_type {
        "String" => RowViewColumnType::Text,
        "Date" => RowViewColumnType::Date,
        "Int" => RowViewColumnType::Int32,
        "BigInt" => RowViewColumnType::Int64,
        "Float" => RowViewColumnType::Float64,
        "Boolean" => RowViewColumnType::Boolean,
        "ID" => RowViewColumnType::Uuid,
        "DateTime" => RowViewColumnType::Timestamptz,
        "JSON" => RowViewColumnType::Json,
        _ => RowViewColumnType::Text, // Custom scalars → text
    }
}

/// Returns `true` if the given protobuf type requires an import of a
/// well-known type `.proto` file.
#[must_use]
pub fn needs_well_known_import(proto_type: &str) -> bool {
    matches!(proto_type, "google.protobuf.Timestamp" | "google.protobuf.Struct")
}

/// Map a [`FieldType`] to a protobuf type string.
///
/// Handles scalars, lists (`repeated`), enums (referenced by name),
/// and object references (referenced by message name).
fn field_type_to_proto(ft: &FieldType) -> ProtoFieldType {
    match ft {
        FieldType::String => ProtoFieldType::scalar("string"),
        FieldType::Int => ProtoFieldType::scalar("int32"),
        FieldType::Float => ProtoFieldType::scalar("double"),
        FieldType::Boolean => ProtoFieldType::scalar("bool"),
        FieldType::Id | FieldType::Uuid => ProtoFieldType::scalar("string"),
        FieldType::DateTime => ProtoFieldType::scalar("google.protobuf.Timestamp"),
        FieldType::Date | FieldType::Time | FieldType::Decimal => ProtoFieldType::scalar("string"),
        FieldType::Json => ProtoFieldType::scalar("google.protobuf.Struct"),
        FieldType::Vector => ProtoFieldType::repeated("double"),
        FieldType::Scalar(_) => ProtoFieldType::scalar("string"),
        FieldType::Enum(name) => ProtoFieldType::scalar(name),
        FieldType::Object(name) | FieldType::Interface(name) | FieldType::Union(name) => {
            ProtoFieldType::scalar(name)
        },
        FieldType::Input(name) => ProtoFieldType::scalar(name),
        FieldType::List(inner) => {
            let inner_proto = field_type_to_proto(inner);
            ProtoFieldType::repeated(&inner_proto.type_name)
        },
        _ => ProtoFieldType::scalar("string"),
    }
}

/// Intermediate representation of a protobuf field type.
struct ProtoFieldType {
    type_name: String,
    repeated:  bool,
}

impl ProtoFieldType {
    fn scalar(name: &str) -> Self {
        Self {
            type_name: name.to_string(),
            repeated:  false,
        }
    }

    fn repeated(name: &str) -> Self {
        Self {
            type_name: name.to_string(),
            repeated:  true,
        }
    }
}

/// Generate a complete `.proto` file from a compiled schema.
///
/// Produces a proto3 service definition with:
/// - One message per GraphQL type (fields sorted alphabetically for stable numbering)
/// - One RPC per query (Get for single, List for list queries)
/// - One RPC per mutation (returns `MutationResponse`)
/// - Enum definitions from the schema
/// - Request/response wrapper messages
///
/// # Errors
///
/// Returns an error if the schema contains no types to expose.
pub fn generate_proto_file(
    schema: &CompiledSchema,
    package: &str,
    include_types: &[String],
    exclude_types: &[String],
) -> String {
    let mut out = String::new();
    let mut imports = BTreeSet::new();

    // Collect which types to expose
    let types: Vec<_> = schema
        .types
        .iter()
        .filter(|t| should_include_type(t.name.as_ref(), include_types, exclude_types))
        .collect();

    // Pre-scan for needed imports
    for td in &types {
        for field in &td.fields {
            let proto = field_type_to_proto(&field.field_type);
            if needs_well_known_import(&proto.type_name) {
                add_import_for_type(&proto.type_name, &mut imports);
            }
        }
    }
    // Scan query/mutation arguments too
    for q in &schema.queries {
        for arg in &q.arguments {
            let proto = field_type_to_proto(&arg.arg_type);
            if needs_well_known_import(&proto.type_name) {
                add_import_for_type(&proto.type_name, &mut imports);
            }
        }
    }

    // Header
    out.push_str("syntax = \"proto3\";\n\n");
    out.push_str(&format!("package {package};\n\n"));

    // Imports
    for imp in &imports {
        out.push_str(&format!("import \"{imp}\";\n"));
    }
    if !imports.is_empty() {
        out.push('\n');
    }

    // Enum definitions
    for enum_def in &schema.enums {
        generate_enum(&mut out, &enum_def.name, &enum_def.values);
    }

    // Type messages
    for td in &types {
        generate_message(&mut out, td.name.as_ref(), &td.fields);
    }

    // MutationResponse message (if any mutations exist)
    if !schema.mutations.is_empty() {
        out.push_str("message MutationResponse {\n");
        out.push_str("  bool success = 1;\n");
        out.push_str("  optional string id = 2;\n");
        out.push_str("  optional string error = 3;\n");
        out.push_str("}\n\n");
    }

    // Request/response messages for queries
    for q in &schema.queries {
        if !types.iter().any(|t| t.name == q.return_type) {
            continue;
        }
        generate_query_messages(&mut out, q);
    }

    // Request messages for mutations
    for m in &schema.mutations {
        generate_mutation_request_message(&mut out, m);
    }

    // Service definition
    let service_name = package_to_service(package);
    out.push_str(&format!("service {service_name} {{\n"));

    for q in &schema.queries {
        if !types.iter().any(|t| t.name == q.return_type) {
            continue;
        }
        let rpc_name = to_pascal_case(&q.name);
        let req = format!("{rpc_name}Request");
        if q.returns_list {
            // Server-streaming RPC: each response frame is a single entity message.
            out.push_str(&format!("  rpc {rpc_name}({req}) returns (stream {});\n", q.return_type));
        } else {
            out.push_str(&format!("  rpc {rpc_name}({req}) returns ({});\n", q.return_type));
        }
    }

    for m in &schema.mutations {
        let rpc_name = to_pascal_case(&m.name);
        let req = format!("{rpc_name}Request");
        out.push_str(&format!("  rpc {rpc_name}({req}) returns (MutationResponse);\n"));
    }

    out.push_str("}\n");

    out
}

/// Generate a protobuf message from a type's fields.
///
/// Fields are sorted alphabetically for deterministic field numbering.
fn generate_message(out: &mut String, name: &str, fields: &[FieldDefinition]) {
    out.push_str(&format!("message {name} {{\n"));

    let mut sorted_fields: Vec<&FieldDefinition> = fields.iter().collect();
    sorted_fields.sort_by(|a, b| a.name.as_ref().cmp(b.name.as_ref()));

    for (i, field) in sorted_fields.iter().enumerate() {
        let proto = field_type_to_proto(&field.field_type);
        let field_num = i + 1;
        let optional = if field.nullable && !proto.repeated {
            "optional "
        } else {
            ""
        };
        let repeated = if proto.repeated { "repeated " } else { "" };
        out.push_str(&format!(
            "  {optional}{repeated}{} {} = {field_num};\n",
            proto.type_name, field.name
        ));
    }

    out.push_str("}\n\n");
}

/// Generate a protobuf enum definition.
fn generate_enum(
    out: &mut String,
    name: &str,
    values: &[fraiseql_core::schema::EnumValueDefinition],
) {
    out.push_str(&format!("enum {name} {{\n"));
    out.push_str(&format!("  {}_UNSPECIFIED = 0;\n", to_screaming_snake(name)));

    for (i, val) in values.iter().enumerate() {
        out.push_str(&format!("  {} = {};\n", val.name, i + 1));
    }

    out.push_str("}\n\n");
}

/// Generate request/response messages for a query.
fn generate_query_messages(out: &mut String, q: &fraiseql_core::schema::QueryDefinition) {
    let rpc_name = to_pascal_case(&q.name);

    // Request message
    out.push_str(&format!("message {rpc_name}Request {{\n"));

    let mut sorted_args: Vec<_> = q.arguments.iter().collect();
    sorted_args.sort_by(|a, b| a.name.cmp(&b.name));

    for (i, arg) in sorted_args.iter().enumerate() {
        let proto = field_type_to_proto(&arg.arg_type);
        let optional = if arg.nullable && !proto.repeated {
            "optional "
        } else {
            ""
        };
        let repeated = if proto.repeated { "repeated " } else { "" };
        out.push_str(&format!(
            "  {optional}{repeated}{} {} = {};\n",
            proto.type_name,
            arg.name,
            i + 1,
        ));
    }

    // Add standard pagination fields for list queries
    if q.returns_list {
        let next_num = sorted_args.len() + 1;
        out.push_str(&format!("  optional int32 limit = {next_num};\n"));
        out.push_str(&format!("  optional int32 offset = {};\n", next_num + 1));
    }

    out.push_str("}\n\n");

    // Note: list queries use server-streaming RPCs and do not need a
    // response wrapper message — each streamed frame is the entity type directly.
}

/// Generate a request message for a mutation.
fn generate_mutation_request_message(
    out: &mut String,
    m: &fraiseql_core::schema::MutationDefinition,
) {
    let rpc_name = to_pascal_case(&m.name);

    out.push_str(&format!("message {rpc_name}Request {{\n"));

    let mut sorted_args: Vec<_> = m.arguments.iter().collect();
    sorted_args.sort_by(|a, b| a.name.cmp(&b.name));

    for (i, arg) in sorted_args.iter().enumerate() {
        let proto = field_type_to_proto(&arg.arg_type);
        let optional = if arg.nullable && !proto.repeated {
            "optional "
        } else {
            ""
        };
        let repeated = if proto.repeated { "repeated " } else { "" };
        out.push_str(&format!(
            "  {optional}{repeated}{} {} = {};\n",
            proto.type_name,
            arg.name,
            i + 1,
        ));
    }

    out.push_str("}\n\n");
}

/// Check if a type should be included based on include/exclude lists.
pub(crate) fn should_include_type(
    name: &str,
    include_types: &[String],
    exclude_types: &[String],
) -> bool {
    if !include_types.is_empty() && !include_types.iter().any(|t| t == name) {
        return false;
    }
    !exclude_types.iter().any(|t| t == name)
}

/// Convert a snake_case or camelCase name to PascalCase.
pub(crate) fn to_pascal_case(name: &str) -> String {
    name.split('_')
        .map(|part| {
            let mut chars = part.chars();
            match chars.next() {
                Some(c) => {
                    let mut s = c.to_uppercase().to_string();
                    s.push_str(&chars.collect::<String>());
                    s
                },
                None => String::new(),
            }
        })
        .collect()
}

/// Convert a PascalCase name to SCREAMING_SNAKE_CASE.
pub(crate) fn to_screaming_snake(name: &str) -> String {
    let mut result = String::new();
    for (i, c) in name.chars().enumerate() {
        if c.is_uppercase() && i > 0 {
            result.push('_');
        }
        result.push(c.to_ascii_uppercase());
    }
    result
}

/// Extract service name from package (e.g., "fraiseql.v1" → "FraiseQLService").
fn package_to_service(package: &str) -> String {
    let parts: Vec<&str> = package.split('.').collect();
    let base = parts.first().copied().unwrap_or("FraiseQL");
    let mut service = to_pascal_case(base);
    service.push_str("Service");
    service
}

/// Add the import path for a well-known protobuf type.
fn add_import_for_type(proto_type: &str, imports: &mut BTreeSet<String>) {
    match proto_type {
        "google.protobuf.Timestamp" => {
            imports.insert("google/protobuf/timestamp.proto".to_string());
        },
        "google.protobuf.Struct" => {
            imports.insert("google/protobuf/struct.proto".to_string());
        },
        _ => {},
    }
}