Module: GraphQL::Groups::HasAggregates::ClassMethods

Defined in:
lib/graphql/groups/has_aggregates.rb

Instance Method Summary collapse

Instance Method Details

#aggregate(name, *_, **options, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/graphql/groups/has_aggregates.rb', line 11

def aggregate(name, *_, **options, &block)
  aggregate_type = aggregate_type(name)

  resolve_method = "resolve_#{name}".to_sym
  query_method = options[:query_method] || name
  field = aggregate_field name, aggregate_type,
                          null: false,
                          query_method:,
                          resolver_method: resolve_method,
                          **options, &block
  aggregate_type.add_fields(field.own_attributes)

  define_method query_method do |scope:, **kwargs|
    scope.public_send(name, **kwargs)
  end

  define_method resolve_method do
    group_result[1][name]
  end
end

#aggregate_field(*args, **kwargs, &block) ⇒ Object



32
33
34
35
36
37
# File 'lib/graphql/groups/has_aggregates.rb', line 32

def aggregate_field(*args, **kwargs, &block)
  field_defn = Schema::AggregateField.from_options(*args, owner: self, **kwargs, &block)
  field_defn.ensure_loaded if Gem::Version.new(GraphQL::VERSION) >= Gem::Version.new('2.3')
  add_field(field_defn)
  field_defn
end