I’m trying to do a pagination for a list of “messages” on a calculated field, so my structure is more or like the following one:
Channel Resource
defmodule Organizations.Channel do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [
AshGraphql.Resource
]
require Ash.Query
# Some other configs ...
relationships do
has_many :messages, Channels.ChannelMessage do
private? true
destination_attribute :channel_id
api Channels
end
actions do
defaults [:create, :update, :destroy]
# This one creates a PageOfChannels
read :list do
pagination countable: true,
required?: true,
offset?: true,
max_page_size: 10
end
read :read do
primary? true
end
end
Channel Messages Resource
defmodule Channels.ChannelMessage do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [
AshGraphql.Resource
]
# Some other configs ...
actions do
# This creates a PageOfChannelMessages
read :list do
pagination countable: true,
required?: true,
offset?: true,
max_page_size: 10,
default_limit: 10
end
read :read do
primary? true
end
end
relationships do
belongs_to :channel, Channels.Channel do
allow_nil? false
primary_key? true
end
end
With that said, I want to access my ChannelMessages, from the Channel resource, so I added a calculation for it, but I also want to paginate them using the same Type and structure (in this case PageOfChannelMessages) to share entities on the UI side with ApolloClient
So I did:
calculations do
calculate :channel_messages, {:array, ChannelMessageUnion}, fn record, %{api: api} ->
record = api.load!(record, :messages)
record.messages
|> Enum.map(&%Ash.Union{type: ChannelMessageUnion.struct_to_name(&1), value: &1})
end
But with this calculation, my channel_messages is just an array with all the messages or just a trimmed array if I limit it, so I don’t see how I can calculate as a PageOf type since those types are not accessible or I don’t know how, so with this in mind, my question would be can I reuse the actual pagination of AshGraphQL o should I cook my own pagination to be able to re-use it across all my app (calculation and lists)