<commit_before>module GraphQL
  module Relay
    class RelationConnection < BaseConnection
      DEFAULT_ORDER = "id"

      def cursor_from_node(item)
        order_value = item.public_send(order_name)
        cursor_parts = [order, order_value]
        Base64.strict_encode64(cursor_parts.join(CURSOR_SEPARATOR))
      end

      def order
        @order ||= (super || DEFAULT_ORDER)
      end

      private

      # apply first / last limit results
      def paged_nodes
        @paged_nodes = begin
          items = sliced_nodes
          first && items = items.first(first)
          last && items.count > last && items = items.last(last)
          items
        end
      end

      # Apply cursors to edges
      def sliced_nodes
        @sliced_nodes ||= begin
          items = object

          if order
            items = items.order(items.table[order_name].public_send(order_direction))
          end

          if after
            _o, order_value = slice_from_cursor(after)
            direction_marker = order_direction == :asc ? ">" : "<"
            where_condition = create_order_condition(table_name, order_name, order_value, direction_marker)
            items = items.where(where_condition)
          end

          if before
            _o, order_value = slice_from_cursor(before)
            direction_marker = order_direction == :asc ? "<" : ">"
            where_condition = create_order_condition(table_name, order_name, order_value, direction_marker)
            items = items.where(where_condition)
          end

          items
        end
      end

      def slice_from_cursor(cursor)
        decoded = Base64.decode64(cursor)
        order, order_value = decoded.split(CURSOR_SEPARATOR)
      end

      # Remove possible direction marker:
      def order_name
        @order_name ||= order.sub(/^-/, '')
      end

      # Check for direction marker
      def order_direction
        @order_direction ||= order.start_with?("-") ? :desc : :asc
      end

      def table_name
        @table_name ||= object.table.table_name
      end

      def create_order_condition(table, column, value, direction_marker)
        table_name = ActiveRecord::Base.connection.quote_table_name(table)
        name = ActiveRecord::Base.connection.quote_column_name(column)
        ["#{table_name}.#{name} #{direction_marker} ?", value]
      end
    end


    if defined?(ActiveRecord)
      BaseConnection.register_connection_implementation(ActiveRecord::Relation, RelationConnection)
    end
  end
end
<commit_msg>Type cast the restored cursor value to correct type in order to let ActiveRecord use it in correct format to the connection adapter<commit_after>module GraphQL
  module Relay
    class RelationConnection < BaseConnection
      DEFAULT_ORDER = "id"

      def cursor_from_node(item)
        order_value = item.public_send(order_name)
        cursor_parts = [order, order_value]
        Base64.strict_encode64(cursor_parts.join(CURSOR_SEPARATOR))
      end

      def order
        @order ||= (super || DEFAULT_ORDER)
      end

      private

      # apply first / last limit results
      def paged_nodes
        @paged_nodes = begin
          items = sliced_nodes
          first && items = items.first(first)
          last && items.count > last && items = items.last(last)
          items
        end
      end

      # Apply cursors to edges
      def sliced_nodes
        @sliced_nodes ||= begin
          items = object

          if order
            items = items.order(items.table[order_name].public_send(order_direction))
          end

          if after
            _o, order_value = slice_from_cursor(after)
            direction_marker = order_direction == :asc ? ">" : "<"
            where_condition = create_order_condition(table_name, order_name, order_value, direction_marker)
            items = items.where(where_condition)
          end

          if before
            _o, order_value = slice_from_cursor(before)
            direction_marker = order_direction == :asc ? "<" : ">"
            where_condition = create_order_condition(table_name, order_name, order_value, direction_marker)
            items = items.where(where_condition)
          end

          items
        end
      end

      def slice_from_cursor(cursor)
        decoded = Base64.decode64(cursor)
        order, order_value = decoded.split(CURSOR_SEPARATOR)
      end

      # Remove possible direction marker:
      def order_name
        @order_name ||= order.sub(/^-/, '')
      end

      # Check for direction marker
      def order_direction
        @order_direction ||= order.start_with?("-") ? :desc : :asc
      end

      def table_name
        @table_name ||= object.table.table_name
      end

      def create_order_condition(table, column, value, direction_marker)
        table_name = ActiveRecord::Base.connection.quote_table_name(table)
        name = ActiveRecord::Base.connection.quote_column_name(column)
        casted_value = object.table.engine.columns_hash[column].cast_type.type_cast_from_user(value)
        ["#{table_name}.#{name} #{direction_marker} ?", casted_value]
      end
    end


    if defined?(ActiveRecord)
      BaseConnection.register_connection_implementation(ActiveRecord::Relation, RelationConnection)
    end
  end
end

{'fraction_non_alphanumeric': 0.08238851095993953, 'fraction_numerical': 0.0030234315948601664, 'mean_word_length': 2.343651295009476, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3506696', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1453, 'n_words': 326}