Sha256: 425e6e06c704fa020b8ec21c310d767c3ee39c5a4d88310149abf51a0e5fe8ea

Contents?: true

Size: 1.88 KB

Versions: 77

Compression:

Stored size: 1.88 KB

Contents

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

module GraphQL
  module Pagination
    class ArrayConnection < Pagination::Connection
      def nodes
        load_nodes
        @nodes
      end

      def has_previous_page
        load_nodes
        @has_previous_page
      end

      def has_next_page
        load_nodes
        @has_next_page
      end

      def cursor_for(item)
        idx = items.find_index(item) + 1
        encode(idx.to_s)
      end

      private

      def index_from_cursor(cursor)
        decode(cursor).to_i
      end

      # Populate all the pagination info _once_,
      # It doesn't do anything on subsequent calls.
      def load_nodes
        @nodes ||= begin
          sliced_nodes = if before && after
            items[index_from_cursor(after)..index_from_cursor(before)-1] || []
          elsif before
            items[0..index_from_cursor(before)-2] || []
          elsif after
            items[index_from_cursor(after)..-1] || []
          else
            items
          end

          @has_previous_page = if last
            # There are items preceding the ones in this result
            sliced_nodes.count > last
          elsif after
            # We've paginated into the Array a bit, there are some behind us
            index_from_cursor(after) > 0
          else
            false
          end

          @has_next_page = if first
            # There are more items after these items
            sliced_nodes.count > first
          elsif before
            # The original array is longer than the `before` index
            index_from_cursor(before) < items.length + 1
          else
            false
          end

          limited_nodes = sliced_nodes

          limited_nodes = limited_nodes.first(first) if first
          limited_nodes = limited_nodes.last(last) if last

          limited_nodes
        end
      end
    end
  end
end

Version data entries

77 entries across 77 versions & 2 rubygems

Version Path
graphql-1.13.23 lib/graphql/pagination/array_connection.rb
graphql-1.13.22 lib/graphql/pagination/array_connection.rb
graphql-1.13.21 lib/graphql/pagination/array_connection.rb
graphql-1.13.20 lib/graphql/pagination/array_connection.rb
graphql-1.13.19 lib/graphql/pagination/array_connection.rb
graphql-1.13.18 lib/graphql/pagination/array_connection.rb
graphql-1.13.17 lib/graphql/pagination/array_connection.rb
graphql-1.13.16 lib/graphql/pagination/array_connection.rb
graphql-1.13.15 lib/graphql/pagination/array_connection.rb
graphql-1.13.14 lib/graphql/pagination/array_connection.rb
graphql-1.13.13 lib/graphql/pagination/array_connection.rb
graphql_cody-1.13.0 lib/graphql/pagination/array_connection.rb
graphql-1.13.12 lib/graphql/pagination/array_connection.rb
graphql-2.0.5 lib/graphql/pagination/array_connection.rb
graphql-2.0.4 lib/graphql/pagination/array_connection.rb
graphql-2.0.3 lib/graphql/pagination/array_connection.rb
graphql-1.13.11 lib/graphql/pagination/array_connection.rb
graphql-2.0.2 lib/graphql/pagination/array_connection.rb
graphql-2.0.1 lib/graphql/pagination/array_connection.rb
graphql-1.13.10 lib/graphql/pagination/array_connection.rb