AE
Ash Elixir•2y ago
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
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
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?
5 Replies
ZachDaniel
ZachDaniel•2y ago
šŸ¤” good question šŸ™‚
Blibs
BlibsOP•2y ago
I tried to find how/where the built-in fields define these filters, but failed miserably šŸ˜…
ZachDaniel
ZachDaniel•2y ago
can you try this:
def graphql_input_type, do: :money
def graphql_input_type, do: :money
(in addition to graphql_type)
Blibs
BlibsOP•2y ago
Yep, I was about to mention that haha. I had just found out that the AshGraphql.Resource.filter_fields function was reaching the rescue code block with a (RuntimeError) Could not determine graphql type for Marketplace.Ash.Types.Currency, please define: graphql_input_type/1! error. Maybe it would be nice to have a config option in ash_graphql to enable "warnings" from exceptions like this? Something like config :ash_graphql, enable_compile_warnings?: true that way I would catch that error without having to change the resource.ex code directly to print that warning
ZachDaniel
ZachDaniel•2y ago
I’m not sure if that specific solution would work, but if you could open an issue illustrating the problem that would be great šŸ™‚

Did you find this page helpful?