Skip to content

Commit fa80b38

Browse files
authored
Merge pull request #231 from gfmurphy/fix-pagination-links
Use Query#params in pagination links instead of Query#hash.
2 parents 51fb889 + 71fbe21 commit fa80b38

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

lib/graphiti/delegates/pagination.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ def links
2020

2121
private
2222

23+
def pagination_params
24+
@pagination_params ||= @proxy.query.params.reject { |key, _| [:action, :controller, :format].include?(key) }
25+
end
26+
2327
def pagination_link(page)
2428
return nil unless @proxy.resource.endpoint
2529

2630
uri = URI(@proxy.resource.endpoint[:url].to_s)
2731

2832
# Overwrite the pagination query params with the desired page
29-
uri.query = @proxy.query.hash.merge({
33+
uri.query = pagination_params.merge({
3034
page: {
3135
number: page,
3236
size: page_size

spec/delegates/pagination_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,32 @@
5353
expect(subject[:first]).to eq(pagination_link(1, size: Graphiti::Scoping::Paginate::DEFAULT_PAGE_SIZE))
5454
end
5555
end
56+
57+
context "with included relationship" do
58+
let(:params) {
59+
{include: "bar,bazzes", filter: {"bazzes.deprecated" => "foo"}}
60+
}
61+
62+
it "preserves include directive and filters on relationships" do
63+
query = URI.decode_www_form(URI(subject[:first]).query).to_h
64+
expect(query["include"]).to include("bar")
65+
expect(query["include"]).to include("bazzes")
66+
expect(query["filter[bazzes.deprecated]"]).to eq("foo")
67+
end
68+
end
69+
70+
context "with rails parameters" do
71+
let(:params) {
72+
{controller: "foos", action: "index", format: "jsonapi"}
73+
}
74+
75+
it "removes them" do
76+
page_links = subject.values
77+
expect(page_links).to all(satisfy { |v| v["controller=foos"].nil? })
78+
expect(page_links).to all(satisfy { |v| v["action=index"].nil? })
79+
expect(page_links).to all(satisfy { |v| v["format=jsonapi"].nil? })
80+
end
81+
end
5682
end
5783

5884
def pagination_link(number, size: current_per_page)

spec/support/pagination_link_helper.rb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,30 @@
22

33
RSpec.shared_context "pagination_context", shared_context: :metadata do
44
let(:proxy) do
5-
double(resource: resource, query: query, scope: scope)
65
Graphiti::ResourceProxy.new(resource, scope, query)
76
end
8-
let(:resource) { double(endpoint: endpoint, default_page_size: nil) }
9-
let(:query) { double(hash: params) }
7+
8+
let(:resource) do
9+
Class.new(Graphiti::Resource) do
10+
attribute :deprecated, :integer, filterable: true
11+
belongs_to :bar, resource: self
12+
has_many :bazzes, resource: self
13+
14+
def self.endpoint
15+
{
16+
path: "/foos",
17+
full_path: "/api/v2/foos",
18+
url: "http://localhost:3000/api/v2/foos",
19+
actions: [:index, :show, :create, :update, :destroy]
20+
}
21+
end
22+
23+
def self.name
24+
"foos"
25+
end
26+
end.new
27+
end
28+
let(:query) { Graphiti::Query.new(resource, params) }
1029
let(:scope) { double(object: collection, pagination: double(size: current_per_page)) }
1130
let(:pagination_delegate) { Graphiti::Delegates::Pagination.new(proxy) }
1231
let(:collection) do
@@ -33,11 +52,6 @@
3352
}
3453
end
3554
let(:endpoint) do
36-
{
37-
path: "/foos",
38-
full_path: "/api/v2/foos",
39-
url: "http://localhost:3000/api/v2/foos",
40-
actions: [:index, :show, :create, :update, :destroy]
41-
}
55+
resource.endpoint
4256
end
4357
end

0 commit comments

Comments
 (0)