mindok
mindok
AEAsh Elixir
Created by mindok on 5/10/2023 in #support
graphql
So I have thumbnail images stored as binaries that I want to expose via graphql as base 64 encoded strings. I have set a default read action with prepare build(load: [:thumbnail_raw, :thumbnail]) where thumbnail_raw is the binary field in the db and thumbnail is a calculated field. I also define the load callback in the calculation to ensure :thumbnail_raw is loaded before the encoding happens in the body of the calc - i.e. there are two places where I force :thumbnail_raw to be loaded. The calc works fine everywhere else, but returns nil when access via graphql. I have IO.inspected the records sent to the calculation and the key I have asked to be loaded isn't loaded. Versions are a little behind, but I've checked the commit logs for changes in these areas and can't see anything:
{:ash, "~> 2.4.0"},
{:ash_postgres, "~> 1.1.0"},
{:ash_phoenix, "~> 1.1.0"},
{:ash_admin, "~> 0.6.1"},
{:ash_json_api, "~> 0.31.1"},
{:ash_graphql, "~> 0.22.4"},
{:ash, "~> 2.4.0"},
{:ash_postgres, "~> 1.1.0"},
{:ash_phoenix, "~> 1.1.0"},
{:ash_admin, "~> 0.6.1"},
{:ash_json_api, "~> 0.31.1"},
{:ash_graphql, "~> 0.22.4"},
Calc is as follows:
use Ash.Calculation


@impl true
def load(_query, _, _) do
[:thumbnail_raw]
end

@impl true
def calculate(media_items, _opts, _context) do
Enum.map(media_items, fn media_item ->
IO.inspect media_item
if media_item.thumbnail_raw do
"#{Base.encode64(media_item.thumbnail_raw)}"
end
end)
end
use Ash.Calculation


@impl true
def load(_query, _, _) do
[:thumbnail_raw]
end

@impl true
def calculate(media_items, _opts, _context) do
Enum.map(media_items, fn media_item ->
IO.inspect media_item
if media_item.thumbnail_raw do
"#{Base.encode64(media_item.thumbnail_raw)}"
end
end)
end
4 replies