Class: ZeroRuby::TypeScriptGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/zero_ruby/typescript_generator.rb

Overview

Generates TypeScript type definitions from registered mutations. Similar to graphql-codegen, this introspects mutation argument definitions and generates corresponding TypeScript interfaces.

Examples:

TypeScriptGenerator.new(ZeroSchema).generate
# => "// Auto-generated by zero-ruby - do not edit\n\nexport interface ..."

Constant Summary collapse

PRIMITIVE_MAP =

Map Ruby primitives to TypeScript types

{
  String => "string",
  Integer => "number",
  Float => "number",
  TrueClass => "boolean",
  FalseClass => "boolean",
  Date => "string",
  DateTime => "string",
  Time => "string"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ TypeScriptGenerator

Returns a new instance of TypeScriptGenerator.



24
25
26
27
# File 'lib/zero_ruby/typescript_generator.rb', line 24

def initialize(schema)
  @schema = schema
  @input_objects = {}
end

Instance Method Details

#generateString

Generate TypeScript definitions

Returns:

  • (String)

    Complete TypeScript type definitions



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/zero_ruby/typescript_generator.rb', line 31

def generate
  collect_input_objects

  parts = [
    generate_header,
    generate_input_objects,
    generate_mutation_args,
    generate_mutation_map
  ]

  parts.compact.join("\n")
end