Class: GraphQR::Pagination::PaginationExtension

Inherits:
GraphQL::Schema::FieldExtension
  • Object
show all
Defined in:
lib/graphqr/pagination/pagination_extension.rb

Overview

The PaginationExtension is used on the ‘GraphQR::Fields::BaseField`.

It adds the ‘per` and `page` arguments to the paginated field and uses the selected paginator resolver to add `nodes`, `edges` and `page_info` on the response

Constant Summary collapse

NO_PAGINATOR_ERROR =
'No paginator defined'
INVALID_PAGINATOR_ERROR =
'Invalid paginator'

Instance Method Summary collapse

Instance Method Details

#after_resolve(value:, arguments:, **_kwargs) ⇒ Object

Raises:

  • (GraphQL::ExecutionError)


29
30
31
32
33
# File 'lib/graphqr/pagination/pagination_extension.rb', line 29

def after_resolve(value:, arguments:, **_kwargs)
  raise GraphQL::ExecutionError, NO_PAGINATOR_ERROR unless GraphQR.paginator.present?

  call_resolver(value, arguments)
end

#applyObject



14
15
16
17
18
19
# File 'lib/graphqr/pagination/pagination_extension.rb', line 14

def apply
  field.argument :per, 'Int', required: false, default_value: 25,
                              description: 'The requested number of nodes for the page'
  field.argument :page, 'Int', required: false, default_value: 1,
                               description: 'The requested page number'
end

#call_resolver(value, arguments) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/graphqr/pagination/pagination_extension.rb', line 35

def call_resolver(value, arguments)
  case GraphQR.paginator
  when :pagy
    Resolvers::PagyResolver.new(value, items: arguments[:per], page: arguments[:page])
  else
    raise GraphQL::ExecutionError, INVALID_PAGINATOR_ERROR
  end
end

#resolve(object:, arguments:, **_kwargs) {|object, next_args| ... } ⇒ Object

Remove pagination args before passing it to a user method

Yields:

  • (object, next_args)


22
23
24
25
26
27
# File 'lib/graphqr/pagination/pagination_extension.rb', line 22

def resolve(object:, arguments:, **_kwargs)
  next_args = arguments.dup
  next_args.delete(:per)
  next_args.delete(:page)
  yield(object, next_args)
end