2021-03-09 08:22:41 +13:00
|
|
|
defmodule AshGraphql.Resource.Transformers.ValidateActions do
|
|
|
|
@moduledoc "Ensures that all referenced actiosn exist"
|
2022-08-31 13:08:16 +12:00
|
|
|
use Spark.Dsl.Transformer
|
2021-03-09 08:22:41 +13:00
|
|
|
|
2022-08-31 13:08:16 +12:00
|
|
|
alias Spark.Dsl.Transformer
|
2021-03-09 08:22:41 +13:00
|
|
|
|
2022-08-31 13:08:16 +12:00
|
|
|
def after_compile?, do: true
|
|
|
|
|
|
|
|
def transform(dsl) do
|
2021-03-09 08:22:41 +13:00
|
|
|
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
|
|
|
|
|
2022-08-31 13:08:16 +12:00
|
|
|
available_actions = Transformer.get_entities(dsl, [:actions]) || []
|
|
|
|
|
|
|
|
action =
|
|
|
|
Enum.find(available_actions, fn action ->
|
|
|
|
action.name == query_or_mutation.action
|
|
|
|
end)
|
|
|
|
|
|
|
|
unless action do
|
|
|
|
resource = Transformer.get_persisted(dsl, :module)
|
|
|
|
|
|
|
|
raise Spark.Error.DslError,
|
2021-03-09 08:22:41 +13:00
|
|
|
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
|