2024-02-11 06:02:56 +13:00
|
|
|
defmodule AshGraphql.Test.PageOfChannelMessagesCalculation do
|
|
|
|
@moduledoc false
|
2024-04-02 07:03:06 +13:00
|
|
|
use Ash.Resource.Calculation
|
2024-02-11 06:02:56 +13:00
|
|
|
|
|
|
|
def load(_, _, context) do
|
2024-04-02 07:03:06 +13:00
|
|
|
limit = context.arguments.limit || 100
|
|
|
|
offset = context.arguments.offset || 0
|
2024-02-11 06:02:56 +13:00
|
|
|
|
|
|
|
[
|
|
|
|
:channel_message_count,
|
2024-04-02 07:03:06 +13:00
|
|
|
messages:
|
|
|
|
AshGraphql.Test.Message
|
|
|
|
|> Ash.Query.limit(limit)
|
|
|
|
|> Ash.Query.offset(offset)
|
|
|
|
|> Ash.Query.select([:type, :text])
|
2024-02-11 06:02:56 +13:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def calculate([post], _, context) do
|
2024-04-02 07:03:06 +13:00
|
|
|
limit = context.arguments.limit || 100
|
|
|
|
offset = context.arguments.offset || 0
|
2024-02-11 06:02:56 +13:00
|
|
|
|
|
|
|
{:ok,
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
count: post.channel_message_count,
|
|
|
|
has_next_page: post.channel_message_count > offset + limit,
|
|
|
|
results:
|
|
|
|
post.messages
|
|
|
|
|> Enum.map(
|
|
|
|
&%Ash.Union{type: AshGraphql.Test.MessageUnion.struct_to_name(&1), value: &1}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
]}
|
|
|
|
end
|
|
|
|
end
|