Sha256: 26fa122c85a3363161fc652674c25a76ef1fffdc7a0019e6a32249637aba7c69

Contents?: true

Size: 1.44 KB

Versions: 10

Compression:

Stored size: 1.44 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 (first = @params.delete(:first))
            @params[:page] ||= {}
            @params[:page][:size] = first
          end
          if (after = @params.delete(:after))
            @params[:page] ||= {}
            @params[:page][:after] = after
          end
          if (before = @params.delete(:before))
            @params[:page] ||= {}
            @params[:page][:before] = before
          end

          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

10 entries across 10 versions & 1 rubygems

Version Path
graphiti_graphql-0.1.17 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.16 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.15 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.14 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.12 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.11 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.10 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.9 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.8 lib/graphiti_graphql/federation/loaders/has_many.rb
graphiti_graphql-0.1.7 lib/graphiti_graphql/federation/loaders/has_many.rb