Class: GraphSQL::Generators::TypeGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/graphsql/type/type_generator.rb

Overview

rails generate graphsql:type Persona

Introspects an already-migrated ActiveRecord model's columns and associations and emits a matching GraphSQL::Mapping type — every column gets a graphsql_column + field pair, every association gets a graphsql_association + field pair (plus a lookahead-driven resolver method for collection associations), and to: is guessed for every non-polymorphic association as "<type_namespace>::Type". Also wires a matching collection field + resolver into the app's query type via Thor's inject_into_class, unless --skip-query.

This only works against a model whose table already exists — it reads real column/association metadata via ActiveRecord, it doesn't infer anything from a migration file. Generate the model (and run its migration) first, then generate its type.

Defined Under Namespace

Classes: MappedAssociation, MappedColumn

Constant Summary collapse

SCALAR_TYPES =
{
  "string" => "String", "text" => "String", "citext" => "String",
  "integer" => "Integer", "bigint" => "Integer",
  "float" => "Float", "decimal" => "Float",
  "boolean" => "Boolean",
  "date" => "GraphQL::Types::ISO8601Date",
  "datetime" => "GraphQL::Types::ISO8601DateTime", "timestamp" => "GraphQL::Types::ISO8601DateTime",
  "json" => "GraphQL::Types::JSON", "jsonb" => "GraphQL::Types::JSON"
}.freeze

Instance Method Summary collapse

Instance Method Details

#add_query_fieldObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/generators/graphsql/type/type_generator.rb', line 54

def add_query_field
  return if options[:skip_query]

  unless File.exist?(File.join(destination_root, query_type_path))
    say_status :skip,
      "#{query_type_path} not found (pass --query-type-path or add the field by hand)", :yellow
    return
  end

  inject_into_class query_type_path, options[:query_type_class_name], query_field_snippet
end

#create_type_fileObject



50
51
52
# File 'lib/generators/graphsql/type/type_generator.rb', line 50

def create_type_file
  template "type.rb.tt", File.join("app/graphql/types", "#{file_name}_type.rb")
end