diff --git a/backend/lib/edgehog/devices/devices.ex b/backend/lib/edgehog/devices/devices.ex index df989582f..e61dd79e5 100644 --- a/backend/lib/edgehog/devices/devices.ex +++ b/backend/lib/edgehog/devices/devices.ex @@ -49,7 +49,8 @@ defmodule Edgehog.Devices do list HardwareType, :hardware_types, :read do description "Returns a list of hardware types." - paginate_with nil + paginate_with :keyset + relay? true end get SystemModel, :system_model, :read do diff --git a/backend/lib/edgehog/devices/hardware_type.ex b/backend/lib/edgehog/devices/hardware_type.ex index 44ec39cb7..59aa2ab10 100644 --- a/backend/lib/edgehog/devices/hardware_type.ex +++ b/backend/lib/edgehog/devices/hardware_type.ex @@ -1,7 +1,7 @@ # # This file is part of Edgehog. # -# Copyright 2021-2024 SECO Mind Srl +# Copyright 2021 - 2025 SECO Mind Srl # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -41,6 +41,8 @@ defmodule Edgehog.Devices.HardwareType do graphql do type :hardware_type + + paginate_relationship_with part_numbers: :relay end actions do diff --git a/backend/test/edgehog_web/schema/mutation/create_hardware_type_test.exs b/backend/test/edgehog_web/schema/mutation/create_hardware_type_test.exs index 994d78aea..a9f52c5d2 100644 --- a/backend/test/edgehog_web/schema/mutation/create_hardware_type_test.exs +++ b/backend/test/edgehog_web/schema/mutation/create_hardware_type_test.exs @@ -1,7 +1,7 @@ # # This file is part of Edgehog. # -# Copyright 2021-2024 SECO Mind Srl +# Copyright 2021 - 2025 SECO Mind Srl # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -41,9 +41,13 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do "id" => _, "name" => "Foobar", "handle" => "foobar", - "partNumbers" => part_numbers + "partNumbers" => %{ + "edges" => part_numbers + } } = hardware_type + part_numbers = extract_nodes!(part_numbers) + assert length(part_numbers) == 2 assert %{"partNumber" => "123"} in part_numbers assert %{"partNumber" => "456"} in part_numbers @@ -126,7 +130,11 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do name handle partNumbers { - partNumber + edges { + node { + partNumber + } + } } } } @@ -175,4 +183,8 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do hardware_type end + + defp extract_nodes!(data) do + Enum.map(data, &Map.fetch!(&1, "node")) + end end diff --git a/backend/test/edgehog_web/schema/mutation/update_hardware_type_test.exs b/backend/test/edgehog_web/schema/mutation/update_hardware_type_test.exs index 246fbe7f2..c4274870b 100644 --- a/backend/test/edgehog_web/schema/mutation/update_hardware_type_test.exs +++ b/backend/test/edgehog_web/schema/mutation/update_hardware_type_test.exs @@ -1,7 +1,7 @@ # # This file is part of Edgehog. # -# Copyright 2021-2024 SECO Mind Srl +# Copyright 2021 - 2025 SECO Mind Srl # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -56,9 +56,11 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do "id" => _id, "name" => "Updated Name", "handle" => "updatedhandle", - "partNumbers" => [ - %{"partNumber" => "updated-1234"} - ] + "partNumbers" => %{ + "edges" => [ + %{"node" => %{"partNumber" => "updated-1234"}} + ] + } } = hardware_type end @@ -77,9 +79,13 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do assert %{ "name" => "Only Name Update", "handle" => ^old_handle, - "partNumbers" => part_numbers + "partNumbers" => %{ + "edges" => part_numbers + } } = hardware_type + part_numbers = extract_nodes!(part_numbers) + assert length(part_numbers) == length(old_part_numbers) Enum.each(old_part_numbers, fn pn -> @@ -99,7 +105,10 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do hardware_type = extract_result!(result) - assert %{"partNumbers" => part_numbers} = hardware_type + assert %{"partNumbers" => %{"edges" => part_numbers}} = hardware_type + + part_numbers = extract_nodes!(part_numbers) + assert length(part_numbers) == 2 assert %{"partNumber" => "B"} in part_numbers assert %{"partNumber" => "D"} in part_numbers @@ -202,7 +211,11 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do name handle partNumbers { - partNumber + edges { + node { + partNumber + } + } } } } @@ -255,4 +268,8 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do hardware_type end + + defp extract_nodes!(data) do + Enum.map(data, &Map.fetch!(&1, "node")) + end end diff --git a/backend/test/edgehog_web/schema/query/hardware_type_test.exs b/backend/test/edgehog_web/schema/query/hardware_type_test.exs index 3fb06697d..c7fdab90a 100644 --- a/backend/test/edgehog_web/schema/query/hardware_type_test.exs +++ b/backend/test/edgehog_web/schema/query/hardware_type_test.exs @@ -32,16 +32,16 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypeTest do id = AshGraphql.Resource.encode_relay_id(fixture) - result = hardware_type_query(tenant: tenant, id: id) + hardware_type = [tenant: tenant, id: id] |> hardware_type_query() |> extract_result!() - refute Map.has_key?(result, :errors) - assert %{data: %{"hardwareType" => hardware_type}} = result assert hardware_type["name"] == fixture.name assert hardware_type["handle"] == fixture.handle - assert length(hardware_type["partNumbers"]) == length(fixture.part_number_strings) + assert length(hardware_type["partNumbers"]["edges"]) == length(fixture.part_number_strings) + + part_numbers = extract_nodes!(hardware_type["partNumbers"]["edges"]) Enum.each(fixture.part_number_strings, fn pn -> - assert(%{"partNumber" => pn} in hardware_type["partNumbers"]) + assert(%{"partNumber" => pn} in part_numbers) end) end @@ -67,7 +67,11 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypeTest do name handle partNumbers { - partNumber + edges { + node { + partNumber + } + } } } } @@ -82,4 +86,23 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypeTest do Absinthe.run!(document, EdgehogWeb.Schema, variables: variables, context: %{tenant: tenant}) end + + defp extract_result!(result) do + refute :errors in Map.keys(result) + refute "errors" in Map.keys(result[:data]) + + assert %{ + data: %{ + "hardwareType" => hardware_type + } + } = result + + assert hardware_type != nil + + hardware_type + end + + defp extract_nodes!(data) do + Enum.map(data, &Map.fetch!(&1, "node")) + end end diff --git a/backend/test/edgehog_web/schema/query/hardware_types_test.exs b/backend/test/edgehog_web/schema/query/hardware_types_test.exs index 846471aec..06e8c1f7c 100644 --- a/backend/test/edgehog_web/schema/query/hardware_types_test.exs +++ b/backend/test/edgehog_web/schema/query/hardware_types_test.exs @@ -1,7 +1,7 @@ # # This file is part of Edgehog. # -# Copyright 2021-2024 SECO Mind Srl +# Copyright 2021 - 2025 SECO Mind Srl # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -25,7 +25,7 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do describe "hardwareTypes query" do test "returns empty hardware types", %{tenant: tenant} do - assert %{data: %{"hardwareTypes" => []}} == hardware_types_query(tenant: tenant) + assert [] == [tenant: tenant] |> hardware_types_query() |> extract_result!() end test "returns hardware types if they're present", %{tenant: tenant} do @@ -34,14 +34,17 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do |> hardware_type_fixture() |> Ash.load!(:part_number_strings) - assert %{data: %{"hardwareTypes" => [hardware_type]}} = hardware_types_query(tenant: tenant) + assert [hardware_type] = + [tenant: tenant] |> hardware_types_query() |> extract_result!() |> extract_nodes!() assert hardware_type["name"] == fixture.name assert hardware_type["handle"] == fixture.handle - assert length(hardware_type["partNumbers"]) == length(fixture.part_number_strings) + assert length(hardware_type["partNumbers"]["edges"]) == length(fixture.part_number_strings) + + part_numbers = extract_nodes!(hardware_type["partNumbers"]["edges"]) Enum.each(fixture.part_number_strings, fn pn -> - assert(%{"partNumber" => pn} in hardware_type["partNumbers"]) + assert(%{"partNumber" => pn} in part_numbers) end) end @@ -57,8 +60,11 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do ] } - assert %{data: %{"hardwareTypes" => hardware_types}} = - hardware_types_query(tenant: tenant, filter: filter) + assert hardware_types = + [tenant: tenant, filter: filter] + |> hardware_types_query() + |> extract_result!() + |> extract_nodes!() assert length(hardware_types) == 2 assert "foo" in Enum.map(hardware_types, & &1["handle"]) @@ -75,8 +81,11 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do %{"field" => "HANDLE", "order" => "ASC"} ] - assert %{data: %{"hardwareTypes" => hardware_types}} = - hardware_types_query(tenant: tenant, sort: sort) + assert hardware_types = + [tenant: tenant, sort: sort] + |> hardware_types_query() + |> extract_result!() + |> extract_nodes!() assert [ %{"handle" => "1"}, @@ -91,10 +100,18 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do """ query HardwareTypes($filter: HardwareTypeFilterInput, $sort: [HardwareTypeSortInput]) { hardwareTypes(filter: $filter, sort: $sort) { - name - handle - partNumbers { - partNumber + edges { + node { + name + handle + partNumbers { + edges { + node { + partNumber + } + } + } + } } } } @@ -111,4 +128,25 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do Absinthe.run!(document, EdgehogWeb.Schema, variables: variables, context: %{tenant: tenant}) end + + defp extract_result!(result) do + refute :errors in Map.keys(result) + refute "errors" in Map.keys(result[:data]) + + assert %{ + data: %{ + "hardwareTypes" => %{ + "edges" => hardware_types + } + } + } = result + + assert hardware_types != nil + + hardware_types + end + + defp extract_nodes!(data) do + Enum.map(data, &Map.fetch!(&1, "node")) + end end