mirror of
https://github.com/ash-project/ash_graphql.git
synced 2024-09-19 21:03:09 +12:00
improvement: validate action existence
This commit is contained in:
parent
ebafb10242
commit
83b77121d1
2 changed files with 48 additions and 1 deletions
|
@ -156,7 +156,8 @@ defmodule AshGraphql.Resource do
|
|||
}
|
||||
|
||||
@transformers [
|
||||
AshGraphql.Resource.Transformers.RequireIdPkey
|
||||
AshGraphql.Resource.Transformers.RequireIdPkey,
|
||||
AshGraphql.Resource.Transformers.ValidateActions
|
||||
]
|
||||
|
||||
@sections [@graphql]
|
||||
|
|
46
lib/resource/transformers/validate_actions.ex
Normal file
46
lib/resource/transformers/validate_actions.ex
Normal 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
|
Loading…
Reference in a new issue