Skip to content

Commit

Permalink
feat!(pagination): paginate existing_device_tags
Browse files Browse the repository at this point in the history
Part of #779
Paginates the `existing_device_tags` query with relay

Signed-off-by: Luca Zaninotto <[email protected]>
  • Loading branch information
lusergit committed Jan 10, 2025
1 parent 096bd19 commit 227365b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
6 changes: 4 additions & 2 deletions backend/lib/edgehog/labeling/labeling.ex
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion backend/lib/edgehog/labeling/tag.ex
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -36,6 +36,8 @@ defmodule Edgehog.Labeling.Tag do

graphql do
type :tag

paginate_relationship_with device_tags: :relay
end

actions do
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -77,7 +90,11 @@ defmodule EdgehogWeb.Schema.Query.ExistingDeviceTagsTest do
document = """
query {
existingDeviceTags {
name
edges {
node {
name
}
}
}
}
"""
Expand All @@ -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

0 comments on commit 227365b

Please sign in to comment.