Skip to content

Commit

Permalink
Use root level errors with AshGraphql
Browse files Browse the repository at this point in the history
They map more naturally with the error handling we were currently using. We
could possibly evaluate reverting this in a second phase.

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
  • Loading branch information
rbino committed Feb 6, 2024
1 parent e22d75c commit 26a46ef
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 54 deletions.
1 change: 1 addition & 0 deletions backend/lib/edgehog/devices/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule Edgehog.Devices do
]

graphql do
root_level_errors? true
end

resources do
Expand Down
1 change: 1 addition & 0 deletions backend/lib/edgehog/tenants/tenants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Edgehog.Tenants do
extensions: [AshGraphql.Api, AshJsonApi.Api]

graphql do
root_level_errors? true
end

json_api do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do
handle: "123Invalid$"
)

assert %{"fields" => ["handle"], "message" => "should only contain" <> _} =
assert %{fields: [:handle], message: "should only contain" <> _} =
extract_error!(result)
end

Expand All @@ -70,7 +70,7 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do
part_numbers: []
)

assert %{"fields" => ["part_numbers"], "message" => "must have 1 or more items"} =
assert %{fields: [:part_numbers], message: "must have 1 or more items"} =
extract_error!(result)
end

Expand All @@ -83,7 +83,7 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do
name: fixture.name
)

assert %{"fields" => ["name"], "message" => "has already been taken"} =
assert %{fields: [:name], message: "has already been taken"} =
extract_error!(result)
end

Expand All @@ -96,7 +96,7 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do
handle: fixture.handle
)

assert %{"fields" => ["handle"], "message" => "has already been taken"} =
assert %{fields: [:handle], message: "has already been taken"} =
extract_error!(result)
end

Expand Down Expand Up @@ -132,13 +132,6 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do
partNumber
}
}
errors {
code
fields
message
shortMessage
vars
}
}
}
"""
Expand All @@ -160,12 +153,8 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do

defp extract_error!(result) do
assert %{
data: %{
"createHardwareType" => %{
"result" => nil,
"errors" => [error]
}
}
data: nil,
errors: [error]
} = result

error
Expand All @@ -178,8 +167,7 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do
assert %{
data: %{
"createHardwareType" => %{
"result" => hardware_type,
"errors" => []
"result" => hardware_type
}
}
} = result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule EdgehogWeb.Schema.Mutation.DeleteHardwareTypeTest do
test "fails with non-existing id", %{tenant: tenant} do
result = delete_hardware_type_mutation(tenant: tenant, id: 123_789)

assert %{"fields" => ["id"], "message" => "could not be found"} = extract_error!(result)
assert %{fields: [:id], message: "could not be found"} = extract_error!(result)
end
end

Expand All @@ -53,13 +53,6 @@ defmodule EdgehogWeb.Schema.Mutation.DeleteHardwareTypeTest do
result {
id
}
errors {
code
fields
message
shortMessage
vars
}
}
}
"""
Expand All @@ -76,12 +69,8 @@ defmodule EdgehogWeb.Schema.Mutation.DeleteHardwareTypeTest do

defp extract_error!(result) do
assert %{
data: %{
"deleteHardwareType" => %{
"result" => nil,
"errors" => [error]
}
}
data: nil,
errors: [error]
} = result

error
Expand All @@ -94,8 +83,7 @@ defmodule EdgehogWeb.Schema.Mutation.DeleteHardwareTypeTest do
assert %{
data: %{
"deleteHardwareType" => %{
"result" => hardware_type,
"errors" => []
"result" => hardware_type
}
}
} = result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do
handle: "123Invalid$"
)

assert %{"fields" => ["handle"], "message" => "should only contain" <> _} =
assert %{fields: [:handle], message: "should only contain" <> _} =
extract_error!(result)
end

Expand All @@ -122,7 +122,7 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do
part_numbers: []
)

assert %{"fields" => ["part_numbers"], "message" => "must have 1 or more items"} =
assert %{fields: [:part_numbers], message: "must have 1 or more items"} =
extract_error!(result)
end

Expand All @@ -136,7 +136,7 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do
name: fixture.name
)

assert %{"fields" => ["name"], "message" => "has already been taken"} =
assert %{fields: [:name], message: "has already been taken"} =
extract_error!(result)
end

Expand All @@ -150,7 +150,7 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do
handle: fixture.handle
)

assert %{"fields" => ["handle"], "message" => "has already been taken"} =
assert %{fields: [:handle], message: "has already been taken"} =
extract_error!(result)
end

Expand Down Expand Up @@ -190,13 +190,6 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do
partNumber
}
}
errors {
code
fields
message
shortMessage
vars
}
}
}
"""
Expand All @@ -222,12 +215,8 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do

defp extract_error!(result) do
assert %{
data: %{
"updateHardwareType" => %{
"result" => nil,
"errors" => [error]
}
}
data: nil,
errors: [error]
} = result

error
Expand All @@ -240,8 +229,7 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do
assert %{
data: %{
"updateHardwareType" => %{
"result" => hardware_type,
"errors" => []
"result" => hardware_type
}
}
} = result
Expand Down

0 comments on commit 26a46ef

Please sign in to comment.