Class: Graphql::Generators::TypeGeneratorBase

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

Class Method Summary collapse

Class Method Details

.normalize_type_expression(type_expression, mode:) ⇒ String

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)

Returns:

  • (String)


22
23
24
25
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
# File 'lib/generators/graphql/type_generator.rb', line 22

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