Sha256: d46447dd938d2400c97802589cb9742a0182cfbcae72606d4fa46ee43e1bfa08

Contents?: true

Size: 1.4 KB

Versions: 39

Compression:

Stored size: 1.4 KB

Contents

module GraphitiGql
  class Schema
    class Connection < ::GraphQL::Pagination::Connection
      def nodes
        return @items if @run_once
        @proxy = @items.proxy
        @items = @items.data
        @run_once = true
        @items
      end

      def proxy
        nodes
        @proxy
      end

      def has_previous_page
        proxy.pagination.has_previous_page?
      end

      def has_next_page
        nodes
        return false if @items.length.zero?
        cursor = JSON.parse(Base64.decode64(cursor_for(@items.last)))
        cursor["offset"] < @proxy.pagination.send(:item_count)
      end

      def cursor_for(item)
        nodes
        starting_offset = 0
        page_param = proxy.query.pagination
        if (page_number = page_param[:number])
          page_size = page_param[:size] || proxy.resource.default_page_size
          starting_offset = (page_number - 1) * page_size
        end
  
        if (cursor = page_param[:after])
          starting_offset = cursor[:offset]
        end
  
        current_offset = @items.index(item)
        offset = starting_offset + current_offset + 1 # (+ 1 b/c o-base index)
        Base64.encode64({offset: offset}.to_json).chomp
      end
    end

    class ToManyConnection < Connection
      def nodes
        return @items if @run_once
        @proxy = @items[1]
        @items = @items[0]
        @run_once = true
        @items
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
graphiti_gql-0.2.38 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.37 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.36 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.35 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.34 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.33 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.32 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.31 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.30 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.29 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.28 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.27 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.26 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.25 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.24 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.23 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.22 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.21 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.20 lib/graphiti_gql/schema/connection.rb
graphiti_gql-0.2.19 lib/graphiti_gql/schema/connection.rb