2020-08-14 09:39:59 +12:00
|
|
|
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
|
|
|
|
|
2021-01-13 09:14:35 +13:00
|
|
|
def transform(resource, dsl) do
|
|
|
|
if Ash.Type.embedded_type?(resource) do
|
|
|
|
{:ok, dsl}
|
|
|
|
else
|
|
|
|
primary_key =
|
|
|
|
dsl
|
|
|
|
|> Transformer.get_entities([:attributes])
|
|
|
|
|> Enum.filter(& &1.primary_key?)
|
2020-08-14 09:39:59 +12:00
|
|
|
|
2021-01-13 09:14:35 +13:00
|
|
|
case primary_key do
|
|
|
|
[_single] ->
|
|
|
|
{:ok, dsl}
|
2020-08-14 09:39:59 +12:00
|
|
|
|
2021-01-13 09:14:35 +13:00
|
|
|
[_ | _] ->
|
|
|
|
if AshGraphql.Resource.primary_key_delimiter(resource) do
|
|
|
|
{:ok, dsl}
|
|
|
|
else
|
|
|
|
{:error,
|
|
|
|
"AshGraphql requires a `primary_key_delimiter` to be set for composite primary keys."}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-14 09:39:59 +12:00
|
|
|
end
|
|
|
|
end
|