Sha256: a2bde86bf77eab7ba415ec7e3e1038397891588b1b36747d1788e90f4f5d5bc4

Contents?: true

Size: 1.78 KB

Versions: 67

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true
require "graphql/pagination/relation_connection"

module GraphQL
  module Pagination
    # Customizes `RelationConnection` to work with `ActiveRecord::Relation`s.
    class ActiveRecordRelationConnection < Pagination::RelationConnection
      private

      def relation_count(relation)
        int_or_hash = if already_loaded?(relation)
          relation.size
        elsif relation.respond_to?(:unscope)
          relation.unscope(:order).count(:all)
        else
          # Rails 3
          relation.count
        end
        if int_or_hash.is_a?(Integer)
          int_or_hash
        else
          # Grouped relations return count-by-group hashes
          int_or_hash.length
        end
      end

      def relation_limit(relation)
        if relation.is_a?(Array)
          nil
        else
          relation.limit_value
        end
      end

      def relation_offset(relation)
        if relation.is_a?(Array)
          nil
        else
          relation.offset_value
        end
      end

      def null_relation(relation)
        if relation.respond_to?(:none)
          relation.none
        else
          # Rails 3
          relation.where("1=2")
        end
      end

      def set_limit(nodes, limit)
        if already_loaded?(nodes)
          nodes.take(limit)
        else
          super
        end
      end

      def set_offset(nodes, offset)
        if already_loaded?(nodes)
          # If the client sent a bogus cursor beyond the size of the relation,
          # it might get `nil` from `#[...]`, so return an empty array in that case
          nodes[offset..-1] || []
        else
          super
        end
      end

      private

      def already_loaded?(relation)
        relation.is_a?(Array) || relation.loaded?
      end
    end
  end
end

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
graphql-2.3.7 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.3.6 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.3.5 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.0.31 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.3.2 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.3.1 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.3.0 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.2.14 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.2.13 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.2.12 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.0.29 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.1.12 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.2.11 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.2.10 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.2.9 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.2.8 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.2.7 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.2.6 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.1.11 lib/graphql/pagination/active_record_relation_connection.rb
graphql-2.0.28 lib/graphql/pagination/active_record_relation_connection.rb