Module: GraphQL::Schema::Member::BuildType Private

Defined in:
lib/graphql/schema/member/build_type.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Constant Summary collapse

LIST_TYPE_ERROR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"Use an array of [T] or [T, null: true] for list types; other arrays are not supported"

Class Method Summary collapse

Class Method Details

.camelize(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

[View source]

127
128
129
130
131
132
133
134
135
136
137
# File 'lib/graphql/schema/member/build_type.rb', line 127

def camelize(string)
  return string if string == '_'
  return string unless string.include?("_")
  camelized = string.split('_').each(&:capitalize!).join
  camelized[0] = camelized[0].downcase
  if string.start_with?("_")
    match_data = string.match(/\A(_+)/)
    camelized = "#{match_data[0]}#{camelized}"
  end
  camelized
end

.constantize(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves constant from string (based on Rails ActiveSupport::Inflector.constantize)

[View source]

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/graphql/schema/member/build_type.rb', line 140

def constantize(string)
  names = string.split('::')

  # Trigger a built-in NameError exception including the ill-formed constant in the message.
  Object.const_get(string) if names.empty?

  # Remove the first blank element in case of '::ClassName' notation.
  names.shift if names.size > 1 && names.first.empty?

  names.inject(Object) do |constant, name|
    if constant == Object
      constant.const_get(name)
    else
      candidate = constant.const_get(name)
      next candidate if constant.const_defined?(name, false)
      next candidate unless Object.const_defined?(name)

      # Go down the ancestors to check if it is owned directly. The check
      # stops when we reach Object or the end of ancestors tree.
      constant = constant.ancestors.inject do |const, ancestor|
        break const    if ancestor == Object
        break ancestor if ancestor.const_defined?(name, false)
        const
      end

      # Owner is in Object, so raise.
      constant.const_get(name, false)
    end
  end
end

.parse_type(type_expr, null:) ⇒ GraphQL::BaseType

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • type_expr (String, Class, GraphQL::BaseType)

Returns:

  • (GraphQL::BaseType)
[View source]

12
13
14
15
16
17
18
19
20
21
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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/graphql/schema/member/build_type.rb', line 12

def parse_type(type_expr, null:)
  list_type = false

  return_type = case type_expr
  when String
    case type_expr
    when "String"
      GraphQL::Types::String
    when "Int", "Integer"
      GraphQL::Types::Int
    when "Float"
      GraphQL::Types::Float
    when "Boolean"
      GraphQL::Types::Boolean
    when "ID"
      GraphQL::Types::ID
    when /\A\[.*\]\Z/
      list_type = true
      # List members are required by default
      parse_type(type_expr[1..-2], null: false)
    when /.*!\Z/
      null = false
      parse_type(type_expr[0..-2], null: true)
    else
      maybe_type = constantize(type_expr)
      case maybe_type
      when Module
        # This is a way to check that it's the right kind of module:
        if maybe_type.respond_to?(:kind)
          maybe_type
        else
          raise ArgumentError, "Unexpected class/module found for GraphQL type: #{type_expr} (must be type definition class/module)"
        end
      end
    end
  when GraphQL::Schema::LateBoundType
    type_expr
  when Array
    case type_expr.length
    when 1
      list_type = true
      # List members are required by default
      parse_type(type_expr.first, null: false)
    when 2
      inner_type, nullable_option = type_expr
      if nullable_option.keys != [:null] || (nullable_option[:null] != true && nullable_option[:null] != false)
        raise ArgumentError, LIST_TYPE_ERROR
      end
      list_type = true
      parse_type(inner_type, null: nullable_option[:null])
    else
      raise ArgumentError, LIST_TYPE_ERROR
    end
  when GraphQL::Schema::NonNull, GraphQL::Schema::List
    type_expr
  when Module
    # This is a way to check that it's the right kind of module:
    if type_expr.respond_to?(:kind)
      type_expr
    else
      # Eg `String` => GraphQL::Types::String
      parse_type(type_expr.name, null: true)
    end
  when Proc
    parse_type(type_expr.call, null: true)
  when false
    raise ArgumentError, "Received `false` instead of a type, maybe a `!` should be replaced with `null: true` (for fields) or `required: true` (for arguments)"
  end

  if return_type.nil?
    raise "Unexpected type input: #{type_expr.inspect} (#{type_expr.class})"
  end

  # Apply list_type first, that way the
  # .to_non_null_type applies to the list type, not the inner type
  if list_type
    return_type = return_type.to_list_type
  end

  if !null
    return_type = return_type.to_non_null_type
  end


  return_type
end

.to_type_name(something) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

[View source]

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/graphql/schema/member/build_type.rb', line 99

def to_type_name(something)
  case something
  when GraphQL::Schema::LateBoundType
    something.unwrap.name
  when Array
    to_type_name(something.first)
  when Module
    if something.respond_to?(:graphql_name)
      something.graphql_name
    else
      to_type_name(something.name)
    end
  when String
    if something.include?("]") ||
        something.include?("[") ||
        something.include?("!") ||
        something.include?("::")
      something.gsub(/\]\[\!/, "").split("::").last
    else
      something
    end
  when GraphQL::Schema::NonNull, GraphQL::Schema::List
    to_type_name(something.unwrap)
  else
    raise "Unhandled to_type_name input: #{something} (#{something.class})"
  end
end

.underscore(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

[View source]

171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/graphql/schema/member/build_type.rb', line 171

def underscore(string)
  if string.match?(/\A[a-z_]+\Z/)
    return string
  end
  string2 = string.dup

  string2.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') # URLDecoder -> URL_Decoder
  string2.gsub!(/([a-z\d])([A-Z])/,'\1_\2')     # someThing -> some_Thing
  string2.downcase!

  string2
end