Skip to content

Commit 0e560ca

Browse files
committed
Use Query#params in pagination links insteam Query#hash.
Using Query#hash doesn't correct render links with include directives and filtering include relationships. Passing the valid parameters received on the original request seems like good approach.
1 parent 51fb889 commit 0e560ca

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

lib/graphiti/delegates/pagination.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def pagination_link(page)
2626
uri = URI(@proxy.resource.endpoint[:url].to_s)
2727

2828
# Overwrite the pagination query params with the desired page
29-
uri.query = @proxy.query.hash.merge({
29+
uri.query = @proxy.query.params.merge({
3030
page: {
3131
number: page,
3232
size: page_size

spec/delegates/pagination_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@
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 "is preserved correctly" 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
5669
end
5770

5871
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)