Commit 444562e1 authored by Alex Kalderimis's avatar Alex Kalderimis Committed by Bob Van Landuyt

Add BaseResolver#offset_pagination helper

This simplifies use of offset-pagination
parent 7f659a4e
...@@ -109,6 +109,10 @@ module Resolvers ...@@ -109,6 +109,10 @@ module Resolvers
[args[:iid], args[:iids]].any? ? 0 : 0.01 [args[:iid], args[:iids]].any? ? 0 : 0.01
end end
def offset_pagination(relation)
::Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection.new(relation)
end
override :object override :object
def object def object
super.tap do |obj| super.tap do |obj|
......
...@@ -16,7 +16,7 @@ module Resolvers ...@@ -16,7 +16,7 @@ module Resolvers
filter_params = issue_filters(args[:filters]).merge(board_id: list.board.id, id: list.id) 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) 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 end
# https://gitlab.com/gitlab-org/gitlab/-/issues/235681 # https://gitlab.com/gitlab-org/gitlab/-/issues/235681
......
...@@ -27,7 +27,7 @@ module Resolvers ...@@ -27,7 +27,7 @@ module Resolvers
List.preload_preferences_for_user(lists, context[:current_user]) List.preload_preferences_for_user(lists, context[:current_user])
end end
Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection.new(lists) offset_pagination(lists)
end end
private private
......
...@@ -24,7 +24,7 @@ module Resolvers ...@@ -24,7 +24,7 @@ module Resolvers
if non_stable_cursor_sort?(args[:sort]) if non_stable_cursor_sort?(args[:sort])
# Certain complex sorts are not supported by the stable cursor pagination yet. # 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. # In these cases, we use offset pagination, so we return the correct connection.
Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection.new(issues) offset_pagination(issues)
else else
issues issues
end end
......
...@@ -86,6 +86,20 @@ However, there are some cases where we have to use the offset ...@@ -86,6 +86,20 @@ However, there are some cases where we have to use the offset
pagination connection, `OffsetActiveRecordRelationConnection`, such as when pagination connection, `OffsetActiveRecordRelationConnection`, such as when
sorting by label priority in issues, due to the complexity of the sort. 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 ### Keyset pagination
The keyset pagination implementation is a subclass of `GraphQL::Pagination::ActiveRecordRelationConnection`, The keyset pagination implementation is a subclass of `GraphQL::Pagination::ActiveRecordRelationConnection`,
...@@ -225,7 +239,7 @@ instead of an `ActiveRecord::Relation`: ...@@ -225,7 +239,7 @@ instead of an `ActiveRecord::Relation`:
if non_stable_cursor_sort?(args[:sort]) if non_stable_cursor_sort?(args[:sort])
# Certain complex sorts are not supported by the stable cursor pagination yet. # 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. # In these cases, we use offset pagination, so we return the correct connection.
Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection.new(issues) offset_pagination(issues)
else else
issues issues
end end
......
...@@ -40,7 +40,7 @@ module Resolvers ...@@ -40,7 +40,7 @@ module Resolvers
# Necessary for scopedPath computation in IterationPresenter # Necessary for scopedPath computation in IterationPresenter
context[:parent_object] = parent context[:parent_object] = parent
Gitlab::Graphql::Pagination::OffsetActiveRecordRelationConnection.new(iterations) offset_pagination(iterations)
end end
private private
......
...@@ -273,4 +273,12 @@ RSpec.describe Resolvers::BaseResolver do ...@@ -273,4 +273,12 @@ RSpec.describe Resolvers::BaseResolver do
end end
end 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 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