Commit cb751a9a authored by David Fernandez's avatar David Fernandez

Add a GraphQL ActiveRecord array connection class

Similar to ExternallyPaginatedArrayConnection but with ActiveRecords
parent 363fd8a9
# 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
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Graphql::Pagination::ActiveRecordArrayConnection do
using RSpec::Parameterized::TableSyntax
let_it_be(:items) { create_list(:package_build_info, 3) }
let_it_be(:context) do
GraphQL::Query::Context.new(
query: GraphQL::Query.new(GitlabSchema, document: nil, context: {}, variables: {}),
values: {},
object: nil
)
end
let(:first) { nil }
let(:last) { nil }
let(:after) { nil }
let(:before) { nil }
let(:max_page_size) { nil }
let(:connection) do
described_class.new(
items,
context: context,
first: first,
last: last,
after: after,
before: before,
max_page_size: max_page_size
)
end
it_behaves_like 'a connection with collection methods'
it_behaves_like 'a redactable connection' do
let(:unwanted) { items[1] }
end
describe '#nodes' do
subject { connection.nodes }
it { is_expected.to match_array(items) }
context 'with first set' do
let(:first) { 2 }
it { is_expected.to match_array([items[0], items[1]]) }
end
context 'with last set' do
let(:last) { 2 }
it { is_expected.to match_array([items[1], items[2]]) }
end
end
describe '#next_page?' do
subject { connection.next_page? }
where(:before, :first, :max_page_size, :result) do
nil | nil | nil | false
1 | nil | nil | true
nil | 1 | nil | true
nil | 10 | nil | false
nil | 1 | 1 | true
nil | 1 | 10 | true
nil | 10 | 10 | false
end
with_them do
it { is_expected.to eq(result) }
end
end
describe '#previous_page?' do
subject { connection.previous_page? }
where(:after, :last, :max_page_size, :result) do
nil | nil | nil | false
1 | nil | nil | true
nil | 1 | nil | true
nil | 10 | nil | false
nil | 1 | 1 | true
nil | 1 | 10 | true
nil | 10 | 10 | false
end
with_them do
it { is_expected.to eq(result) }
end
end
describe '#cursor_for' do
let(:item) { items[0] }
let(:expected_result) do
GitlabSchema.cursor_encoder.encode(
Gitlab::Json.dump(id: item.id.to_s),
nonce: true
)
end
subject { connection.cursor_for(item) }
it { is_expected.to eq(expected_result) }
context 'with a BatchLoader::GraphQL item' do
let_it_be(:user) { create(:user) }
let(:item) { ::Gitlab::Graphql::Loaders::BatchModelLoader.new(::User, user.id).find }
let(:expected_result) do
GitlabSchema.cursor_encoder.encode(
Gitlab::Json.dump(id: user.id.to_s),
nonce: true
)
end
it { is_expected.to eq(expected_result) }
end
end
describe '#dup' do
subject { connection.dup }
it 'properly handles items duplication' do
connection2 = subject
connection2 << create(:package_build_info)
expect(connection.items).not_to eq(connection2.items)
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