ash_graphql/lib/resource/transformers/require_id_pkey.ex
2020-08-13 17:39:59 -04:00

20 lines
524 B
Elixir

defmodule AshGraphql.Resource.Transformers.RequireIdPkey do
@moduledoc "Ensures that the resource has a primary key called `id`"
use Ash.Dsl.Transformer
alias Ash.Dsl.Transformer
def transform(_resource, dsl) do
primary_key =
dsl
|> Transformer.get_entities([:attributes])
|> Enum.filter(& &1.primary_key?)
|> Enum.map(& &1.name)
unless primary_key == [:id] do
raise "AshGraphql currently requires the primary key to be a field called `id`"
end
{:ok, dsl}
end
end