module AutoGraphQL

Constants

VERSION

Public Instance Methods

register(model, options = {}) click to toggle source
# File lib/autographql/autographql.rb, line 12
def register model, options = {}
  unless @@query_type.nil?
    raise RuntimeError, 'registration not allowed after generation.  please register model sooner'
  end

  # 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

Protected Instance Methods

const_missing(const) click to toggle source
Calls superclass method
# File lib/autographql/autographql.rb, line 58
def const_missing const
  case const
  when :Types
    gen_types
  when :QueryType
    gen_query
  else
    super
  end
end

Private Instance Methods

gen_query() click to toggle source
# File lib/autographql/autographql.rb, line 76
def gen_query
  @@query_type ||= AutoGraphQL::QueryBuilder.build gen_types
end
gen_types() click to toggle source
# File lib/autographql/autographql.rb, line 72
def gen_types
  @@type_map ||= AutoGraphQL::TypeBuilder.build @@models
end