Sha256: 3bc98d51efb7883767e1385279390ca8424728bf62a74f772de36521497f590d

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module SolidusGraphqlApi
  class BatchLoader
    # A batch loader for +has_many :through+ associations.
    class HasManyThrough < BatchLoader
      def load
        graphql_loader_for(object.id, default_value: []) do |object_ids, loader|
          records_by_parent = group_records_by_parent(retrieve_records_for(object_ids))

          records_by_parent.each_pair do |parent_id, children|
            loader.call(parent_id) do |memo|
              memo.concat(children)
            end
          end
        end
      end

      private

      def retrieve_records_for(object_ids)
        through_reflection = reflection.through_reflection

        base_relation
          .joins(through_reflection.name)
          .where("#{through_reflection.table_name}.#{through_reflection.foreign_key}" => object_ids)
          .distinct
      end

      def group_records_by_parent(records)
        result = Hash.new { |h, k| h[k] = [] }

        records.each do |record|
          record.send(reflection.through_reflection.name).each do |parent|
            result[parent.send(reflection.through_reflection.foreign_key)] << record
          end
        end

        result
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_graphql_api-0.3.1 lib/solidus_graphql_api/batch_loader/has_many_through.rb
solidus_graphql_api-0.3.0 lib/solidus_graphql_api/batch_loader/has_many_through.rb
solidus_graphql_api-0.2.0 lib/solidus_graphql_api/batch_loader/has_many_through.rb