Class: ElasticGraph::GraphQL::DecodedCursor::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/graphql/decoded_cursor.rb

Overview

Used to build decoded cursor values for the given ‘sort_fields`.

Defined Under Namespace

Modules: Null

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_sort_list(sort_list) ⇒ Object

Builds a factory from a list like: ‘[{ ’amount_money.amount’ => ‘asc’ }, { ‘created_at’ => ‘desc’ }]‘.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/elastic_graph/graphql/decoded_cursor.rb', line 79

def self.from_sort_list(sort_list)
  sort_fields = sort_list.map do |hash|
    if hash.values.any? { |v| !v.is_a?(::Hash) } || hash.values.flat_map(&:keys) != ["order"]
      raise InvalidSortFieldsError,
        "Given `sort_list` contained an invalid entry. Each must be a flat hash with one entry. Got: #{sort_list.inspect}"
    end

    # Steep thinks it could be `nil` because `hash.keys` could be empty, but we raise an error above in
    # that case, so we know this will wind up being a `String`. `_` here silences Steep's type check error.
    _ = hash.keys.first
  end

  if sort_fields.uniq.size < sort_fields.size
    raise InvalidSortFieldsError,
      "Given `sort_list` contains a duplicate field, which the CursorEncoder cannot handler. " \
      "The caller is responsible for de-duplicating the sort list fist. Got: #{sort_list.inspect}"
  end

  new(sort_fields)
end

Instance Method Details

#build(sort_values) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/elastic_graph/graphql/decoded_cursor.rb', line 100

def build(sort_values)
  unless sort_values.size == sort_fields.size
    raise CursorEncodingError,
      "size of sort values (#{sort_values.inspect}) does not match the " \
      "size of sort fields (#{sort_fields.inspect})"
  end

  DecodedCursor.new(sort_fields.zip(sort_values).to_h)
end