Module: Graphiform::Helpers

Defined in:
lib/graphiform/helpers.rb

Class Method Summary collapse

Class Method Details

.association_arguments_valid?(association_def, method) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/graphiform/helpers.rb', line 56

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

.dataloader_support?(dataloader, association_def, keys) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/graphiform/helpers.rb', line 63

def self.dataloader_support?(dataloader, association_def, keys)
  (
    association_def.present? &&
    !association_def.polymorphic? &&
    !association_def.through_reflection? &&
    !association_def.inverse_of&.polymorphic? &&
    (
      !association_def.scope ||
      association_def.scope.arity.zero?
    ) &&
    !keys.is_a?(Array) &&
    !dataloader.is_a?(GraphQL::Dataloader::NullDataloader)
  )
end

.equal_graphql_names?(key, name) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/graphiform/helpers.rb', line 45

def self.equal_graphql_names?(key, name)
  key.downcase == name.to_s.camelize.downcase || key.downcase == name.to_s.downcase
end

.full_const_name(name) ⇒ Object



49
50
51
52
53
54
# File 'lib/graphiform/helpers.rb', line 49

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/graphiform/helpers.rb', line 30

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
# 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)

  Graphiform.configuration[:scalar_mappings][active_record_type.to_sym] || active_record_type
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:

  • (Boolean)


25
26
27
28
# File 'lib/graphiform/helpers.rb', line 25

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