How to paginate related data in json API?

This is just an example. Say I have Client and Data in many to many relationship. I have a json API to read the clients which defines a related route to read a client's related Data. Since the Data can be quite a lot, I want to force paginating it. I have a paginated read action defined in both Client and Data. However, when I call http://localhost:4000/api/v1/client/{id}/data the data is not paginated. Is this a bug or am I missing something? Here's the relevant code
# In Client
relationships do
many_to_many :data, MyApp.Data do
public? true
through MyApp.Client2Data
source_attribute_on_join_resource :client_id
destination_attribute_on_join_resource :data_id
end
end

actions do
defaults [:read, :destroy, update: :*]

# This action is duplicated in both Client and Data because at first I thought the json api called the one on Data
read :read_force_pagination do
description "Limited to 500 per page."

pagination do
keyset? true
required? true
default_limit 500
max_page_size 500
end
end
end

# In domain
base_route "/client", MyApp.Client do
index :read
related :data, :read_force_pagination, primary?: true
end
# In Client
relationships do
many_to_many :data, MyApp.Data do
public? true
through MyApp.Client2Data
source_attribute_on_join_resource :client_id
destination_attribute_on_join_resource :data_id
end
end

actions do
defaults [:read, :destroy, update: :*]

# This action is duplicated in both Client and Data because at first I thought the json api called the one on Data
read :read_force_pagination do
description "Limited to 500 per page."

pagination do
keyset? true
required? true
default_limit 500
max_page_size 500
end
end
end

# In domain
base_route "/client", MyApp.Client do
index :read
related :data, :read_force_pagination, primary?: true
end
Solution:
You can define a read action on the target resource
Jump to solution
5 Replies
sevenseacat
sevenseacat3mo ago
there's an open issue to support this https://github.com/ash-project/ash_json_api/issues/230
GitHub
Implement pagination feature for related routes · Issue #230 · as...
Problem: The pagination feature is currently not functioning as expected for related routes in the ash_json_api gem. Specifically, when fetching related records for a resource, the records are retu...
Sienhopist
SienhopistOP3mo ago
Ah, shame. Oh well.
Solution
ZachDaniel
ZachDaniel3mo ago
You can define a read action on the target resource
ZachDaniel
ZachDaniel3mo ago
And put it at that same route So the resulting API can look the same Just slightly less convenient
Sienhopist
SienhopistOP3mo ago
Huh hadn't thought of that I guess that's as good as it can get for now I'll mark that as the solution for now

Did you find this page helpful?