Module: Graphiform::Helpers

Defined in:
lib/graphiform/helpers.rb

Class Method Summary collapse

Class Method Details

.association_arguments_valid?(association_def, method) ⇒ Boolean

Returns:



67
68
69
70
71
72
# File 'lib/graphiform/helpers.rb', line 67

def self.association_arguments_valid?(association_def, method)
  association_def.present? &&
    association_def.klass.respond_to?(method) &&
    association_def.klass.send(method).respond_to?(:arguments) &&
    !association_def.klass.send(method).arguments.empty?
end

.full_const_name(name) ⇒ Object



60
61
62
63
64
65
# File 'lib/graphiform/helpers.rb', line 60

def self.full_const_name(name)
  name = "Object#{name}" if name.starts_with?('::')
  name = "Object::#{name}" unless name.starts_with?('Object::')

  name
end

.get_const_or_create(const, mod = Object) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/graphiform/helpers.rb', line 45

def self.get_const_or_create(const, mod = Object)
  new_full_const_name = full_const_name("#{mod}::#{const}")
  new_full_const_name.constantize
  Object.const_get(new_full_const_name)
rescue NameError => e
  unless full_const_name(e.missing_name) == new_full_const_name.to_s
    logger.warn "Failed to load #{e.missing_name} when loading constant #{new_full_const_name}"
    return Object.const_get(new_full_const_name)
  end

  val = yield
  mod.const_set(const, val)
  val
end

.graphql_type(active_record_type) ⇒ Object



12
13
14
15
16
17
# File 'lib/graphiform/helpers.rb', line 12

def self.graphql_type(active_record_type)
  is_array = active_record_type.is_a? Array
  active_record_type = is_array ? active_record_type[0] : active_record_type
  graphql_type = graphql_type_single(active_record_type)
  is_array ? [graphql_type] : graphql_type
end

.graphql_type_single(active_record_type) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graphiform/helpers.rb', line 19

def self.graphql_type_single(active_record_type)
  return active_record_type unless active_record_type.respond_to?(:to_sym)

  case active_record_type.to_sym
  when :string, :text
    GraphQL::Types::String
  when :date
    GraphQL::Types::ISO8601Date
  when :time, :datetime, :timestamp
    GraphQL::Types::ISO8601DateTime
  when :integer
    GraphQL::Types::Int
  when :float, :decimal
    GraphQL::Types::Float
  when :boolean
    GraphQL::Types::Boolean
  else
    active_record_type
  end
end

.loggerObject



5
6
7
8
9
10
# File 'lib/graphiform/helpers.rb', line 5

def self.logger
  return Rails.logger if Rails.logger.present?

  @logger ||= Logger.new(STDOUT)
  @logger
end

.resolver?(val) ⇒ Boolean

Returns:



40
41
42
43
# File 'lib/graphiform/helpers.rb', line 40

def self.resolver?(val)
  val.respond_to?(:ancestors) &&
    val.ancestors.include?(GraphQL::Schema::Resolver)
end