|
| 1 | +"""Generator: SchemaForge IR → GraphQL SDL. |
| 2 | +
|
| 3 | +Converts tables/columns into GraphQL Schema Definition Language |
| 4 | +type definitions with enums, directives, and nullable/required annotations. |
| 5 | +""" |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +from ..ir import Schema, Table, Column, ColumnType |
| 11 | +from ..type_config import EMPTY_CONFIG, TypeConfig |
| 12 | + |
| 13 | +# ColumnType → GraphQL type mapping |
| 14 | +_TYPE_TO_GRAPHQL: dict[ColumnType, str] = { |
| 15 | + ColumnType.STRING: "String", |
| 16 | + ColumnType.INTEGER: "Int", |
| 17 | + ColumnType.FLOAT: "Float", |
| 18 | + ColumnType.BOOLEAN: "Boolean", |
| 19 | + ColumnType.DATETIME: "DateTime", |
| 20 | + ColumnType.DATE: "Date", |
| 21 | + ColumnType.TIME: "Time", |
| 22 | + ColumnType.TEXT: "String", |
| 23 | + ColumnType.BLOB: "Byte", |
| 24 | + ColumnType.JSON: "JSON", |
| 25 | + ColumnType.UUID: "UUID", |
| 26 | + ColumnType.DECIMAL: "Decimal", |
| 27 | + ColumnType.ENUM: "String", # Will be overridden in field generation |
| 28 | +} |
| 29 | + |
| 30 | +# CUSTOM types that should use a specific GraphQL scalar annotation |
| 31 | +_CUSTOM_TO_SCALAR: dict[str, str] = { |
| 32 | + "JSON": "JSON", |
| 33 | + "Json": "JSON", |
| 34 | + "UUID": "UUID", |
| 35 | + "UID": "UUID", |
| 36 | + "Email": "String", |
| 37 | + "PhoneNumber": "String", |
| 38 | + "DateTime": "DateTime", |
| 39 | + "Date": "Date", |
| 40 | + "Time": "Time", |
| 41 | + "Decimal": "Decimal", |
| 42 | + "BigInt": "Int", |
| 43 | + "URI": "URI", |
| 44 | + "Upload": "Upload", |
| 45 | + "Void": "Void", |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +class GraphQLGenerator: |
| 50 | + """Convert Schema IR to GraphQL SDL.""" |
| 51 | + |
| 52 | + def __init__(self, type_config: TypeConfig | None = None) -> None: |
| 53 | + """Initialize with optional custom type overrides. |
| 54 | +
|
| 55 | + Args: |
| 56 | + type_config: Optional custom type mapping overrides. |
| 57 | + """ |
| 58 | + self._type_config = type_config or EMPTY_CONFIG |
| 59 | + |
| 60 | + def generate(self, schema: Schema) -> str: |
| 61 | + """Generate a GraphQL SDL document from Schema IR. |
| 62 | +
|
| 63 | + Args: |
| 64 | + schema: Schema IR with tables and enums. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + Formatted GraphQL SDL string. |
| 68 | + """ |
| 69 | + parts: list[str] = [] |
| 70 | + |
| 71 | + # Generate custom scalar declarations for types not in default set |
| 72 | + custom_scalars = self._collect_custom_scalars(schema) |
| 73 | + for scalar in sorted(custom_scalars): |
| 74 | + parts.append(f"scalar {scalar}") |
| 75 | + |
| 76 | + # Generate enums |
| 77 | + for enum_type in schema.enums: |
| 78 | + parts.append(self._enum_to_sdl(enum_type)) |
| 79 | + |
| 80 | + # Generate types from tables |
| 81 | + for table in schema.tables: |
| 82 | + is_input = table.options.get("is_input") == "true" |
| 83 | + parts.append(self._table_to_sdl(table, is_input=is_input)) |
| 84 | + |
| 85 | + return "\n\n".join(parts) + "\n" |
| 86 | + |
| 87 | + def _collect_custom_scalars(self, schema: Schema) -> set[str]: |
| 88 | + """Collect GraphQL scalar types that need explicit declaration. |
| 89 | +
|
| 90 | + Returns: |
| 91 | + Set of scalar type names used in columns but not built-in. |
| 92 | + """ |
| 93 | + built_in = {"String", "Int", "Float", "Boolean", "ID"} |
| 94 | + scalars: set[str] = set() |
| 95 | + |
| 96 | + for table in schema.tables: |
| 97 | + for col in table.columns: |
| 98 | + if col.custom_type and col.custom_type not in built_in: |
| 99 | + gql = _CUSTOM_TO_SCALAR.get(col.custom_type) |
| 100 | + if gql and gql not in built_in: |
| 101 | + scalars.add(gql) |
| 102 | + |
| 103 | + return scalars |
| 104 | + |
| 105 | + def _enum_to_sdl(self, enum_type) -> str: |
| 106 | + """Convert an EnumType IR to a GraphQL enum definition. |
| 107 | +
|
| 108 | + Args: |
| 109 | + enum_type: The enum type to convert. |
| 110 | +
|
| 111 | + Returns: |
| 112 | + GraphQL enum definition string. |
| 113 | + """ |
| 114 | + values = "\n ".join(enum_type.values) |
| 115 | + return f"enum {enum_type.name} {{\n {values}\n}}" |
| 116 | + |
| 117 | + def _table_to_sdl(self, table: Table, is_input: bool = False) -> str: |
| 118 | + """Convert a Table IR to a GraphQL type definition. |
| 119 | +
|
| 120 | + Args: |
| 121 | + table: The table to convert. |
| 122 | + is_input: Whether to generate an 'input' vs 'type'. |
| 123 | +
|
| 124 | + Returns: |
| 125 | + GraphQL type definition string. |
| 126 | + """ |
| 127 | + keyword = "input" if is_input else "type" |
| 128 | + lines = [f"{keyword} {table.name} {{"] |
| 129 | + |
| 130 | + for col in table.columns: |
| 131 | + lines.append(f" {self._field_to_sdl(col)}") |
| 132 | + |
| 133 | + lines.append("}") |
| 134 | + return "\n".join(lines) |
| 135 | + |
| 136 | + def _field_to_sdl(self, col: Column) -> str: |
| 137 | + """Convert a Column IR to a GraphQL field definition. |
| 138 | +
|
| 139 | + Generates: fieldName: Type! @directive |
| 140 | +
|
| 141 | + Args: |
| 142 | + col: The column to convert. |
| 143 | +
|
| 144 | + Returns: |
| 145 | + GraphQL field definition string. |
| 146 | + """ |
| 147 | + gql_type = self._resolve_graphql_type(col) |
| 148 | + field_def = f"{col.name}: {gql_type}" |
| 149 | + |
| 150 | + # Collect directives |
| 151 | + directives: list[str] = [] |
| 152 | + |
| 153 | + if col.unique: |
| 154 | + directives.append("@unique") |
| 155 | + |
| 156 | + if col.primary_key and col.name == "id": |
| 157 | + directives.append("@id") |
| 158 | + |
| 159 | + # Comments |
| 160 | + if col.comment: |
| 161 | + directives.append(f'# {col.comment.replace(chr(10), " ")}') |
| 162 | + |
| 163 | + if directives: |
| 164 | + field_def += f" {' '.join(directives)}" |
| 165 | + |
| 166 | + return field_def |
| 167 | + |
| 168 | + def _resolve_graphql_type(self, col: Column) -> str: |
| 169 | + """Resolve a column's type to a GraphQL type string with ! annotation. |
| 170 | +
|
| 171 | + Args: |
| 172 | + col: The column to resolve. |
| 173 | +
|
| 174 | + Returns: |
| 175 | + GraphQL type string (e.g. 'String!', '[Item]', 'Int!'). |
| 176 | + """ |
| 177 | + # Check type_config override first |
| 178 | + if self._type_config: |
| 179 | + overridden = self._type_config.get_override(col, "graphql") |
| 180 | + if overridden: |
| 181 | + nullable_suffix = "!" if not col.nullable else "" |
| 182 | + return f"{overridden}{nullable_suffix}" |
| 183 | + |
| 184 | + base_type = self._base_graphql_type(col) |
| 185 | + |
| 186 | + # Nullable annotation: ! means required/non-null |
| 187 | + if not col.nullable: |
| 188 | + return f"{base_type}!" |
| 189 | + |
| 190 | + return base_type |
| 191 | + |
| 192 | + def _base_graphql_type(self, col: Column) -> str: |
| 193 | + """Resolve the base GraphQL type name for a column. |
| 194 | +
|
| 195 | + Args: |
| 196 | + col: The column to resolve. |
| 197 | +
|
| 198 | + Returns: |
| 199 | + Base GraphQL type name. |
| 200 | + """ |
| 201 | + # STRING type with primary_key and name "id" → ID |
| 202 | + if col.type == ColumnType.STRING and col.primary_key and col.name == "id": |
| 203 | + return "ID" |
| 204 | + |
| 205 | + # Handle CUSTOM types |
| 206 | + if col.type == ColumnType.CUSTOM and col.custom_type: |
| 207 | + # Check if we have a known scalar mapping |
| 208 | + mapped = _CUSTOM_TO_SCALAR.get(col.custom_type) |
| 209 | + if mapped: |
| 210 | + return mapped |
| 211 | + return col.custom_type |
| 212 | + |
| 213 | + # Check type_config override |
| 214 | + if self._type_config: |
| 215 | + overridden = self._type_config.get_override(col, "graphql") |
| 216 | + if overridden: |
| 217 | + return overridden |
| 218 | + |
| 219 | + # Check if there's an enum with this name (handled at schema level) |
| 220 | + # Default type mapping |
| 221 | + return _TYPE_TO_GRAPHQL.get(col.type, "String") |
0 commit comments