Ash FrameworkAF
Ash Framework3y ago
19 replies
Myrmyr

`Ash.Type.NewType` defines multiple GraphQL types with the same name.

I have the following custom 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


Also I've defined a Scalar and imported it to schema
  scalar :day_of_week do
    parse(&parse_day_of_week/1)
    serialize(& &1)
  end

  defp parse_day_of_week(%Absinthe.Blueprint.Input.String{value: value}) do
    case MyApp.Types.DayOfWeek.cast_input(value, []) do
      {:ok, value} -> {:ok, value}
      _ -> :error
    end
  end
Was this page helpful?