Load data works in terminal but in json api it returns empty

Inside this code i am trying to load all versions of a record for example, it works in my terminal and paper_trail_versions has some records as a list but in api json call the paper_trail_versions is [] empty
action :list_versions_api, :struct do
constraints instance_of: MishkaCms.Runtime.Layout
argument :layout_id, :uuid, allow_nil?: false
argument :layout, :map, default: %{}

run fn input, context ->
layout_id = input.arguments.layout_id

not_found_error =
{:error, Ash.Error.Query.NotFound.exception(resource: MishkaCms.Runtime.Layout)}

layout_params =
for {k, v} <- input.arguments.layout,
k in ["limit", "offset"],
into: %{},
do: {String.to_existing_atom(k), String.to_integer(v)}

with {:ok, layout} when not is_nil(layout) <-
Ash.get(MishkaCms.Runtime.Layout, layout_id, action: :get_any, authorize?: false),
:authorized <-
if(is_nil(context.tenant) or layout.site_id == context.tenant,
do: :authorized,
else: not_found_error
) do
limit = layout_params[:limit] || 20
offset = layout_params[:offset] || 0

# Load versions through the proper relationship
versions_load_query =
MishkaCms.Runtime.Layout.Version
|> Ash.Query.sort(version_inserted_at: :desc)
|> Ash.Query.limit(limit)
|> Ash.Query.offset(offset)

# Load the layout with versions
layout
|> Ash.load!([paper_trail_versions: versions_load_query], authorize?: false)
|> IO.inspect(label: "Loaded Layout=====")
|> then(&{:ok, &1})
else
{:error, _} = error -> error
_ -> not_found_error
end
end
end
action :list_versions_api, :struct do
constraints instance_of: MishkaCms.Runtime.Layout
argument :layout_id, :uuid, allow_nil?: false
argument :layout, :map, default: %{}

run fn input, context ->
layout_id = input.arguments.layout_id

not_found_error =
{:error, Ash.Error.Query.NotFound.exception(resource: MishkaCms.Runtime.Layout)}

layout_params =
for {k, v} <- input.arguments.layout,
k in ["limit", "offset"],
into: %{},
do: {String.to_existing_atom(k), String.to_integer(v)}

with {:ok, layout} when not is_nil(layout) <-
Ash.get(MishkaCms.Runtime.Layout, layout_id, action: :get_any, authorize?: false),
:authorized <-
if(is_nil(context.tenant) or layout.site_id == context.tenant,
do: :authorized,
else: not_found_error
) do
limit = layout_params[:limit] || 20
offset = layout_params[:offset] || 0

# Load versions through the proper relationship
versions_load_query =
MishkaCms.Runtime.Layout.Version
|> Ash.Query.sort(version_inserted_at: :desc)
|> Ash.Query.limit(limit)
|> Ash.Query.offset(offset)

# Load the layout with versions
layout
|> Ash.load!([paper_trail_versions: versions_load_query], authorize?: false)
|> IO.inspect(label: "Loaded Layout=====")
|> then(&{:ok, &1})
else
{:error, _} = error -> error
_ -> not_found_error
end
end
end
Thank you in advance
Solution:
But you'd add a calculation like sorted_versions or something
Jump to solution
6 Replies
ZachDaniel
ZachDaniel2w ago
loaded data is not automatically shown in the response on the route those fields should be included with fields[type]=whatever and then the logic you're doing here should be in a calculation that can be loaded
Shahryar
ShahryarOP2w ago
i am doing like this
http://localhost:4000/api/json/v1/layout/admin/7c38b0c9-38ed-4348-8d1a-8952b7a5042d/versions?fields%5Blayout%5D=id%2Cname%2Cpath%2Cdescription%2Ctemplate%2Cformat%2Cpriority%2Cextra%2Ccontent%2Cexample%2Cresource_links%2Cseo_tags%2Chelpers%2Cactive%2Cprecompile%2Cpermissions%2Csite_id%2Cpaper_trail_versions' \
http://localhost:4000/api/json/v1/layout/admin/7c38b0c9-38ed-4348-8d1a-8952b7a5042d/versions?fields%5Blayout%5D=id%2Cname%2Cpath%2Cdescription%2Ctemplate%2Cformat%2Cpriority%2Cextra%2Ccontent%2Cexample%2Cresource_links%2Cseo_tags%2Chelpers%2Cactive%2Cprecompile%2Cpermissions%2Csite_id%2Cpaper_trail_versions' \
ZachDaniel
ZachDaniel2w ago
paper_trail_versions is a relationship in this case so it should be included
Solution
ZachDaniel
ZachDaniel2w ago
But you'd add a calculation like sorted_versions or something
ZachDaniel
ZachDaniel2w ago
that returns :struct with constraints instance_of: Version or version_history etc.
Shahryar
ShahryarOP2w ago
i will try with calculation, but i just wanted to know can i do something like my code , it did not work whole day :)) Thank you for the fast response ♥️ i think calculation supports pagination from Version

Did you find this page helpful?