Skip to content

Commit

Permalink
fix: generic action mutations need to use the input object
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Oct 2, 2023
1 parent d7a49bc commit 58a038e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/graphql/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ defmodule AshGraphql.Graphql.Resolver do

def resolve(
%{arguments: arguments, context: context} = resolution,
{api, resource, %AshGraphql.Resource.Action{name: query_name, action: action}}
{api, resource, %AshGraphql.Resource.Action{name: query_name, action: action}, input?}
) do
arguments =
if input? do
arguments[:input] || %{}
else
arguments
end

action = Ash.Resource.Info.action(resource, action)

case handle_arguments(resource, action, arguments) do
Expand Down
23 changes: 20 additions & 3 deletions lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ defmodule AshGraphql.Resource do
middleware:
action_middleware ++
[
{{AshGraphql.Graphql.Resolver, :resolve}, {api, resource, query}}
{{AshGraphql.Graphql.Resolver, :resolve}, {api, resource, query, false}}
],
complexity: {AshGraphql.Graphql.Resolver, :query_complexity},
module: schema,
Expand Down Expand Up @@ -501,13 +501,30 @@ defmodule AshGraphql.Resource do
Ash.Resource.Info.action(resource, action) ||
raise "No such action #{action} on #{resource}"

args =
case query_action.arguments do
[] ->
[]

_ ->
[
%Absinthe.Blueprint.Schema.InputValueDefinition{
identifier: :input,
module: schema,
name: "input",
placement: :argument_definition,
type: String.to_atom("#{name}_input")
}
]
end

%Absinthe.Blueprint.Schema.FieldDefinition{
arguments: generic_action_args(query_action, resource, schema),
arguments: args,
identifier: name,
middleware:
action_middleware ++
[
{{AshGraphql.Graphql.Resolver, :resolve}, {api, resource, query}}
{{AshGraphql.Graphql.Resolver, :resolve}, {api, resource, query, true}}
],
complexity: {AshGraphql.Graphql.Resolver, :query_complexity},
module: schema,
Expand Down
24 changes: 24 additions & 0 deletions test/generic_actions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,28 @@ defmodule AshGraphql.GenericActionsTest do

assert %{data: %{"randomPost" => %{"id" => ^post_id, "comments" => []}}} = result
end

test "generic action mutations can be run with input" do
post = AshGraphql.Test.Api.create!(Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar"))

resp =
"""
mutation {
randomPost(input: {published: true}) {
id
comments{
id
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema)

assert {:ok, result} = resp

refute Map.has_key?(result, :errors)
post_id = post.id

assert %{data: %{"randomPost" => %{"id" => ^post_id, "comments" => []}}} = result
end
end

0 comments on commit 58a038e

Please sign in to comment.