ash-project / ash_json_api

The JSON:API extension for the Ash Framework
https://hexdocs.pm/ash_json_api
MIT License
63 stars 43 forks source link

Implement pagination feature for related routes #230

Open sangeethailango opened 2 months ago

sangeethailango commented 2 months ago

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 returned without pagination, even though pagination is correctly implemented for the primary resource.

Example setup:

  use Ash.Resource,
    domain: Account.Domain,
    data_layer: AshPostgres.DataLayer,
    extensions: [AshJsonApi.Resource]

  ...

actions do
    read :read do  
      pagination countable: true, offset?: true, required?: true, default_limit: 10
    end
  end

  relationships do
    has_many :comments, Comment
  end
end

comment resource

  use Ash.Resource,
    domain: Account.Domain,
    data_layer: AshPostgres.DataLayer,
    extensions: [AshJsonApi.Resource]

  ...

  actions do
   read :read do  
      pagination countable: true, offset?: true, required?: true, default_limit: 10
   end
  end

  relationships do
    belongs_to, :article, Article
  end
end

Domain

defmodule Account.Domain do
  use Ash.Domain, extensions: [AshJsonApi.Domain]

  alias Accounts.Blog.{Article, Comment}

  json_api do
    routes do
      base_route "/articles", Article do
        get :read
        related :comments, :read
      end
    end
  end

When making a GET request to articles/:article_id/comments, the related comments are not paginated. However, pagination works correctly when fetching the articles directly from /articles.

Expected Behavior:

The pagination functionality should be applied to related routes just as it is for the primary resource. For instance, when accessing related comments through the endpoint articles/:article_id/comments, the response should include pagination controls and adhere to the pagination settings defined for the :read action on the Comment resource.

Proposed Solution: