Class: Graphql::Generators::TypeGeneratorBase

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Core
Defined in:
lib/generators/graphql/type_generator.rb

Defined Under Namespace

Classes: NormalizedField

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Core

#create_dir, included, #insert_root_type, #module_namespacing_when_supported, #schema_file_path

Instance Attribute Details

#graphql_type ⇒ Object

Returns the value of attribute graphql_type.



30
31
32
# File 'lib/generators/graphql/type_generator.rb', line 30

def graphql_type
  @graphql_type
end

Class Method Details

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

Take a type expression in any combination of GraphQL or Ruby styles and return it in a specified output style TODO: nullability / list with mode: :graphql doesn't work

Parameters

  • type_expresson (String)
  • mode (Symbol)
  • null (Boolean)

Returns

  • (String, Boolean) — The type expression, followed by null: value

:call-seq:

normalize_type_expression(type_expression, Symbol mode:, bool null:) -> (String, bool)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/generators/graphql/type_generator.rb', line 52

def self.normalize_type_expression(type_expression, mode:, null: true)
  if type_expression.start_with?("!")
    normalize_type_expression(type_expression[1..-1], mode: mode, null: false)
  elsif type_expression.end_with?("!")
    normalize_type_expression(type_expression[0..-2], mode: mode, null: false)
  elsif type_expression.start_with?("[") && type_expression.end_with?("]")
    name, is_null = normalize_type_expression(type_expression[1..-2], mode: mode, null: null)
    ["[#{name}]", is_null]
  elsif type_expression.end_with?("Type")
    normalize_type_expression(type_expression[0..-5], mode: mode, null: null)
  elsif type_expression.start_with?("Types::")
    normalize_type_expression(type_expression[7..-1], mode: mode, null: null)
  elsif type_expression.start_with?("types.")
    normalize_type_expression(type_expression[6..-1], mode: mode, null: null)
  else
    case mode
    when :ruby
      case type_expression
      when "Int"
        ["Integer", null]
      when "Integer", "Float", "Boolean", "String", "ID"
        [type_expression, null]
      else
        ["Types::#{type_expression.camelize}Type", null]
      end
    when :graphql
      [type_expression.camelize, null]
    else
      raise "Unexpected normalize mode: #{mode}"
    end
  end
end

Instance Method Details

#create_type_file ⇒ Object



32
33
34
# File 'lib/generators/graphql/type_generator.rb', line 32

def create_type_file
  template "#{graphql_type}.erb", "#{options[:directory]}/types#{subdirectory}/#{type_file_name}.rb"
end