Class: Graphql::Generators::TypeGeneratorBase

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

Defined Under Namespace

Classes: NormalizedField

Class Method Summary collapse

Methods included from Core

#create_dir, #create_mutation_root_type, #insert_root_type, #module_namespacing_when_supported, #schema_file_path

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/graphql/type_generator.rb', line 26

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