Sha256: 97daa1bfe2a5e662f000dc9a709acdece37996b24dcd1ca49a3c0cd3770c073f

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

module GraphitiGraphQL
  module Federation
    module Loaders
      class HasMany < GraphQL::Batch::Loader
        def initialize(federated_relationship, params)
          @federated_relationship = federated_relationship
          @resource_class = federated_relationship.local_resource_class
          @params = params
          @foreign_key = federated_relationship.foreign_key
        end

        def perform(ids)
          @params[:filter] ||= {}
          @params[:filter][@foreign_key] = {eq: ids.join(",")}

          @federated_relationship.params_block&.call(@params)

          if ids.length > 1 && @params[:page]
            raise Graphiti::Errors::UnsupportedPagination
          elsif !@params[:page]
            @params[:page] = {size: 999}
          end

          Util.with_gql_context do
            records = @resource_class.all(@params).as_json[:data]
            map = records.group_by { |record| record[@foreign_key].to_s}
            ids.each { |id| fulfill(id, (map[id] || [])) }
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
graphiti_graphql-0.1.6 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.5 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.4 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.3 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.2 lib/graphiti_graphql/federation/loaders/has_many.rb