Skip to content

Implement pagination feature for related routes #230

@sangeethailango

Description

@sangeethailango

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Someday

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions