Sha256: 694215070c42dbbcc04254eaf5b328a5f860454db3c1c308335e2b47507de692

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module GraphQL
  module Groups
    module HasAggregates
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        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

        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

        private

        def aggregate_type(name)
          name = "#{name}AggregateType".upcase_first
          own_aggregate_types[name] ||= Class.new(Schema::AggregateType) do
            graphql_name name
          end
        end

        def own_aggregate_types
          @own_aggregate_types ||= {}
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-groups-0.2.3 lib/graphql/groups/has_aggregates.rb