sangeethailango

sangeethailango

Pagination is not working for related resources

I have two resources. The two are related to each other.

Article resource

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

Comments 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
end

When I try to get the related comments for Accounts.Blog.Article using the endpoint articles/:article_id/comments, the records are not paginated as expected. However, if I make a GET request to /articles, the articles are paginated as expected. Fetching the resource directly works, but pagination does not work for the related resource.

Most Liked

zachdaniel

zachdaniel

Creator of Ash

I don’t think pagination is implemented for relationship routes like that, but you can do this in your other resource:

routes do
  index "/articles/:article_id/comments", :read
end

...

actions do
  read :read do
    argument :article_id, :uuid

    prepare fn query, _ -> 
      case Ash.Query.fetch_argument(query, :article_id) do
        {:ok, article_id} -> Ash.Query.filter(query, article_id == ^article_id)
        :error -> query
      end
    end
  end  
end

Could you open a request against ash_json_api to implement pagination for related routes? For backwards compatibility it would have to default to false, but we could add related :comments, :read, paginate?: true to make it obey the pagination rules of the destination action.

sangeethailango

sangeethailango

Thank you for replying. I have opened a request on ash_jsob_api.

Issue: Implement pagination feature for related routes · Issue #230 · ash-project/ash_json_api · GitHub

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement