What Is the Best Way to Load Related Data with GET Requests, Paginated Posts of a category?

What Is the Best Way to Load Related Data with GET Requests, Such as Paginated Posts of a Specific category? For example i do like this, i create media_limit and media_offset and i put it manually inside Ash.Query, but i think AshJson has better way?
read :get_with_media do
get? true
multitenancy :bypass
description "Gets a single media category with paginated media"

argument :media_limit, :integer do
allow_nil? true
default 20
constraints min: 1, max: 100
end

argument :media_offset, :integer do
allow_nil? true
default 0
constraints min: 0
end

argument :site_id, :uuid do
allow_nil? true
description "Optional site_id to filter by (only for master users)"
end

prepare fn query, context ->
tenant = context.tenant
site_id_arg = Ash.Query.get_argument(query, :site_id)
media_limit = Ash.Query.get_argument(query, :media_limit)
media_offset = Ash.Query.get_argument(query, :media_offset)

filtered_query =
cond do
not is_nil(tenant) ->
Ash.Query.filter(query, site_id == ^tenant)

not is_nil(site_id_arg) ->
Ash.Query.filter(query, site_id == ^site_id_arg)

true ->
query
end

paginated_media =
MishkaCms.Runtime.Media
|> Ash.Query.for_read(:read)
|> Ash.Query.limit(media_limit)
|> Ash.Query.offset(media_offset)
|> Ash.Query.sort(inserted_at: :desc)

filtered_query
|> Ash.Query.load(media: paginated_media)
end
end
read :get_with_media do
get? true
multitenancy :bypass
description "Gets a single media category with paginated media"

argument :media_limit, :integer do
allow_nil? true
default 20
constraints min: 1, max: 100
end

argument :media_offset, :integer do
allow_nil? true
default 0
constraints min: 0
end

argument :site_id, :uuid do
allow_nil? true
description "Optional site_id to filter by (only for master users)"
end

prepare fn query, context ->
tenant = context.tenant
site_id_arg = Ash.Query.get_argument(query, :site_id)
media_limit = Ash.Query.get_argument(query, :media_limit)
media_offset = Ash.Query.get_argument(query, :media_offset)

filtered_query =
cond do
not is_nil(tenant) ->
Ash.Query.filter(query, site_id == ^tenant)

not is_nil(site_id_arg) ->
Ash.Query.filter(query, site_id == ^site_id_arg)

true ->
query
end

paginated_media =
MishkaCms.Runtime.Media
|> Ash.Query.for_read(:read)
|> Ash.Query.limit(media_limit)
|> Ash.Query.offset(media_offset)
|> Ash.Query.sort(inserted_at: :desc)

filtered_query
|> Ash.Query.load(media: paginated_media)
end
end
Thank you in advance
Solution:
I don't know if ash_json_api supports paginated relationships. You may need to open a feature request.
Jump to solution
3 Replies
kernel
kernel2mo ago
if you use Ashjson I would assume that it loads what you request automagically no? as last I used JSON:API you could specify loads in the url?
Shahryar
ShahryarOP2mo ago
Sorry, I didn’t quite understand your question. I wanted to see if there’s a better way to display the information of a collection and all its posts using AshJson, for example! And the posts should also be paginated. i do not know :thinkies:
Solution
ZachDaniel
ZachDaniel2mo ago
I don't know if ash_json_api supports paginated relationships. You may need to open a feature request.

Did you find this page helpful?