Having the following type
defmodule MyApp.Types.DayOfWeek do
@moduledoc false
use Ash.Type.NewType,
subtype_of: :atom,
constraints: [one_of: [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]]
def graphql_input_type(_), do: :day_of_week
def graphql_type, do: :day_of_week
def graphql_type(_), do: :day_of_week
end
That is used in two resources:
defmodule ResourceA do
...
attributes do
...
attribute :days_of_week, {:array, MyApp.Types.DayOfWeek}
end
end
defmodule ResourceB do
...
attributes do
...
attribute :day_of_week, MyApp.Types.DayOfWeek}
end
relationships do
belongs_to :a_resource, ResourceA, allow_nil?: false
end
end
Causes the GraphQL type to be defined multiple times resulting in the following error:
== Compilation error in file lib/my_app/schema.ex ==
** (Absinthe.Schema.Error) Compilation failed:
---------------------------------------
## Locations
my_app/deps/ash_graphql/lib/resource/resource.ex:2358
my_app/deps/ash_graphql/lib/resource/resource.ex:2358
my_app/deps/ash_graphql/lib/resource/resource.ex:2358
my_app/deps/ash_graphql/lib/resource/resource.ex:2358
my_app/lib/my_app/graphql/types/common_types.ex:63
Type name "DayOfWeek" is not unique.
References to types must be unique.
> All types within a GraphQL schema must have unique names. No two provided
> types may have the same name. No provided type may have a name which
> conflicts with any built in types (including Scalar and Introspection
> types).
Reference: https://github.com/facebook/graphql/blob/master/spec/Section%203%20--%20Type%20System.md#type-system
---------------------------------------
Clues after debugging
After some debugging I think it's because the NewType is being defined per resource as we can see in resource.ex:2297.
I've inserted IO.inspect into get_auto_enums and after resource.ex:2342 and when my type was based on Ash.Type.NewType it was in these inspects, but when I've switched back to Ash.Type.Enum it wasn't.
I'm guessing that it's being treated as "inline enum", so when you defined enum like this:
attribute :some_enum, :atom do
constraints one_of: [:a, :b, :c]
end
because in these inspects I see "types" that are defined like this, the main difference is that for those it has unique name per resource.
It would work without defining def graphql_input, but for some reason to types defined like that the _input postfix isn't added like to those "inline enums", so there are same atoms returned for type_name and additional_type_name in resource.ex:2304.
When def graphql_input is defined, it uses this name everywhere, so there are more Locations with conflicts for Absinthe, as seen before.