Skip to content

Commit

Permalink
Merge pull request #783 from lusergit/feat/device_ssp
Browse files Browse the repository at this point in the history
feat!(pagination): paginate device relationships
  • Loading branch information
davidebriani authored Jan 9, 2025
2 parents 6d3e198 + cd18f7d commit 76d38d2
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 29 deletions.
5 changes: 5 additions & 0 deletions backend/lib/edgehog/devices/device/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ defmodule Edgehog.Devices.Device do

graphql do
type :device

# TODO: add :device_groups as a relay-paginated relationship. Since it's a
# manual relationship, it needs to implement callbacks that define
# datalayer subqueries so Ash can compose and support the functionality.
paginate_relationship_with ota_operations: :relay, tags: :relay
end

actions do
Expand Down
29 changes: 20 additions & 9 deletions backend/test/edgehog_web/schema/mutation/add_device_tags_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule EdgehogWeb.Schema.Mutation.AddDeviceTagsTest do
test "successfully adds tags", %{tenant: tenant, id: id} do
result = add_device_tags_mutation(tenant: tenant, id: id, tags: ["foo", "bar"])

assert %{"tags" => tags} = extract_result!(result)
assert tags = extract_result_tags!(result)
assert length(tags) == 2
tag_names = Enum.map(tags, &Map.fetch!(&1, "name"))
assert "foo" in tag_names
Expand All @@ -46,7 +46,7 @@ defmodule EdgehogWeb.Schema.Mutation.AddDeviceTagsTest do
result =
add_device_tags_mutation(tenant: tenant, id: id, tags: ["FOO", "bAr", " BaZ ", "baz"])

assert %{"tags" => tags} = extract_result!(result)
assert tags = extract_result_tags!(result)
assert length(tags) == 3
tag_names = Enum.map(tags, &Map.fetch!(&1, "name"))
assert "foo" in tag_names
Expand All @@ -62,7 +62,7 @@ defmodule EdgehogWeb.Schema.Mutation.AddDeviceTagsTest do
result =
add_device_tags_mutation(tenant: tenant, id: id, tags: ["foo", "baz"])

assert %{"tags" => tags} = extract_result!(result)
assert tags = extract_result_tags!(result)
assert length(tags) == 3
tag_names = Enum.map(tags, &Map.fetch!(&1, "name"))
assert "foo" in tag_names
Expand Down Expand Up @@ -92,8 +92,13 @@ defmodule EdgehogWeb.Schema.Mutation.AddDeviceTagsTest do
mutation AddDeviceTags($id: ID!, $input: AddDeviceTagsInput!) {
addDeviceTags(id: $id, input: $input) {
result {
tags {
name
tags(first: 10, sort: [{field: NAME, order: ASC}]) {
count
edges {
node {
name
}
}
}
}
}
Expand Down Expand Up @@ -122,21 +127,27 @@ defmodule EdgehogWeb.Schema.Mutation.AddDeviceTagsTest do
error
end

defp extract_result!(result) do
defp extract_result_tags!(result) do
refute :errors in Map.keys(result)
refute "errors" in Map.keys(result[:data])

assert %{
data: %{
"addDeviceTags" => %{
"result" => device
"result" => %{
"tags" => %{
"count" => count,
"edges" => edges
}
}
}
}
} = result

assert device != nil
tags = Enum.map(edges, & &1["node"])
assert length(tags) == count

device
tags
end

defp non_existing_device_id(tenant) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ defmodule EdgehogWeb.Schema.Mutation.RemoveDeviceTagsTest do
test "successfully removes tags", %{tenant: tenant, id: id} do
result = remove_device_tags_mutation(tenant: tenant, id: id, tags: ["foo"])

assert %{"tags" => [%{"name" => "bar"}]} = extract_result!(result)
assert [%{"name" => "bar"}] = extract_result!(result)
end

test "normalizes tag names", %{tenant: tenant, id: id} do
result = remove_device_tags_mutation(tenant: tenant, id: id, tags: ["FOO", " bar "])

assert %{"tags" => []} = extract_result!(result)
assert [] = extract_result!(result)
end

test "is idempotent and works with non-existing tags", ctx do
Expand All @@ -58,7 +58,7 @@ defmodule EdgehogWeb.Schema.Mutation.RemoveDeviceTagsTest do
result =
remove_device_tags_mutation(tenant: tenant, id: id, tags: ["bar", "baz"])

assert %{"tags" => [%{"name" => "foo"}]} = extract_result!(result)
assert [%{"name" => "foo"}] = extract_result!(result)
end

test "fails with empty tags", %{tenant: tenant, id: id} do
Expand All @@ -83,8 +83,13 @@ defmodule EdgehogWeb.Schema.Mutation.RemoveDeviceTagsTest do
mutation RemoveDeviceTags($id: ID!, $input: RemoveDeviceTagsInput!) {
removeDeviceTags(id: $id, input: $input) {
result {
tags {
name
tags(first: 10, sort: [{ field: NAME, order: ASC }]) {
count
edges {
node {
name
}
}
}
}
}
Expand Down Expand Up @@ -120,14 +125,21 @@ defmodule EdgehogWeb.Schema.Mutation.RemoveDeviceTagsTest do
assert %{
data: %{
"removeDeviceTags" => %{
"result" => device
"result" => %{
"tags" => %{
"count" => count,
"edges" => edges
}
}
}
}
} = result

assert device != nil
tags = Enum.map(edges, & &1["node"])

device
assert length(tags) == count

tags
end

defp non_existing_device_id(tenant) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateDeviceTest do
name
deviceId
online
otaOperations {
edges {
node {
id
}
}
}
}
}
}
Expand Down
46 changes: 34 additions & 12 deletions backend/test/edgehog_web/schema/query/device_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,44 @@ defmodule EdgehogWeb.Schema.Query.DeviceTest do
query ($id: ID!) {
device(id: $id) {
otaOperations {
id
baseImageUrl
count
edges {
cursor
node {
id
baseImageUrl
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
"""

%{"otaOperations" => [ota_operation]} =
[document: document, tenant: tenant, id: id]
|> device_query()
|> extract_result!()

assert ota_operation["id"] == ota_operation_id
assert ota_operation["baseImageUrl"] == base_image_url
assert %{
"otaOperations" => %{
"count" => 1,
"edges" => [
%{
"cursor" => cursor,
"node" => %{
"id" => ^ota_operation_id,
"baseImageUrl" => ^base_image_url
}
}
],
"pageInfo" => %{
"endCursor" => cursor,
"hasNextPage" => false
}
}
} =
[document: document, tenant: tenant, id: id]
|> device_query()
|> extract_result!()
end

test "returns nil if non existing", %{tenant: tenant} do
Expand Down Expand Up @@ -574,8 +598,6 @@ defmodule EdgehogWeb.Schema.Query.DeviceTest do
setup %{tenant: tenant} do
fixture = device_fixture(tenant: tenant)

device_id = fixture.device_id

id = AshGraphql.Resource.encode_relay_id(fixture)

document = """
Expand All @@ -588,7 +610,7 @@ defmodule EdgehogWeb.Schema.Query.DeviceTest do
}
"""

%{device: fixture, device_id: device_id, tenant: tenant, id: id, document: document}
%{device: fixture, tenant: tenant, id: id, document: document}
end

test "is empty with no groups", ctx do
Expand Down

0 comments on commit 76d38d2

Please sign in to comment.