Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!(pagination): paginate existing_device_tags #788

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading