class Kanji::Graph::CoerceType

Constants

TYPE_MAP

Public Class Methods

call(type) click to toggle source
# File lib/kanji/graph/coerce_type.rb, line 6
def self.call(type)
  if array_type?(type)
    member_type = get_member_type(type)
    graphql_type = get_graphql_type(member_type)
    graphql_type.to_list_type
  else
    get_graphql_type(type)
  end
end

Private Class Methods

array_type?(type) click to toggle source
# File lib/kanji/graph/coerce_type.rb, line 53
def self.array_type?(type)
  return type.primitive == Array if defined?(type.primitive)
  defined?(type.type) && type.type.primitive == Array
end
get_graphql_type(type) click to toggle source
# File lib/kanji/graph/coerce_type.rb, line 31
def self.get_graphql_type(type)
  return type if type.is_a?(GraphQL::ObjectType)

  type_string = TYPE_MAP[type.name]

  if type.optional?
    GraphQL::Define::TypeDefiner.instance.send(type_string)
  else
    !GraphQL::Define::TypeDefiner.instance.send(type_string)
  end
end
get_member_type(type) click to toggle source
# File lib/kanji/graph/coerce_type.rb, line 43
def self.get_member_type(type)
  member_type = type.options[:member] || type.type.options[:member]

  if has_ancestor?(member_type, Kanji::Type)
    member_type[:graphql_type]
  else
    member_type
  end
end
has_ancestor?(type, ancestor) click to toggle source
# File lib/kanji/graph/coerce_type.rb, line 58
def self.has_ancestor?(type, ancestor)
  return false unless type.respond_to?(:ancestors)
  type.ancestors.include?(ancestor)
end