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, #insert_root_type, #module_namespacing_when_supported, #schema_file_path

Instance Attribute Details

#graphql_typeObject

Returns the value of attribute graphql_type.


28
29
30
# File 'lib/generators/graphql/type_generator.rb', line 28

def graphql_type
  @graphql_type
end

Class Method Details

.normalize_type_expression(type_expression, mode:, null: true) ⇒ (String, Boolean)

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) (defaults to: true)

Returns:

  • ((String, Boolean))

    The type expression, followed by null: value


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/generators/graphql/type_generator.rb', line 41

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_fileObject


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

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