Commit 840299c3 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'ajk-offset-pagination' into 'master'

Add offset_pagination resolver helper

See merge request gitlab-org/gitlab!48493
parents 918ba112 444562e1
......@@ -109,6 +109,10 @@ module Resolvers
[args[:iid], args[:iids]].any? ? 0 : 0.01
end
def offset_pagination(relation)
::Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection.new(relation)
end
override :object
def object
super.tap do |obj|
......
......@@ -16,7 +16,7 @@ module Resolvers
filter_params = issue_filters(args[:filters]).merge(board_id: list.board.id, id: list.id)
service = ::Boards::Issues::ListService.new(list.board.resource_parent, context[:current_user], filter_params)
Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection.new(service.execute)
offset_pagination(service.execute)
end
# https://gitlab.com/gitlab-org/gitlab/-/issues/235681
......
......@@ -27,7 +27,7 @@ module Resolvers
List.preload_preferences_for_user(lists, context[:current_user])
end
Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection.new(lists)
offset_pagination(lists)
end
private
......
......@@ -24,7 +24,7 @@ module Resolvers
if non_stable_cursor_sort?(args[:sort])
# Certain complex sorts are not supported by the stable cursor pagination yet.
# In these cases, we use offset pagination, so we return the correct connection.
Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection.new(issues)
offset_pagination(issues)
else
issues
end
......
......@@ -86,6 +86,20 @@ However, there are some cases where we have to use the offset
pagination connection, `OffsetActiveRecordRelationConnection`, such as when
sorting by label priority in issues, due to the complexity of the sort.
If you return a relation from a resolver that is not suitable for keyset
pagination (due to the sort order for example), then you can use the
`BaseResolver#offset_pagination` method to wrap the relation in the correct
connection type. For example:
```ruby
def resolve(**args)
result = Finder.new(object, current_user, args).execute
result = offset_pagination(result) if needs_offset?(args[:sort])
result
end
```
### Keyset pagination
The keyset pagination implementation is a subclass of `GraphQL::Pagination::ActiveRecordRelationConnection`,
......@@ -225,7 +239,7 @@ instead of an `ActiveRecord::Relation`:
if non_stable_cursor_sort?(args[:sort])
# Certain complex sorts are not supported by the stable cursor pagination yet.
# In these cases, we use offset pagination, so we return the correct connection.
Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection.new(issues)
offset_pagination(issues)
else
issues
end
......
......@@ -40,7 +40,7 @@ module Resolvers
# Necessary for scopedPath computation in IterationPresenter
context[:parent_object] = parent
Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection.new(iterations)
offset_pagination(iterations)
end
private
......
......@@ -273,4 +273,12 @@ RSpec.describe Resolvers::BaseResolver do
end
end
end
describe '#offset_pagination' do
let(:instance) { resolver_instance(resolver) }
it 'is sugar for OffsetActiveRecordRelationConnection.new' do
expect(instance.offset_pagination(User.none)).to be_a(::Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment