Class: Ibrain::Graphql::ObjectGenerator

Inherits:
TypeGeneratorBase show all
Defined in:
lib/generators/ibrain/graphql/object_generator.rb

Overview

Generate an object type by name, with the specified fields.

“‘ rails g graphql:object PostType name:String! “`

Add the Node interface with ‘–node`.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core

#create_dir, #create_mutation_root_type, #create_repository_root_type, #create_resolver_root_type, included, #insert_root_type, #module_namespacing_when_supported, #schema_file_path

Class Method Details

.normalize_type_expression(type_expression, mode:, null: true) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/generators/ibrain/graphql/object_generator.rb', line 45

def self.normalize_type_expression(type_expression, mode:, null: true)
  case type_expression
  when "Text", "Json", "json"
    ["String", null]
  when "Decimal"
    ["Float", null]
  when "DateTime", "Datetime"
    ["GraphQL::Types::ISO8601DateTime", null]
  when "Date"
    ["GraphQL::Types::ISO8601Date", null]
  else
    super
  end
end

Instance Method Details

#create_type_fileObject



31
32
33
34
35
36
37
# File 'lib/generators/ibrain/graphql/object_generator.rb', line 31

def create_type_file
  create_dir('app/repositories') unless Dir.exist?('app/repositories')

  template "object.erb", "#{options[:directory]}/types/objects/#{type_file_name}.rb"
  template "input.erb", "#{options[:directory]}/types/attributes/#{input_file_name}.rb"
  template "repository.erb", "app/repositories/#{type_name}_repository.rb"
end

#fieldsObject



39
40
41
42
43
# File 'lib/generators/ibrain/graphql/object_generator.rb', line 39

def fields
  columns = []
  columns += klass.columns.map { |c| generate_column_string(c) } if class_exists?
  columns + custom_fields
end