This is the channel query for that response
{
channel(id: "channel ID") {
id
channelMessages(offset: 0, limit: 100) {
count
hasNextPage
results {
...on TextChannelMessage {
id
}
...on ImageChannelMessages {
id
}
}
}
}
}
This is the calculation module
defmodule MyApp.ChannelMessages do
use Ash.Calculation
def load(_, _, context) do
limit = context[:limit] || 100
offset = context[:offset] || 0
[
:channel_messages_count,
messages: MyApp.Message |> Ash.Query.limit(limit) |> Ash.Query.offset(offset)
]
end
def calculate([record], _, _) do
{:ok,
[
%{
id: record.id,
count: record.channel_messages_count,
has_next_page: false,
results:
record.messages
|> Enum.map(
&%Ash.Union{type: MyApp.ChannelMessageUnion.struct_to_name(&1), value: &1}
)
}
]}
end
end
This calculation is set in the Channel resource.
We also made sure that in iex
we get the result for channel_messages from the calculation