Skip to content

Commit

Permalink
fix: properly thread read action arguments through on updates
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed May 19, 2023
1 parent 8679cd3 commit 20ce8b5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/graphql/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ defmodule AshGraphql.Graphql.Resolver do
|> Ash.Query.do_filter(filter)
|> Ash.Query.set_tenant(Map.get(context, :tenant))
|> Ash.Query.set_context(get_context(context))
|> set_query_arguments(action, read_action_input)
|> set_query_arguments(read_action, read_action_input)
|> api.read_one(
action: read_action,
verbose?: AshGraphql.Api.Info.debug?(api),
Expand Down
11 changes: 11 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ defmodule AshGraphql.Test.Post do
data_layer: Ash.DataLayer.Ets,
extensions: [AshGraphql.Resource]

require Ash.Query

graphql do
type :post

Expand Down Expand Up @@ -139,6 +141,7 @@ defmodule AshGraphql.Test.Post do
update :update_post_with_comments, :update_with_comments
update :update_post_confirm, :update_confirm
update :update_best_post, :update, read_action: :best_post, identity: false
update :update_best_post_arg, :update, read_action: :best_post_arg, identity: false

destroy :archive_post, :archive
destroy :delete_post, :destroy
Expand Down Expand Up @@ -233,6 +236,14 @@ defmodule AshGraphql.Test.Post do
filter(expr(best == true))
end

read :best_post_arg do
argument(:best, :boolean, allow_nil?: false)

prepare(fn query, _ ->
Ash.Query.filter(query, best == ^query.arguments.best)
end)
end

update :update, primary?: true

update :update_with_comments do
Expand Down
34 changes: 34 additions & 0 deletions test/update_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,40 @@ defmodule AshGraphql.UpdateTest do
resp
end

test "an update with a configured read action and no identity works with an argument the same name as an attribute" do
post =

Check warning on line 172 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci / mix test

variable "post" is unused (if the variable is not meant to be used, prefix it with an underscore)
AshGraphql.Test.Api.create!(
Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar", best: true)
)

resp =
"""
mutation UpdateBestPostArg($best: Boolean!, $input: UpdateBestPostArgInput) {
updateBestPostArg(best: $best, input: $input) {
result{
text
}
errors{
message
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{
"best" => true,
"input" => %{
"text" => "barbuz"
}
}
)

assert {:ok,
%{
data: %{"updateBestPostArg" => %{"errors" => [], "result" => %{"text" => "barbuz"}}}
}} = resp
end

test "arguments are threaded properly" do

Check failure on line 205 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci / mix test

test arguments are threaded properly (AshGraphql.UpdateTest)
post =
AshGraphql.Test.Api.create!(
Expand Down

0 comments on commit 20ce8b5

Please sign in to comment.