diff --git a/backend/lib/edgehog/devices/devices.ex b/backend/lib/edgehog/devices/devices.ex index 05448cd4b..f4a684dfd 100644 --- a/backend/lib/edgehog/devices/devices.ex +++ b/backend/lib/edgehog/devices/devices.ex @@ -60,7 +60,8 @@ defmodule Edgehog.Devices do list SystemModel, :system_models, :read do description "Returns a list of system models." - paginate_with nil + relay? true + paginate_with :keyset end end diff --git a/backend/lib/edgehog/devices/system_model/system_model.ex b/backend/lib/edgehog/devices/system_model/system_model.ex index e3b58bbd6..ee91d164d 100644 --- a/backend/lib/edgehog/devices/system_model/system_model.ex +++ b/backend/lib/edgehog/devices/system_model/system_model.ex @@ -43,6 +43,8 @@ defmodule Edgehog.Devices.SystemModel do graphql do type :system_model + + paginate_relationship_with part_numbers: :relay end actions do diff --git a/backend/lib/edgehog/devices/system_model_part_number.ex b/backend/lib/edgehog/devices/system_model_part_number.ex index 9a288f960..77b76d07b 100644 --- a/backend/lib/edgehog/devices/system_model_part_number.ex +++ b/backend/lib/edgehog/devices/system_model_part_number.ex @@ -28,6 +28,8 @@ defmodule Edgehog.Devices.SystemModelPartNumber do graphql do type :system_model_part_number + + paginate_relationship_with devices: :relay end actions do diff --git a/backend/test/edgehog_web/schema/mutation/create_system_model_test.exs b/backend/test/edgehog_web/schema/mutation/create_system_model_test.exs index 574d5fefb..66a595453 100644 --- a/backend/test/edgehog_web/schema/mutation/create_system_model_test.exs +++ b/backend/test/edgehog_web/schema/mutation/create_system_model_test.exs @@ -49,15 +49,15 @@ defmodule EdgehogWeb.Schema.Mutation.CreateSystemModelTest do "name" => "Foobar", "handle" => "foobar", "pictureUrl" => nil, - "partNumbers" => part_numbers, + "partNumbers" => %{"edges" => part_numbers}, "hardwareType" => %{ "id" => ^hardware_type_id } } = system_model assert length(part_numbers) == 2 - assert %{"partNumber" => "123"} in part_numbers - assert %{"partNumber" => "456"} in part_numbers + assert %{"node" => %{"partNumber" => "123"}} in part_numbers + assert %{"node" => %{"partNumber" => "456"}} in part_numbers end test "allows passing localized descriptions", %{tenant: tenant} do @@ -233,7 +233,11 @@ defmodule EdgehogWeb.Schema.Mutation.CreateSystemModelTest do handle pictureUrl partNumbers { - partNumber + edges { + node { + partNumber + } + } } hardwareType { id diff --git a/backend/test/edgehog_web/schema/mutation/update_system_model_test.exs b/backend/test/edgehog_web/schema/mutation/update_system_model_test.exs index d88722a6f..68727541a 100644 --- a/backend/test/edgehog_web/schema/mutation/update_system_model_test.exs +++ b/backend/test/edgehog_web/schema/mutation/update_system_model_test.exs @@ -57,9 +57,13 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do "id" => _id, "name" => "Updated Name", "handle" => "updatedhandle", - "partNumbers" => [ - %{"partNumber" => "updated-1234"} - ] + "partNumbers" => %{ + "edges" => [ + %{ + "node" => %{"partNumber" => "updated-1234"} + } + ] + } } = system_model end @@ -78,13 +82,15 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do assert %{ "name" => "Only Name Update", "handle" => ^old_handle, - "partNumbers" => part_numbers + "partNumbers" => %{ + "edges" => part_numbers + } } = system_model assert length(part_numbers) == length(old_part_numbers) Enum.each(old_part_numbers, fn pn -> - assert %{"partNumber" => pn} in part_numbers + assert %{"node" => %{"partNumber" => pn}} in part_numbers end) end @@ -100,10 +106,10 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do system_model = extract_result!(result) - assert %{"partNumbers" => part_numbers} = system_model + assert %{"partNumbers" => %{"edges" => part_numbers}} = system_model assert length(part_numbers) == 2 - assert %{"partNumber" => "B"} in part_numbers - assert %{"partNumber" => "D"} in part_numbers + assert %{"node" => %{"partNumber" => "B"}} in part_numbers + assert %{"node" => %{"partNumber" => "D"}} in part_numbers end test "allows updating localized descriptions", %{tenant: tenant} do @@ -387,7 +393,11 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do handle pictureUrl partNumbers { - partNumber + edges { + node { + partNumber + } + } } } } diff --git a/backend/test/edgehog_web/schema/query/device_test.exs b/backend/test/edgehog_web/schema/query/device_test.exs index 8e3f83333..beef7017d 100644 --- a/backend/test/edgehog_web/schema/query/device_test.exs +++ b/backend/test/edgehog_web/schema/query/device_test.exs @@ -58,7 +58,11 @@ defmodule EdgehogWeb.Schema.Query.DeviceTest do systemModel { id partNumbers { - partNumber + edges { + node { + partNumber + } + } } } } @@ -71,7 +75,10 @@ defmodule EdgehogWeb.Schema.Query.DeviceTest do |> extract_result!() assert device["systemModel"]["id"] == system_model_id - assert device["systemModel"]["partNumbers"] == [%{"partNumber" => part_number}] + + assert device["systemModel"]["partNumbers"]["edges"] == [ + %{"node" => %{"partNumber" => part_number}} + ] end test "queries associated OTA operations", %{tenant: tenant} do diff --git a/backend/test/edgehog_web/schema/query/devices_test.exs b/backend/test/edgehog_web/schema/query/devices_test.exs index 8b9099332..b30c33202 100644 --- a/backend/test/edgehog_web/schema/query/devices_test.exs +++ b/backend/test/edgehog_web/schema/query/devices_test.exs @@ -57,27 +57,36 @@ defmodule EdgehogWeb.Schema.Query.DevicesTest do systemModel { id partNumbers { - partNumber + edges { + node { + partNumber + } + } } } } } """ - devices = + system_models = [document: document, tenant: tenant] |> devices_query() |> extract_result!() + |> Enum.flat_map(fn device -> + system_model = get_in(device, ["systemModel", "partNumbers", "edges"]) + + if system_model, + do: system_model, + else: [] + end) - assert Enum.count(devices, fn device -> - %{"partNumber" => part_number_1} in (device["systemModel"]["partNumbers"] || []) + assert Enum.count(system_models, fn element -> + element == %{"node" => %{"partNumber" => part_number_1}} end) == 1 - assert Enum.count(devices, fn device -> - %{"partNumber" => part_number_2} in (device["systemModel"]["partNumbers"] || []) + assert Enum.count(system_models, fn element -> + element == %{"node" => %{"partNumber" => part_number_2}} end) == 2 - - assert Enum.count(devices, fn device -> device["systemModel"] == nil end) == 1 end test "allows filtering", %{tenant: tenant} do diff --git a/backend/test/edgehog_web/schema/query/system_model_test.exs b/backend/test/edgehog_web/schema/query/system_model_test.exs index 7c4a1f466..befc27e9b 100644 --- a/backend/test/edgehog_web/schema/query/system_model_test.exs +++ b/backend/test/edgehog_web/schema/query/system_model_test.exs @@ -46,10 +46,10 @@ defmodule EdgehogWeb.Schema.Query.SystemModelTest do assert system_model["name"] == fixture.name assert system_model["handle"] == fixture.handle assert system_model["pictureUrl"] == fixture.picture_url - assert length(system_model["partNumbers"]) == length(fixture.part_number_strings) + assert length(system_model["partNumbers"]["edges"]) == length(fixture.part_number_strings) Enum.each(fixture.part_number_strings, fn pn -> - assert(%{"partNumber" => pn} in system_model["partNumbers"]) + assert(%{"node" => %{"partNumber" => pn}} in system_model["partNumbers"]["edges"]) end) assert system_model["hardwareType"]["id"] == @@ -164,7 +164,11 @@ defmodule EdgehogWeb.Schema.Query.SystemModelTest do handle pictureUrl partNumbers { - partNumber + edges { + node { + partNumber + } + } } hardwareType { id diff --git a/backend/test/edgehog_web/schema/query/system_models_test.exs b/backend/test/edgehog_web/schema/query/system_models_test.exs index 144df42a6..f3e4abaed 100644 --- a/backend/test/edgehog_web/schema/query/system_models_test.exs +++ b/backend/test/edgehog_web/schema/query/system_models_test.exs @@ -25,7 +25,7 @@ defmodule EdgehogWeb.Schema.Query.SystemModelsTest do describe "systemModels query" do test "returns empty system models", %{tenant: tenant} do - assert %{data: %{"systemModels" => []}} == system_models_query(tenant: tenant) + assert [] == [tenant: tenant] |> system_models_query() |> extract_result!() end test "returns system models if they're present", %{tenant: tenant} do @@ -40,15 +40,15 @@ defmodule EdgehogWeb.Schema.Query.SystemModelsTest do |> system_model_fixture() |> Ash.load!(:part_number_strings) - assert %{data: %{"systemModels" => [system_model]}} = system_models_query(tenant: tenant) + assert [system_model] = [tenant: tenant] |> system_models_query() |> extract_result!() assert system_model["name"] == fixture.name assert system_model["handle"] == fixture.handle assert system_model["pictureUrl"] == fixture.picture_url - assert length(system_model["partNumbers"]) == length(fixture.part_number_strings) + assert length(system_model["partNumbers"]["edges"]) == length(fixture.part_number_strings) Enum.each(fixture.part_number_strings, fn pn -> - assert(%{"partNumber" => pn} in system_model["partNumbers"]) + assert(%{"node" => %{"partNumber" => pn}} in system_model["partNumbers"]["edges"]) end) assert system_model["hardwareType"]["id"] == @@ -63,12 +63,14 @@ defmodule EdgehogWeb.Schema.Query.SystemModelsTest do filter = %{ "or" => [ %{"handle" => %{"eq" => "foo"}}, - %{"partNumbers" => %{"partNumber" => %{"eq" => "123-bar"}}} + %{ + "partNumbers" => %{"partNumber" => %{"eq" => "123-bar"}} + } ] } - assert %{data: %{"systemModels" => system_models}} = - system_models_query(tenant: tenant, filter: filter) + assert system_models = + [tenant: tenant, filter: filter] |> system_models_query() |> extract_result!() assert length(system_models) == 2 assert "foo" in Enum.map(system_models, & &1["handle"]) @@ -85,8 +87,8 @@ defmodule EdgehogWeb.Schema.Query.SystemModelsTest do %{"field" => "HANDLE", "order" => "ASC"} ] - assert %{data: %{"systemModels" => system_models}} = - system_models_query(tenant: tenant, sort: sort) + assert system_models = + [tenant: tenant, sort: sort] |> system_models_query() |> extract_result!() assert [ %{"handle" => "1"}, @@ -101,14 +103,23 @@ defmodule EdgehogWeb.Schema.Query.SystemModelsTest do """ query SystemModels($filter: SystemModelFilterInput, $sort: [SystemModelSortInput]) { systemModels(filter: $filter, sort: $sort) { - name - handle - pictureUrl - partNumbers { - partNumber - } - hardwareType { - id + count + edges { + node { + name + handle + pictureUrl + partNumbers { + edges { + node { + partNumber + } + } + } + hardwareType { + id + } + } } } } @@ -125,4 +136,15 @@ defmodule EdgehogWeb.Schema.Query.SystemModelsTest do Absinthe.run!(document, EdgehogWeb.Schema, variables: variables, context: %{tenant: tenant}) end + + defp extract_result!(result) do + assert %{data: %{"systemModels" => %{"count" => count, "edges" => edges}}} = result + refute :errors in Map.keys(result) + + system_models = Enum.map(edges, & &1["node"]) + + assert length(system_models) == count + + system_models + end end