class Graphiti::Scoping::Paginate

Constants

DEFAULT_PAGE_SIZE
PARAMS

Public Instance Methods

apply() click to toggle source
Calls superclass method Graphiti::Scoping::Base#apply
# File lib/graphiti/scoping/paginate.rb, line 6
def apply
  if size > resource.max_page_size
    raise Graphiti::Errors::UnsupportedPageSize
      .new(size, resource.max_page_size)
  elsif requested? && @opts[:sideload_parent_length].to_i > 1
    raise Graphiti::Errors::UnsupportedPagination
  else
    super
  end
end
apply?() click to toggle source

We want to apply this logic unless we’ve explicitly received the +default: false+ option. In that case, only apply if pagination was explicitly specified in the request.

@return [Boolean] should we apply this logic?

# File lib/graphiti/scoping/paginate.rb, line 22
def apply?
  if @opts[:default_paginate] == false
    requested?
  else
    true
  end
end
apply_custom_scope() click to toggle source

Apply the custom pagination proc

# File lib/graphiti/scoping/paginate.rb, line 47
def apply_custom_scope
  resource.instance_exec \
    @scope,
    number,
    size,
    resource.context,
    offset,
    &custom_scope
end
apply_standard_scope() click to toggle source

Apply default pagination proc via the Resource adapter

# File lib/graphiti/scoping/paginate.rb, line 36
def apply_standard_scope
  meth = resource.adapter.method(:paginate)

  if meth.arity == 4 # backwards-compat
    resource.adapter.paginate(@scope, number, size, offset)
  else
    resource.adapter.paginate(@scope, number, size)
  end
end
custom_scope() click to toggle source

@return [Proc, Nil] the custom pagination proc

# File lib/graphiti/scoping/paginate.rb, line 31
def custom_scope
  resource.pagination
end

Private Instance Methods

after_cursor() click to toggle source
# File lib/graphiti/scoping/paginate.rb, line 90
def after_cursor
  page_param[:after]
end
before_cursor() click to toggle source
# File lib/graphiti/scoping/paginate.rb, line 94
def before_cursor
  page_param[:before]
end
number() click to toggle source
# File lib/graphiti/scoping/paginate.rb, line 98
def number
  (page_param[:number] || 1).to_i
end
offset() click to toggle source
# File lib/graphiti/scoping/paginate.rb, line 67
def offset
  offset = nil

  if (value = page_param[:offset])
    offset = value.to_i
  end

  if before_cursor&.key?(:offset)
    if page_param.key?(:number)
      raise Errors::UnsupportedBeforeCursor
    end

    offset = before_cursor[:offset] - (size * number) - 1
    offset = 0 if offset.negative?
  end

  if after_cursor&.key?(:offset)
    offset = after_cursor[:offset]
  end

  offset
end
page_param() click to toggle source
# File lib/graphiti/scoping/paginate.rb, line 63
def page_param
  @page_param ||= (query_hash[:page] || {})
end
requested?() click to toggle source
# File lib/graphiti/scoping/paginate.rb, line 59
def requested?
  !PARAMS.map { |p| page_param[p] }.all?(&:nil?)
end
size() click to toggle source
# File lib/graphiti/scoping/paginate.rb, line 102
def size
  (page_param[:size] || resource.default_page_size || DEFAULT_PAGE_SIZE).to_i
end