From 227365b0765a6cc704ea6f1ecadb633fe4b63240 Mon Sep 17 00:00:00 2001 From: Luca Zaninotto Date: Fri, 10 Jan 2025 11:04:07 +0100 Subject: [PATCH] feat!(pagination): paginate `existing_device_tags` Part of #779 Paginates the `existing_device_tags` query with relay Signed-off-by: Luca Zaninotto --- backend/lib/edgehog/labeling/labeling.ex | 6 ++-- backend/lib/edgehog/labeling/tag.ex | 11 ++++++- .../query/existing_device_tags_test.exs | 33 +++++++++++++++---- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/backend/lib/edgehog/labeling/labeling.ex b/backend/lib/edgehog/labeling/labeling.ex index 6c867e8ce..99e6f7578 100644 --- a/backend/lib/edgehog/labeling/labeling.ex +++ b/backend/lib/edgehog/labeling/labeling.ex @@ -1,7 +1,7 @@ # # This file is part of Edgehog. # -# Copyright 2022-2024 SECO Mind Srl +# Copyright 2022 - 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. @@ -32,7 +32,9 @@ defmodule Edgehog.Labeling do queries do list Tag, :existing_device_tags, :read_assigned_to_devices do - paginate_with nil + description "Returns the list of device tags associated to some device group." + relay? true + paginate_with :keyset end end end diff --git a/backend/lib/edgehog/labeling/tag.ex b/backend/lib/edgehog/labeling/tag.ex index f18cb8b8c..cb3ce542c 100644 --- a/backend/lib/edgehog/labeling/tag.ex +++ b/backend/lib/edgehog/labeling/tag.ex @@ -1,7 +1,7 @@ # # This file is part of Edgehog. # -# Copyright 2022 SECO Mind Srl +# Copyright 2022 - 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. @@ -36,6 +36,8 @@ defmodule Edgehog.Labeling.Tag do graphql do type :tag + + paginate_relationship_with device_tags: :relay end actions do @@ -52,6 +54,13 @@ defmodule Edgehog.Labeling.Tag do read :read_assigned_to_devices do description "Returns Tags currently assigned to some device." prepare build(filter: expr(exists(device_tags, true))) + + pagination do + required? false + offset? true + keyset? true + countable true + end end end diff --git a/backend/test/edgehog_web/schema/query/existing_device_tags_test.exs b/backend/test/edgehog_web/schema/query/existing_device_tags_test.exs index e4f8a4ecc..0c82e4a58 100644 --- a/backend/test/edgehog_web/schema/query/existing_device_tags_test.exs +++ b/backend/test/edgehog_web/schema/query/existing_device_tags_test.exs @@ -1,7 +1,7 @@ # # This file is part of Edgehog. # -# Copyright 2022 SECO Mind Srl +# Copyright 2022 - 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. @@ -34,7 +34,12 @@ defmodule EdgehogWeb.Schema.Query.ExistingDeviceTagsTest do |> Ash.Changeset.for_update(:add_tags, tags: ["foo", "bar"]) |> Ash.update!() - assert tags = [tenant: tenant] |> existing_device_tags_query() |> extract_result!() + assert tags = + [tenant: tenant] + |> existing_device_tags_query() + |> extract_result!() + |> extract_nodes!() + assert length(tags) == 2 tag_names = Enum.map(tags, &Map.fetch!(&1, "name")) assert "foo" in tag_names @@ -50,7 +55,10 @@ defmodule EdgehogWeb.Schema.Query.ExistingDeviceTagsTest do |> Ash.update!() assert [%{"name" => "bar"}] == - [tenant: tenant] |> existing_device_tags_query() |> extract_result!() + [tenant: tenant] + |> existing_device_tags_query() + |> extract_result!() + |> extract_nodes!() end test "does not return duplicates if a tag is assigned multiple times", %{tenant: tenant} do @@ -64,7 +72,12 @@ defmodule EdgehogWeb.Schema.Query.ExistingDeviceTagsTest do |> Ash.Changeset.for_update(:add_tags, tags: ["foo", "baz"]) |> Ash.update!() - assert tags = [tenant: tenant] |> existing_device_tags_query() |> extract_result!() + assert tags = + [tenant: tenant] + |> existing_device_tags_query() + |> extract_result!() + |> extract_nodes!() + assert length(tags) == 3 tag_names = Enum.map(tags, &Map.fetch!(&1, "name")) assert "foo" in tag_names @@ -77,7 +90,11 @@ defmodule EdgehogWeb.Schema.Query.ExistingDeviceTagsTest do document = """ query { existingDeviceTags { - name + edges { + node { + name + } + } } } """ @@ -89,9 +106,13 @@ defmodule EdgehogWeb.Schema.Query.ExistingDeviceTagsTest do defp extract_result!(result) do refute :errors in Map.keys(result) - assert %{data: %{"existingDeviceTags" => tags}} = result + assert %{data: %{"existingDeviceTags" => %{"edges" => tags}}} = result assert tags != nil tags end + + defp extract_nodes!(result) do + Enum.map(result, &Map.fetch!(&1, "node")) + end end