Class: Synthra::Export::Graphql
- Defined in:
- lib/synthra/export/graphql.rb
Overview
GraphQL schema export
Exports Synthra schemas to GraphQL SDL (Schema Definition Language).
Constant Summary collapse
- TYPE_MAP =
Type mapping from Synthra types to GraphQL scalars
{ "uuid" => "ID", "ulid" => "ID", "id_sequence" => "ID", "snowflake_id" => "ID", "numeric_string_id" => "ID", "email" => "String", "url" => "String", "text" => "String", "name" => "String", "first_name" => "String", "last_name" => "String", "full_name" => "String", "username" => "String", "phone" => "String", "address" => "String", "city" => "String", "state" => "String", "country" => "String", "country_code" => "String", "postal_code" => "String", "ip" => "String", "ipv6" => "String", "domain" => "String", "user_agent" => "String", "password" => "String", "color" => "String", "hex_color" => "String", "number" => "Int", "integer" => "Int", "age" => "Int", "airport_elevation" => "Int", "discount" => "Int", "formula" => "Int", "row_number" => "Int", "sequence" => "Int", "float" => "Float", "money" => "Float", "latitude" => "Float", "longitude" => "Float", "airport_latitude" => "Float", "airport_longitude" => "Float", "product_price" => "Float", "tax_rate" => "Float", "transaction_amount" => "Float", "boolean" => "Boolean", "date" => "String", "date_between" => "String", "past_date" => "String", "future_date" => "String", "mobile_device_release_date" => "String", "timestamp" => "String", "datetime" => "String", "now" => "String", "time" => "String" }.freeze
Class Method Summary collapse
- .build_query_type(registry) ⇒ Object
-
.export_all(registry) ⇒ String
Export all schemas to GraphQL.
Instance Method Summary collapse
- #build_enum(schema_name, field) ⇒ Object private
- #build_field(field) ⇒ Object private
-
#build_type(schema) ⇒ String
Build a GraphQL type from a schema.
-
#collect_enums(schema) ⇒ Array<String>
Collect enum definitions from a schema.
- #enum_type_name(field) ⇒ Object private
-
#export ⇒ String
Export single schema to GraphQL.
- #field_to_graphql_type(field) ⇒ Object private
Constructor Details
This class inherits a constructor from Synthra::Export::Base
Class Method Details
.build_query_type(registry) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/synthra/export/graphql.rb', line 250 def self.build_query_type(registry) lines = ["type Query {"] registry.schemas.each do |name, _schema| singular = name.downcase plural = "#{singular}s" lines << " #{singular}(id: ID!): #{name}" lines << " #{plural}(limit: Int = 10, offset: Int = 0): [#{name}!]!" end lines << "}" lines.join("\n") end |
.export_all(registry) ⇒ String
Export all schemas to GraphQL
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 |
# File 'lib/synthra/export/graphql.rb', line 98 def self.export_all(registry) lines = [] lines << "# Synthra Generated GraphQL Schema" lines << "# Generated at: #{Time.now.iso8601}" lines << "" enums = [] types = [] registry.schemas.each_value do |schema| exporter = new(schema, registry: registry) types << exporter.build_type(schema) enums.concat(exporter.collect_enums(schema)) end # Add enums first enums.uniq.each do |enum_def| lines << enum_def lines << "" end # Add types types.each do |type_def| lines << type_def lines << "" end # Add Query type lines << build_query_type(registry) lines.join("\n") end |
Instance Method Details
#build_enum(schema_name, field) ⇒ Object (private)
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/synthra/export/graphql.rb', line 231 def build_enum(schema_name, field) enum_name = "#{schema_name}#{field.name.split('_').map(&:capitalize).join}Enum" values = field.type_args[:values] return nil unless values&.any? lines = ["enum #{enum_name} {"] values.each do |val| value_str = val.respond_to?(:value) ? val.value : val.to_s # GraphQL enum values must be SCREAMING_SNAKE_CASE graphql_value = value_str.to_s.upcase.gsub(/[^A-Z0-9_]/, "_") lines << " #{graphql_value}" end lines << "}" lines.join("\n") end |
#build_field(field) ⇒ Object (private)
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/synthra/export/graphql.rb', line 176 def build_field(field) graphql_type = field_to_graphql_type(field) nullable = field.optional? || field.nullable? required_marker = nullable ? "" : "!" deprecated = "" if field.behaviors.any? { |b| b[:name] == :deprecated } deprecated = " @deprecated" end "#{field.name}: #{graphql_type}#{required_marker}#{deprecated}" end |
#build_type(schema) ⇒ String
Build a GraphQL type from a schema
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/synthra/export/graphql.rb', line 136 def build_type(schema) lines = [] # Add description if available if schema.deprecated? lines << "\"\"\"#{schema. || 'This type is deprecated'}\"\"\"" end lines << "type #{schema.name} {" schema.fields.each do |field| field_def = build_field(field) lines << " #{field_def}" end lines << "}" lines.join("\n") end |
#collect_enums(schema) ⇒ Array<String>
Collect enum definitions from a schema
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/synthra/export/graphql.rb', line 161 def collect_enums(schema) enums = [] schema.fields.each do |field| if field.type_name == "enum" enum_def = build_enum(schema.name, field) enums << enum_def if enum_def end end enums end |
#enum_type_name(field) ⇒ Object (private)
226 227 228 229 |
# File 'lib/synthra/export/graphql.rb', line 226 def enum_type_name(field) # Generate enum type name from field context "#{@schema.name}#{field.name.split('_').map(&:capitalize).join}Enum" end |
#export ⇒ String
Export single schema to GraphQL
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/synthra/export/graphql.rb', line 79 def export lines = [] # Add custom scalar definitions lines << "# Synthra Generated GraphQL Schema" lines << "# Generated at: #{Time.now.iso8601}" lines << "" # Build type lines << build_type(@schema) lines.join("\n") end |
#field_to_graphql_type(field) ⇒ Object (private)
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 |
# File 'lib/synthra/export/graphql.rb', line 189 def field_to_graphql_type(field) type_name = field.type_name # Handle enums if type_name == "enum" return enum_type_name(field) end # Handle const if type_name == "const" value = field.type_args[:value] return case value when String then "String" when Integer then "Int" when Float then "Float" when TrueClass, FalseClass then "Boolean" else "String" end end # Handle arrays. The element is either a scalar type (map it; unknown scalars → String, # matching the scalar fallback) or a schema reference (a PascalCase schema name — keep it). if type_name == "array" element_str = field.type_args[:element].to_s element_graphql = TYPE_MAP[element_str] || (element_str.match?(/\A[A-Z]/) ? element_str : "String") return "[#{element_graphql}!]" end # Handle schema references if @registry&.schema?(type_name) return type_name end # Handle known types TYPE_MAP[type_name] || "String" end |