Commit 27d9b19b authored by Sean Arnold's avatar Sean Arnold Committed by Mayra Cabrera

Add ExternallyPaginatedArrayConnection for graphql

- included specs
- refactor some specs to be more generic
parent e46ecc8e
......@@ -12,6 +12,10 @@ module Gitlab
Gitlab::Graphql::FilterableArray,
Gitlab::Graphql::Connections::FilterableArrayConnection
)
GraphQL::Relay::BaseConnection.register_connection_implementation(
Gitlab::Graphql::ExternallyPaginatedArray,
Gitlab::Graphql::Connections::ExternallyPaginatedArrayConnection
)
end
end
end
......
# frozen_string_literal: true
# Make a customized connection type
module Gitlab
module Graphql
module Connections
class ExternallyPaginatedArrayConnection < GraphQL::Relay::ArrayConnection
# As the pagination happens externally
# we just return all the nodes here.
def sliced_nodes
@nodes
end
def start_cursor
nodes.previous_cursor
end
def end_cursor
nodes.next_cursor
end
def next_page?
end_cursor.present?
end
def previous_page?
start_cursor.present?
end
alias_method :has_next_page, :next_page?
alias_method :has_previous_page, :previous_page?
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Graphql
class ExternallyPaginatedArray < Array
attr_reader :previous_cursor, :next_cursor
def initialize(previous_cursor, next_cursor, *args)
super(args)
@previous_cursor = previous_cursor
@next_cursor = next_cursor
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Graphql::Connections::ExternallyPaginatedArrayConnection do
let(:prev_cursor) { 1 }
let(:next_cursor) { 6 }
let(:values) { [2, 3, 4, 5] }
let(:all_nodes) { Gitlab::Graphql::ExternallyPaginatedArray.new(prev_cursor, next_cursor, *values) }
let(:arguments) { {} }
subject(:connection) do
described_class.new(all_nodes, arguments)
end
describe '#sliced_nodes' do
let(:sliced_nodes) { connection.sliced_nodes }
it 'returns all the nodes' do
expect(connection.sliced_nodes).to eq(values)
end
end
describe '#paged_nodes' do
let(:paged_nodes) { connection.send(:paged_nodes) }
it_behaves_like "connection with paged nodes" do
let(:paged_nodes_size) { values.size }
end
end
describe '#start_cursor' do
it 'returns the prev cursor' do
expect(connection.start_cursor).to eq(prev_cursor)
end
context 'when there is none' do
let(:prev_cursor) { nil }
it 'returns nil' do
expect(connection.start_cursor).to eq(nil)
end
end
end
describe '#end_cursor' do
it 'returns the next cursor' do
expect(connection.end_cursor).to eq(next_cursor)
end
context 'when there is none' do
let(:next_cursor) { nil }
it 'returns nil' do
expect(connection.end_cursor).to eq(nil)
end
end
end
describe '#has_next_page' do
it 'returns true when there is a end cursor' do
expect(connection.has_next_page).to eq(true)
end
context 'there is no end cursor' do
let(:next_cursor) { nil }
it 'returns false' do
expect(connection.has_next_page).to eq(false)
end
end
end
describe '#has_previous_page' do
it 'returns true when there is a start cursor' do
expect(connection.has_previous_page).to eq(true)
end
context 'there is no start cursor' do
let(:prev_cursor) { nil }
it 'returns false' do
expect(connection.has_previous_page).to eq(false)
end
end
end
end
......@@ -14,7 +14,9 @@ describe Gitlab::Graphql::Connections::FilterableArrayConnection do
describe '#paged_nodes' do
let(:paged_nodes) { subject.paged_nodes }
it_behaves_like "connection with paged nodes"
it_behaves_like "connection with paged nodes" do
let(:paged_nodes_size) { 3 }
end
context 'when callback filters some nodes' do
let(:callback) { proc { |nodes| nodes[1..-1] } }
......
......@@ -232,7 +232,9 @@ describe Gitlab::Graphql::Connections::Keyset::Connection do
let_it_be(:all_nodes) { create_list(:project, 5) }
let(:paged_nodes) { subject.paged_nodes }
it_behaves_like "connection with paged nodes"
it_behaves_like "connection with paged nodes" do
let(:paged_nodes_size) { 3 }
end
context 'when both are passed' do
let(:arguments) { { first: 2, last: 2 } }
......
......@@ -2,7 +2,7 @@
RSpec.shared_examples 'connection with paged nodes' do
it 'returns the collection limited to max page size' do
expect(paged_nodes.size).to eq(3)
expect(paged_nodes.size).to eq(paged_nodes_size)
end
it 'is a loaded memoized array' do
......@@ -22,7 +22,7 @@ RSpec.shared_examples 'connection with paged nodes' do
let(:arguments) { { last: 2 } }
it 'returns only the last elements' do
expect(paged_nodes).to contain_exactly(all_nodes[3], all_nodes[4])
expect(paged_nodes).to contain_exactly(*all_nodes.last(2))
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