Sha256: 05ed12ab3f7a4037bea86f61111d603110316b67d69a26e2166c2595a809b330

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module GraphQL
  module Relay
    class ArrayConnection < BaseConnection
      def cursor_from_node(item)
        idx = starting_offset + sliced_nodes.find_index(item) + 1
        Base64.strict_encode64(idx.to_s)
      end

      def has_next_page
        !!(first && sliced_nodes.count > limit)
      end

      def has_previous_page
        !!(last && starting_offset > 0)
      end

      private

      # apply first / last limit results
      def paged_nodes
        @paged_nodes = begin
          items = sliced_nodes

          if limit
            items.first(limit)
          else
            items
          end
        end
      end

      # Apply cursors to edges
      def sliced_nodes
        @sliced_nodes ||= begin
          items = object
          items[starting_offset..-1]
        end
      end

      def index_from_cursor(cursor)
        Base64.decode64(cursor).to_i
      end

      def starting_offset
        @starting_offset = if before
          [previous_offset, 0].max
        else
          previous_offset
        end
      end

      def previous_offset
        @previous_offset ||= if after
          index_from_cursor(after)
        elsif before
          prev_page_size = [max_page_size, last].compact.min || 0
          index_from_cursor(before) - prev_page_size - 1
        else
          0
        end
      end

      def limit
        @limit ||= begin
          limit_from_arguments = if first
            first
          else
            if previous_offset < 0
              previous_offset + (last ? last : 0)
            else
              last
            end
          end
          [limit_from_arguments, max_page_size].compact.min
        end
      end
    end
    BaseConnection.register_connection_implementation(Array, ArrayConnection)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-0.18.0 lib/graphql/relay/array_connection.rb