Module: AutoGraphQL

Extended by:
AutoGraphQL
Included in:
AutoGraphQL
Defined in:
lib/autographql.rb,
lib/autographql/autographql.rb,
lib/autographql/object_type.rb,
lib/autographql/type_builder.rb,
lib/autographql/query_builder.rb

Defined Under Namespace

Modules: ObjectType, QueryBuilder, TypeBuilder

Constant Summary collapse

VERSION =
'0.0.3'
@@models =
{}

Instance Method Summary collapse

Instance Method Details

#const_missing(const) ⇒ Object

dynamically generate ::QueryType



51
52
53
54
55
56
57
58
# File 'lib/autographql/autographql.rb', line 51

def const_missing const
  case const
  when :QueryType
    AutoGraphQL::QueryBuilder.build @@models
  else
    super
  end
end

#register(model, options = {}) ⇒ Object



10
11
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
# File 'lib/autographql/autographql.rb', line 10

def register model, options = {}
  # sanitize options

  name = options.fetch(:name, model.name)
  name.gsub! /:/, '_'

  exclude = options.fetch(:exclude, []).map(&:to_sym)

  # add `id' column by default
  fields = [ :id ]

  # either use user specified fields or default to all
  fields += options.fetch(:fields) do
    res = model.columns_hash.keys

    # add relationships
    res += [ :has_one, :has_many, :belongs_to ].map do |type|
      model.reflect_on_all_associations(type).map &:name
    end.flatten
  end.map(&:to_sym) - exclude

  model_methods = options.fetch(:methods, {})
  # ensure methods actually exist
  model_methods.each do |name, type|
    unless model.method_defined? name
      raise NoMethodError.new(
        "undefined method `#{name}' for #{model}"
      )
    end
  end

  @@models[model] = {
    name: name,
    description: options.fetch(:description, ''),
    fields: fields,
    methods: model_methods,
  }
end