Sha256: bfab0ae8d67e92fe7491ea5c0e95effd10e4bcaf05e98bd91c5aeca67ae07e3c
Contents?: true
Size: 1.67 KB
Versions: 25
Compression:
Stored size: 1.67 KB
Contents
module GraphitiGql class Schema module Fields class Stats def initialize(resource) @resource = resource end def apply(type) type.field :stats, build_stat_class, null: false type.define_method :stats do # Process grouped (to-many relationship) stats 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 type end private def build_stat_class name = Registry.instance.key_for(@resource) stat_graphql_name = "#{name}Stats" return Registry.instance[stat_graphql_name][:type] if Registry.instance[stat_graphql_name] klass = Class.new(Schema.base_object) klass.graphql_name(stat_graphql_name) @resource.stats.each_pair do |name, config| calc_class = build_calc_class(stat_graphql_name, name, config.calculations.keys) klass.field name, calc_class, null: false end Registry.instance[stat_graphql_name] = { type: klass } klass end def build_calc_class(stat_graphql_name, stat_name, calculations) name = "#{stat_graphql_name}#{stat_name}Calculations" klass = Class.new(Schema.base_object) klass.graphql_name(name) calculations.each do |calc| klass.field calc, Float, null: false end klass end end end end end
Version data entries
25 entries across 25 versions & 1 rubygems