ash-project / ash-project/ash_json_api

Implement pagination feature for related routes

Open
#230 4 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
Elixir
Stars
94
Forks
80
Avg merge
5h 5m
Merged PRs (30d)
4

Description

**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:**

```defmodule Accounts.Blog.Article do
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

```defmodule Accounts.Blog.Comment do
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:**

- Implement pagination for related routes similar to how it is implemented for primary resources.
- I have already created an issue about this on elixir forum where @zachdaniel proposed a solution: https://elixirforum.com/t/pagination-is-not-working-for-related-resources/66057

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.