Ash FrameworkAF
Ash Framework6mo ago
10 replies
Sienhopist

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
Solution
You can define a read action on the target resource
Was this page helpful?