Skip to content

Commit

Permalink
improvement: add modify_resolution for generic actions
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Dec 20, 2024
1 parent eeedddc commit 0779c6d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/graphql/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule AshGraphql.Graphql.Resolver do
%AshGraphql.Resource.Action{
name: query_name,
action: action,
modify_resolution: modify,
error_location: error_location
}, input?}
) do
Expand Down Expand Up @@ -54,10 +55,13 @@ defmodule AshGraphql.Graphql.Resolver do
tenant: Map.get(context, :tenant)
]

result =
input =
%Ash.ActionInput{domain: domain, resource: resource}
|> Ash.ActionInput.set_context(get_context(context))
|> Ash.ActionInput.for_action(action.name, arguments)

result =
input
|> Ash.run_action(opts)
|> case do
:ok ->
Expand Down Expand Up @@ -112,6 +116,8 @@ defmodule AshGraphql.Graphql.Resolver do
{:error, error}
end

modify_args = [input, result]

result =
if error_location == :in_result do
case result do
Expand All @@ -134,6 +140,7 @@ defmodule AshGraphql.Graphql.Resolver do
)
)
|> add_root_errors(domain, result)
|> modify_resolution(modify, modify_args)
end

{:error, error} ->
Expand Down
7 changes: 7 additions & 0 deletions lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ defmodule AshGraphql.Resource do
"If the result should have an `errors` and a `result` key (like create/update/destroy mutations), or if errors should be shown in the top level errors key",
default: :top_level
],
modify_resolution: [
type: :mfa,
doc: """
An MFA that will be called with the resolution, the query, and the result of the action as the first three arguments. See the [the guide](/documentation/topics/modifying-the-resolution.html) for more.
"""
],
relay_id_translations: [
type: :keyword_list,
doc: """
Expand All @@ -93,6 +99,7 @@ defmodule AshGraphql.Resource do
:description,
:relay_id_translations,
:error_location,
:modify_resolution,
hide_inputs: []
]
end
Expand Down

0 comments on commit 0779c6d

Please sign in to comment.