diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 13:00:54 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-04-20 13:00:54 +0300 |
commit | 3cccd102ba543e02725d247893729e5c73b38295 (patch) | |
tree | f36a04ec38517f5deaaacb5acc7d949688d1e187 /lib/gitlab/graphql/pagination/active_record_array_connection.rb | |
parent | 205943281328046ef7b4528031b90fbda70c75ac (diff) |
Add latest changes from gitlab-org/gitlab@14-10-stable-eev14.10.0-rc42
Diffstat (limited to 'lib/gitlab/graphql/pagination/active_record_array_connection.rb')
-rw-r--r-- | lib/gitlab/graphql/pagination/active_record_array_connection.rb | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/gitlab/graphql/pagination/active_record_array_connection.rb b/lib/gitlab/graphql/pagination/active_record_array_connection.rb new file mode 100644 index 00000000000..9e40f79b2fd --- /dev/null +++ b/lib/gitlab/graphql/pagination/active_record_array_connection.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +# Connection for an array of Active Record instances. +# Resolvers needs to handle cursors (before and after). +# This connection will handle (first and last). +# Supports batch loaded items. +# Expects the array to use a fixed DESC order. This is similar to +# ExternallyPaginatedArrayConnection. +module Gitlab + module Graphql + module Pagination + class ActiveRecordArrayConnection < GraphQL::Pagination::ArrayConnection + include ::Gitlab::Graphql::ConnectionCollectionMethods + prepend ::Gitlab::Graphql::ConnectionRedaction + + delegate :<<, to: :items + + def nodes + load_nodes + + @nodes + end + + def next_page? + load_nodes + + if before + true + elsif first + limit_value < items.size + else + false + end + end + + def previous_page? + load_nodes + + if after + true + elsif last + limit_value < items.size + else + false + end + end + + # see https://graphql-ruby.org/pagination/custom_connections#connection-wrapper + alias_method :has_next_page, :next_page? + alias_method :has_previous_page, :previous_page? + + def cursor_for(item) + # item could be a batch loaded item. Sync it to have the id. + cursor = { 'id' => Gitlab::Graphql::Lazy.force(item).id.to_s } + encode(cursor.to_json) + end + + # Part of the implied interface for default objects for BatchLoader: objects must be clonable + def dup + self.class.new( + items.dup, + first: first, + after: after, + max_page_size: max_page_size, + last: last, + before: before + ) + end + + private + + def limit_value + # note: only first _or_ last can be specified, not both + @limit_value ||= [first, last, max_page_size].compact.min + end + + def load_nodes + @nodes ||= begin + limited_nodes = items + + limited_nodes = limited_nodes.first(first) if first + limited_nodes = limited_nodes.last(last) if last + + limited_nodes + end + end + end + end + end +end |