Module: GraphQL::Api::Helpers

Defined in:
lib/graphql/api/helpers.rb

Instance Method Summary collapse

Instance Method Details

#all_constants(root) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/graphql/api/helpers.rb', line 5

def all_constants(root)
  begin
    Dir["#{Rails.root}/app/#{root}/*"].map do |f|
      file = f.split('/')[-1]
      if file.end_with?('.rb')
        const = file.split('.')[0].camelize.constantize
        const unless const.try(:abstract_class)
      end
    end.compact
  rescue
    []
  end
end

#graphql_fetch(obj, ctx, name) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/graphql/api/helpers.rb', line 80

def graphql_fetch(obj, ctx, name)
  if obj.respond_to?("access_#{name}?")
    obj.send(name) if obj.send("access_#{name}?", ctx)
  else
    obj.send(name)
  end
end

#graphql_type(column) ⇒ Object



76
77
78
# File 'lib/graphql/api/helpers.rb', line 76

def graphql_type(column)
  graphql_type_of(column.type)
end

#graphql_type_for_object(return_type, object_types) ⇒ Object



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

def graphql_type_for_object(return_type, object_types)
  if return_type.nil?
    raise SchemaError.new("return type is nil for object")
  end

  if return_type.respond_to?(:to_sym) || (return_type.is_a?(Array) && return_type[0].respond_to?(:to_sym))
    type = graphql_type_of(return_type.to_sym)
  elsif return_type.is_a?(Array)
    type = object_types[return_type[0]].to_list_type
  else
    type = object_types[return_type]
  end

  if type.nil?
    raise SchemaError.new("could not parse return type for: #{return_type}")
  end

  type
end

#graphql_type_of(type) ⇒ Object



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
# File 'lib/graphql/api/helpers.rb', line 39

def graphql_type_of(type)

  is_required = false
  if type.to_s.end_with?('!')
    is_required = true
    type = type.to_s.chomp('!').to_sym
  end

  is_list = false
  if type.is_a?(Array)
    is_list = true
    type = type[0]
  end

  case type
    when :integer
      res = GraphQL::INT_TYPE
    when :text
      res = GraphQL::STRING_TYPE
    when :string
      res = GraphQL::STRING_TYPE
    when :decimal
      res = GraphQL::FLOAT_TYPE
    when :float
      res = GraphQL::FLOAT_TYPE
    when :boolean
      res = GraphQL::BOOLEAN_TYPE
    else
      res = GraphQL::STRING_TYPE
  end

  res = res.to_list_type if is_list
  res = !res if is_required

  res
end