Class: GraphitiGql::Schema::Fields::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti_gql/schema/fields/stats.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Stats

Returns a new instance of Stats.



5
6
7
# File 'lib/graphiti_gql/schema/fields/stats.rb', line 5

def initialize(resource)
  @resource = resource
end

Instance Method Details

#apply(type) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/graphiti_gql/schema/fields/stats.rb', line 9

def apply(type)
  type.field :stats, build_stat_class, null: false
  type.define_method :stats do
    Graphiti.broadcast('before_stats', {})
    # Process grouped (to-many relationship) stats
    Graphiti.broadcast('after_stats', {}) do
      stats = object.proxy.stats.deep_dup
      stats.each_pair do |attr, calc|
        calc.each_pair do |calc_name, value|
          if value.is_a?(Hash)
            stats[attr][calc_name] = value[parent.id]
          end
        end
      end
      stats
    end
  end
  type
end