diff --git a/lib/graphql/resolver.ex b/lib/graphql/resolver.ex index 2c195d91..802de417 100644 --- a/lib/graphql/resolver.ex +++ b/lib/graphql/resolver.ex @@ -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 diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index 1a5ad35f..8b1453c9 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -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, @@ -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, diff --git a/test/generic_actions_test.exs b/test/generic_actions_test.exs index 30d2af23..55a88521 100644 --- a/test/generic_actions_test.exs +++ b/test/generic_actions_test.exs @@ -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