Handle dynamic calculations and aggregates in AshGraphql

I have in my resources some actions that need to do some specific calculations and aggregates, since they are very specific for the action itself (they will never be used by other actions), I don’t want to add them to my resource global list of calculations and aggregates, because of that, I add them dynamically in a preparations like this:

    defmodule Bla do
      use Ash.Resource.Calculation
      def expression(_opts, _context) do
        expr(count(:property))
      end
    end

    read :blibs do
      prepare fn query, context ->
        query
        |> Ash.Query.calculate(:blibs, :integer, Bla)
        |> Ash.Query.aggregate(:blobs, :count, :property, query: [filter: [status: :draft]])
      end
    end

Now, when I call the action, I do the dynamic calculations in the calculations field and the dynamic aggregates in the aggregates field correctly, but I can’t see them in my GraphQL api for the same action.

Is there some way to make them visible to the graphql API?

From docs

The calculations declared on a resource allow for declaring a set of named calculations that can be used by extensions. … Calculations declared on the resource will be keys in the resource’s struct.

But you’re doing custom calculations.

I think the problem here is that, since the calculation is not declared on a resource, it’s not a present key in that resource type.
So

  graphql do
    type :your_resource
  end

will not have those dynamic calculations and it will not be picked up by the graphQL extension.

since they are very specific for the action itself (they will never be used by other actions)

wdym by this? A calculation is specific to some resource, it’s not global per say. I don’t see why you don’t write your calculations and aggregates normally to be honest. :man_shrugging:

1 Like

Because they get noisy in big resources. If I want to do a calculation that only makes sense in the context of an action, then I want to define it in that action only instead of making it available for the full resource.

1 Like

There are two main ways to go about this kind of thing without having to define additional resources etc.

Use action-specific metadata

read :blibs do
  prepare fn query, context ->
    query
     |> Ash.Query.calculate(:blibs, :integer, Bla)
     |> Ash.Query.aggregate(:blobs, :count, :property, query: [filter: [status: :draft]])
     |> Ash.Query.after_action(fn query, records -> 
       Enum.map(records, fn record -> 
         update_in(record.__metadata__, &Map.merge(&1, Map.take(record.calculations, [:blibs])
          # add stuff to `__metadata__`
       end)
     end)
   end

   metadata :blibs, :integer
   metadata :blobs, :integer
end

Then in your query, you must set show_metadata [:blibs, :blobs] as well as configure a new type_name (because you can’t reuse the type that is already defined that does not have these keys in it)

generic actions with custom return types

defmodule ThingWithExtra do
  use AshGraphql.Type

  use Ash.Type.NewType, subtype_of: :map, constraints: [
      fields: [
        thing: [type: :struct, constraints: [instance_of: Thing]],
        extra: [type: :string]
      ]
    ]

  def graphql_type(_), do: :thing_with_extra
end

action :thing_with_extra, ThingWithExtra do
  run fn input, _ ->
     # fetch and calculate additional stuff
     %{thing: thing, extra: "extra_stuff"}
  end
end
3 Likes

Amazing as always! Thanks a lot Zack!

1 Like

Just in case it is useful to someone, here is some preparations I created to abstract away some of this:

defmodule Core.Ash.Preparations.AddCalculationsToMetadata do
  @moduledoc """
  Add dynamic calculations values to metadata

  # Usage:

    prepare {#{inspect(__MODULE__)}, [:some_calculation]}
  """

  use Ash.Resource.Preparation

  def prepare(query, opts, _context) when is_list(opts) do
    Ash.Query.after_action(query, fn _, resources ->
      resources =
        Enum.map(resources, fn resource ->
          calculations = Map.take(resource.calculations, opts)

          update_in(resource.__metadata__, &Map.merge(&1, calculations))
        end)

      {:ok, resources}
    end)
  end
end
defmodule Core.Ash.Preparations.AddAggregatesToMetadata do
  @moduledoc """
  Add dynamic aggregates values to metadata

  # Usage:

    prepare {#{inspect(__MODULE__)}, [:some_aggregate]}
  """

  use Ash.Resource.Preparation

  def prepare(query, opts, _context) when is_list(opts) do
    Ash.Query.after_action(query, fn _, resources ->
      resources =
        Enum.map(resources, fn resource ->
          calculations = Map.take(resource.aggregates, opts)

          update_in(resource.__metadata__, &Map.merge(&1, calculations))
        end)

      {:ok, resources}
    end)
  end
end
2 Likes