Skip to content

Commit ce56722

Browse files
authored
fix: apply destroy loads on initial query (#346)
1 parent 59b975b commit ce56722

File tree

2 files changed

+117
-17
lines changed

2 files changed

+117
-17
lines changed

lib/graphql/resolver.ex

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,7 @@ defmodule AshGraphql.Graphql.Resolver do
17921792
|> Ash.Query.set_context(get_context(context))
17931793
|> set_query_arguments(read_action, read_action_input)
17941794
|> Ash.Query.limit(1)
1795+
|> pre_load_for_mutation(domain, resource, resolution, context, mutation_name)
17951796

17961797
{result, modify_args} =
17971798
query
@@ -1811,23 +1812,7 @@ defmodule AshGraphql.Graphql.Resolver do
18111812
select:
18121813
get_select(resource, resolution, mutation_result_type(mutation_name), [
18131814
"result"
1814-
]),
1815-
load:
1816-
get_loads(
1817-
[
1818-
domain: domain,
1819-
tenant: Map.get(context, :tenant),
1820-
authorize?: AshGraphql.Domain.Info.authorize?(domain),
1821-
tracer: AshGraphql.Domain.Info.tracer(domain),
1822-
actor: Map.get(context, :actor)
1823-
],
1824-
resource,
1825-
resolution,
1826-
resolution.path,
1827-
context,
1828-
mutation_result_type(mutation_name),
1829-
["result"]
1830-
)
1815+
])
18311816
)
18321817
|> case do
18331818
%Ash.BulkResult{status: :success, records: [value]} ->
@@ -2068,6 +2053,29 @@ defmodule AshGraphql.Graphql.Resolver do
20682053
end
20692054
end
20702055

2056+
# Pre-load aggregates and calculations on the query before destruction
2057+
# to ensure they are available in the returned record for GraphQL serialization
2058+
defp pre_load_for_mutation(query, domain, resource, resolution, context, mutation_name) do
2059+
load_opts = [
2060+
domain: domain,
2061+
tenant: Map.get(context, :tenant),
2062+
authorize?: AshGraphql.Domain.Info.authorize?(domain),
2063+
tracer: AshGraphql.Domain.Info.tracer(domain),
2064+
actor: Map.get(context, :actor)
2065+
]
2066+
2067+
load_fields(
2068+
query,
2069+
load_opts,
2070+
resource,
2071+
resolution,
2072+
resolution.path,
2073+
context,
2074+
mutation_result_type(mutation_name),
2075+
["result"]
2076+
)
2077+
end
2078+
20712079
@doc false
20722080
def get_loads(
20732081
load_opts,

test/destroy_test.exs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ defmodule AshGraphql.DestroyTest do
99
end)
1010
end
1111

12+
defp create_post_with_comments(comment_count) do
13+
post =
14+
AshGraphql.Test.Post
15+
|> Ash.Changeset.for_create(:create, text: "test post")
16+
|> Ash.create!()
17+
18+
if comment_count > 0 do
19+
for i <- 1..comment_count do
20+
AshGraphql.Test.Comment
21+
|> Ash.Changeset.for_create(:create, text: "comment #{i}", post_id: post.id)
22+
|> Ash.create!()
23+
end
24+
end
25+
26+
AshGraphql.Test.Post
27+
|> Ash.get!(post.id)
28+
end
29+
1230
test "a destroy works" do
1331
post =
1432
AshGraphql.Test.Post
@@ -227,4 +245,78 @@ defmodule AshGraphql.DestroyTest do
227245
}
228246
}} = resp
229247
end
248+
249+
test "destroy mutation returns aggregates" do
250+
post = create_post_with_comments(2)
251+
252+
resp =
253+
"""
254+
mutation DeletePost($id: ID!) {
255+
deletePost(id: $id) {
256+
result {
257+
text
258+
commentCount
259+
}
260+
errors {
261+
message
262+
}
263+
}
264+
}
265+
"""
266+
|> Absinthe.run(AshGraphql.Test.Schema,
267+
variables: %{
268+
"id" => post.id
269+
}
270+
)
271+
272+
assert {:ok,
273+
%{
274+
data: %{
275+
"deletePost" => %{
276+
"result" => %{
277+
"text" => "test post",
278+
"commentCount" => 2
279+
},
280+
"errors" => []
281+
}
282+
}
283+
}} = resp
284+
end
285+
286+
test "destroy mutation returns zero aggregates" do
287+
post = create_post_with_comments(0)
288+
289+
resp =
290+
"""
291+
mutation DeletePost($id: ID!) {
292+
deletePost(id: $id) {
293+
result {
294+
text
295+
commentCount
296+
}
297+
errors {
298+
message
299+
}
300+
}
301+
}
302+
"""
303+
|> Absinthe.run(AshGraphql.Test.Schema,
304+
variables: %{
305+
"id" => post.id
306+
}
307+
)
308+
309+
assert {:ok,
310+
%{
311+
data: %{
312+
"deletePost" => %{
313+
"result" => %{
314+
"text" => "test post",
315+
"commentCount" => 0
316+
},
317+
"errors" => []
318+
}
319+
}
320+
}} = resp
321+
end
230322
end

0 commit comments

Comments
 (0)