Ash FrameworkAF
Ash Framework4mo ago
6 replies
Shahryar

What Is the Best Way to Load Related Data with GET Requests, Paginated Posts of a category?

What Is the Best Way to Load Related Data with GET Requests, Such as Paginated Posts of a Specific category?

For example i do like this, i create media_limit and media_offset and i put it manually inside Ash.Query, but i think AshJson has better way?

 read :get_with_media do
      get? true
      multitenancy :bypass
      description "Gets a single media category with paginated media"

      argument :media_limit, :integer do
        allow_nil? true
        default 20
        constraints min: 1, max: 100
      end

      argument :media_offset, :integer do
        allow_nil? true
        default 0
        constraints min: 0
      end

      argument :site_id, :uuid do
        allow_nil? true
        description "Optional site_id to filter by (only for master users)"
      end

      prepare fn query, context ->
        tenant = context.tenant
        site_id_arg = Ash.Query.get_argument(query, :site_id)
        media_limit = Ash.Query.get_argument(query, :media_limit)
        media_offset = Ash.Query.get_argument(query, :media_offset)

        filtered_query =
          cond do
            not is_nil(tenant) ->
              Ash.Query.filter(query, site_id == ^tenant)

            not is_nil(site_id_arg) ->
              Ash.Query.filter(query, site_id == ^site_id_arg)

            true ->
              query
          end

        paginated_media =
          MishkaCms.Runtime.Media
          |> Ash.Query.for_read(:read)
          |> Ash.Query.limit(media_limit)
          |> Ash.Query.offset(media_offset)
          |> Ash.Query.sort(inserted_at: :desc)

        filtered_query
        |> Ash.Query.load(media: paginated_media)
      end
    end


Thank you in advance
Solution
I don't know if ash_json_api supports paginated relationships. You may need to open a feature request.
Was this page helpful?