improvement: validate action existence

This commit is contained in:
Zach Daniel 2021-03-08 14:22:41 -05:00
parent ebafb10242
commit 83b77121d1
2 changed files with 48 additions and 1 deletions

View file

@ -156,7 +156,8 @@ defmodule AshGraphql.Resource do
}
@transformers [
AshGraphql.Resource.Transformers.RequireIdPkey
AshGraphql.Resource.Transformers.RequireIdPkey,
AshGraphql.Resource.Transformers.ValidateActions
]
@sections [@graphql]

View file

@ -0,0 +1,46 @@
defmodule AshGraphql.Resource.Transformers.ValidateActions do
@moduledoc "Ensures that all referenced actiosn exist"
use Ash.Dsl.Transformer
alias Ash.Dsl.Transformer
def transform(resource, dsl) do
dsl
|> Transformer.get_entities([:graphql, :queries])
|> Enum.concat(Transformer.get_entities(dsl, [:graphql, :mutations]))
|> Enum.each(fn query_or_mutation ->
type =
case query_or_mutation do
%AshGraphql.Resource.Query{} ->
:read
%AshGraphql.Resource.Mutation{type: type} ->
type
end
unless Ash.Resource.Info.action(
resource,
query_or_mutation.action,
type
) do
available_actions =
resource
|> Ash.Resource.Info.actions()
|> Enum.filter(&(&1.type == type))
|> Enum.map(&" * #{&1.name}\n")
raise Ash.Error.Dsl.DslError,
module: __MODULE__,
message: """
No such action #{query_or_mutation.action} of type #{type} on #{inspect(resource)}
Available #{type} actions:
#{available_actions}
"""
end
end)
{:ok, dsl}
end
end