Ash FrameworkAF
Ash Framework3y ago
6 replies
Blibs

Custom type don't show all possible filters in GraphQL Api

In my project I have the following type:

defmodule Marketplace.Ash.Types.Currency do
  @moduledoc false

  use Ash.Type

  @impl true
  def storage_type, do: :decimal

  @impl true
  def cast_input(nil, _), do: {:ok, nil}

  def cast_input(value, _) do
    case Money.parse(value) do
      {:ok, value} -> {:ok, Money.to_decimal(value)}
      :error -> {:error, [reason: "failed to parse value"]}
    end
  end

  @impl true
  def cast_stored(nil, _), do: {:ok, nil}
  def cast_stored(value, _), do: {:ok, Money.parse!(value)}

  @impl true
  def dump_to_native(nil, _), do: {:ok, nil}
  def dump_to_native(%Decimal{} = value, _), do: {:ok, value}
  def dump_to_native(%Money{} = value, _), do: {:ok, Money.to_decimal(value)}
  def dump_to_native(_, _), do: :error

  def graphql_type, do: :money
end


I use it inside a resource like this:

attribute :price, Currency, allow_nil?: false


The issue I'm having is that now I can't see all the default filters that AshGraphQL generate for built-in supported fields.

So, in this case, AshGraphQL will generate a PropertyFilterPrice type which only have a isNil option, where, before, then that field was a :decimal, it would show a bunch of filter options like eq, greaterThan, greaterThanOrEqual, in, etc.

So, the question is, what is the correct approach to implement or make these filters available for custom types?
Was this page helpful?