module GraphQL::Groups::HasGroups::ClassMethods

Attributes

class_scope[R]

Public Class Methods

new(query_method:, **options, &definition_block) click to toggle source
Calls superclass method
# File lib/graphql/groups/has_groups.rb, line 79
def initialize(query_method:, **options, &definition_block)
  @query_method = query_method
  super(**options.except(:query_method), &definition_block)
end

Public Instance Methods

by(name, **options, &block) click to toggle source
# File lib/graphql/groups/has_groups.rb, line 20
def by(name, **options, &block)
  query_method = options[:query_method] || name
  resolver_method = "resolve_#{query_method}".to_sym
  group_field name, [own_result_type],
              query_method: query_method,
              null: true,
              resolver_method: resolver_method,

              **options, &block

  define_method query_method do |**kwargs|
    kwargs[:scope].group(name)
  end

  define_method resolver_method do |**_|
    group[name]
  end
end
group_field(*args, **kwargs, &block) click to toggle source
# File lib/graphql/groups/has_groups.rb, line 39
def group_field(*args, **kwargs, &block)
  field_defn = field_class.from_options(*args, owner: self, **kwargs, &block)
  add_field(field_defn)
  field_defn
end
result_type(&block) click to toggle source
# File lib/graphql/groups/has_groups.rb, line 45
def result_type(&block)
  @own_result_type = instance_eval(&block)
end
scope(&block) click to toggle source
# File lib/graphql/groups/has_groups.rb, line 49
def scope(&block)
  @class_scope = block
end

Private Instance Methods

find_result_type() click to toggle source
# File lib/graphql/groups/has_groups.rb, line 86
def find_result_type
  return @own_result_type if @own_result_type

  return GraphQL::Groups::Schema::GroupResultType unless name

  "#{name.gsub(/Type$/, '')}ResultType".safe_constantize || GraphQL::Groups::Schema::GroupResultType
end
group_by() click to toggle source
# File lib/graphql/groups/has_groups.rb, line 66
def group_by
  group_result[1][:group_by]
end
own_field_type() click to toggle source
# File lib/graphql/groups/has_groups.rb, line 72
def own_field_type
  type = "#{name}Field"
  base_field_type = field_class
  registry = GraphQL::Groups::GroupTypeRegistry.instance
  registry.get(type) || registry.register(type, Class.new(base_field_type) do
    attr_reader :query_method

    def initialize(query_method:, **options, &definition_block)
      @query_method = query_method
      super(**options.except(:query_method), &definition_block)
    end
  end)
end
own_result_type() click to toggle source
# File lib/graphql/groups/has_groups.rb, line 55
def own_result_type
  type = find_result_type
  own_group_type = self

  registry = GraphQL::Groups::GroupTypeRegistry.instance
  # To avoid name conflicts check if a result type has already been registered, and if not create a new one
  registry.get(type) || registry.register(type, Class.new(type) do
    graphql_name type.name.demodulize

    field :group_by, own_group_type, null: false, camelize: true

    def group_by
      group_result[1][:group_by]
    end
  end)
end