Skip to content

Commit

Permalink
Merge pull request #15 from mrexox/fix/support-empty-nodes
Browse files Browse the repository at this point in the history
fix: support empty nodes
  • Loading branch information
palkan authored Nov 7, 2023
2 parents 9c5e553 + 20e2738 commit 63f321a
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: [ '2.6', '2.7', '3.0' ]
ruby: [ '3.0', '3.1', '3.2' ]
env:
RUBY_IMAGE: ${{ matrix.ruby }}
name: Ruby ${{ matrix.ruby }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/gemfiles/
/pkg/
/spec/reports/
/spec/internal/tmp/
/tmp/
.pry_history
.rspec_status
Expand Down
2 changes: 1 addition & 1 deletion .standard.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby_version: 2.6
ruby_version: 2.7
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
ruby:
image: ruby:${RUBY_IMAGE:-3.0}-buster
image: ruby:${RUBY_IMAGE:-2.7}-bullseye
environment:
HISTFILE: /app/.bash_history
BUNDLE_PATH: /bundle
Expand Down
6 changes: 3 additions & 3 deletions graphql-connections.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.required_ruby_version = "> 2.5"
spec.required_ruby_version = "> 2.6"

spec.add_runtime_dependency "activerecord", ">= 5"
spec.add_runtime_dependency "graphql", [">= 1.10", "< 3.0"]
Expand All @@ -41,8 +41,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "pg", "~> 1.2"
spec.add_development_dependency "pry-byebug", "~> 3"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec-rails", "~> 5.0"
spec.add_development_dependency "rspec-rails", "~> 6.0"
spec.add_development_dependency "standard", "~> 1.1"
spec.add_development_dependency "test-prof", "~> 1.0"
spec.add_development_dependency "test-prof", "~> 1.0.0"
end
# rubocop:enable Metrics/BlockLength
2 changes: 1 addition & 1 deletion lefthook-local.dip_example.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pre-commit:
commands:
rubocop:
runner: dip {cmd}
run: dip {cmd}
2 changes: 1 addition & 1 deletion lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pre-commit:
rubocop:
tags: backend
glob: "**/*.rb"
runner: bundle exec standardrb --fix {staged_files} && git add {staged_files}
run: bundle exec standardrb --fix {staged_files} && git add {staged_files}
3 changes: 2 additions & 1 deletion lib/graphql/connections/keyset/asc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def has_next_page
false
end
end
# rubocop:enable Naming/PredicateName, Metrics/AbcSize, Metrics/MethodLength

private

# standard:disable Metrics/AbcSize, Metrics/MethodLength
def limited_relation
scope = sliced_relation
nodes = []
Expand Down Expand Up @@ -78,6 +78,7 @@ def sliced_relation_before(relation)
.where(arel_table[primary_key].lt(before_cursor_primary_key))
.or(relation.where(arel_table[field_key].lt(before_cursor_date)))
end
# standard:enable Metrics/AbcSize, Metrics/MethodLength
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/graphql/connections/keyset/desc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def has_next_page
false
end
end
# rubocop:enable Naming/PredicateName, Metrics/AbcSize, Metrics/MethodLength

# standard:disable Metrics/AbcSize, Metrics/MethodLength
def cursor_for(item)
cursor = [item[field_key], item[primary_key]].map { |value| serialize(value) }.join(@separator)
cursor = encode(cursor) if opaque_cursor
Expand Down Expand Up @@ -85,6 +85,7 @@ def sliced_relation_before(relation)
.where(arel_table[primary_key].gt(before_cursor_primary_key))
.or(relation.where(arel_table[field_key].gt(before_cursor_date)))
end
# standard:disable Metrics/AbcSize, Metrics/MethodLength
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/graphql/connections/primary_key/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ def initialize(*args, primary_key: nil, **kwargs)
end

def has_previous_page
return false if nodes.empty?

if last
nodes.any? && items_exist?(type: :query, search: nodes.first[primary_key], page_type: :previous)
items_exist?(type: :query, search: nodes.first[primary_key], page_type: :previous)
elsif after
items_exist?(type: :cursor, search: after_cursor, page_type: :previous)
else
Expand All @@ -26,6 +28,8 @@ def has_previous_page
end

def has_next_page
return false if nodes.empty?

if first
items_exist?(type: :query, search: nodes.last[primary_key], page_type: :next)
elsif before
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/keyset/desc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
expect(connection.has_previous_page).to be false
expect(connection.has_next_page).to be true
end

context "with empty relation" do
let(:relation) { Message.none }

it "returns no nodes and has_previous_page and has_next_page are false" do
expect(nodes.size).to eq 0
expect(connection.has_previous_page).to be false
expect(connection.has_next_page).to be false
end
end
end

describe ":first param" do
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/primary_key/asc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
expect(connection.has_previous_page).to be false
expect(connection.has_next_page).to be true
end

context "with empty relation" do
let(:relation) { Message.none }

it "returns no nodes and has_previous_page and has_next_page are false" do
expect(nodes.size).to eq 0
expect(connection.has_previous_page).to be false
expect(connection.has_next_page).to be false
end
end
end

describe ":first param" do
Expand Down

0 comments on commit 63f321a

Please sign in to comment.