Skip to content

Commit

Permalink
feat!(pagination): paginate hardware_type
Browse files Browse the repository at this point in the history
Part of #779
Paginates the `HardwareType`'s relationship `part_numbers` with relay
Paginates the `hardware_types` query with relay

Signed-off-by: Luca Zaninotto <[email protected]>
  • Loading branch information
lusergit committed Jan 10, 2025
1 parent 096bd19 commit 5058660
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 31 deletions.
3 changes: 2 additions & 1 deletion backend/lib/edgehog/devices/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ defmodule Edgehog.Devices do

list HardwareType, :hardware_types, :read do
description "Returns a list of hardware types."
paginate_with nil
paginate_with :keyset
relay? true
end

get SystemModel, :system_model, :read do
Expand Down
4 changes: 3 additions & 1 deletion backend/lib/edgehog/devices/hardware_type.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 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 @@ -41,6 +41,8 @@ defmodule Edgehog.Devices.HardwareType do

graphql do
type :hardware_type

paginate_relationship_with part_numbers: :relay
end

actions do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 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 @@ -41,9 +41,13 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do
"id" => _,
"name" => "Foobar",
"handle" => "foobar",
"partNumbers" => part_numbers
"partNumbers" => %{
"edges" => part_numbers
}
} = hardware_type

part_numbers = extract_nodes!(part_numbers)

assert length(part_numbers) == 2
assert %{"partNumber" => "123"} in part_numbers
assert %{"partNumber" => "456"} in part_numbers
Expand Down Expand Up @@ -126,7 +130,11 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do
name
handle
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
}
}
Expand Down Expand Up @@ -175,4 +183,8 @@ defmodule EdgehogWeb.Schema.Mutation.CreateHardwareTypeTest do

hardware_type
end

defp extract_nodes!(data) do
Enum.map(data, &Map.fetch!(&1, "node"))
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 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 @@ -56,9 +56,11 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do
"id" => _id,
"name" => "Updated Name",
"handle" => "updatedhandle",
"partNumbers" => [
%{"partNumber" => "updated-1234"}
]
"partNumbers" => %{
"edges" => [
%{"node" => %{"partNumber" => "updated-1234"}}
]
}
} = hardware_type
end

Expand All @@ -77,9 +79,13 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do
assert %{
"name" => "Only Name Update",
"handle" => ^old_handle,
"partNumbers" => part_numbers
"partNumbers" => %{
"edges" => part_numbers
}
} = hardware_type

part_numbers = extract_nodes!(part_numbers)

assert length(part_numbers) == length(old_part_numbers)

Enum.each(old_part_numbers, fn pn ->
Expand All @@ -99,7 +105,10 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do

hardware_type = extract_result!(result)

assert %{"partNumbers" => part_numbers} = hardware_type
assert %{"partNumbers" => %{"edges" => part_numbers}} = hardware_type

part_numbers = extract_nodes!(part_numbers)

assert length(part_numbers) == 2
assert %{"partNumber" => "B"} in part_numbers
assert %{"partNumber" => "D"} in part_numbers
Expand Down Expand Up @@ -202,7 +211,11 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do
name
handle
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
}
}
Expand Down Expand Up @@ -255,4 +268,8 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateHardwareTypeTest do

hardware_type
end

defp extract_nodes!(data) do
Enum.map(data, &Map.fetch!(&1, "node"))
end
end
35 changes: 29 additions & 6 deletions backend/test/edgehog_web/schema/query/hardware_type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypeTest do

id = AshGraphql.Resource.encode_relay_id(fixture)

result = hardware_type_query(tenant: tenant, id: id)
hardware_type = [tenant: tenant, id: id] |> hardware_type_query() |> extract_result!()

refute Map.has_key?(result, :errors)
assert %{data: %{"hardwareType" => hardware_type}} = result
assert hardware_type["name"] == fixture.name
assert hardware_type["handle"] == fixture.handle
assert length(hardware_type["partNumbers"]) == length(fixture.part_number_strings)
assert length(hardware_type["partNumbers"]["edges"]) == length(fixture.part_number_strings)

part_numbers = extract_nodes!(hardware_type["partNumbers"]["edges"])

Enum.each(fixture.part_number_strings, fn pn ->
assert(%{"partNumber" => pn} in hardware_type["partNumbers"])
assert(%{"partNumber" => pn} in part_numbers)
end)
end

Expand All @@ -67,7 +67,11 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypeTest do
name
handle
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
}
}
Expand All @@ -82,4 +86,23 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypeTest do

Absinthe.run!(document, EdgehogWeb.Schema, variables: variables, context: %{tenant: tenant})
end

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

assert %{
data: %{
"hardwareType" => hardware_type
}
} = result

assert hardware_type != nil

hardware_type
end

defp extract_nodes!(data) do
Enum.map(data, &Map.fetch!(&1, "node"))
end
end
64 changes: 51 additions & 13 deletions backend/test/edgehog_web/schema/query/hardware_types_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 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 All @@ -25,7 +25,7 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do

describe "hardwareTypes query" do
test "returns empty hardware types", %{tenant: tenant} do
assert %{data: %{"hardwareTypes" => []}} == hardware_types_query(tenant: tenant)
assert [] == [tenant: tenant] |> hardware_types_query() |> extract_result!()
end

test "returns hardware types if they're present", %{tenant: tenant} do
Expand All @@ -34,14 +34,17 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do
|> hardware_type_fixture()
|> Ash.load!(:part_number_strings)

assert %{data: %{"hardwareTypes" => [hardware_type]}} = hardware_types_query(tenant: tenant)
assert [hardware_type] =
[tenant: tenant] |> hardware_types_query() |> extract_result!() |> extract_nodes!()

assert hardware_type["name"] == fixture.name
assert hardware_type["handle"] == fixture.handle
assert length(hardware_type["partNumbers"]) == length(fixture.part_number_strings)
assert length(hardware_type["partNumbers"]["edges"]) == length(fixture.part_number_strings)

part_numbers = extract_nodes!(hardware_type["partNumbers"]["edges"])

Enum.each(fixture.part_number_strings, fn pn ->
assert(%{"partNumber" => pn} in hardware_type["partNumbers"])
assert(%{"partNumber" => pn} in part_numbers)
end)
end

Expand All @@ -57,8 +60,11 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do
]
}

assert %{data: %{"hardwareTypes" => hardware_types}} =
hardware_types_query(tenant: tenant, filter: filter)
assert hardware_types =
[tenant: tenant, filter: filter]
|> hardware_types_query()
|> extract_result!()
|> extract_nodes!()

assert length(hardware_types) == 2
assert "foo" in Enum.map(hardware_types, & &1["handle"])
Expand All @@ -75,8 +81,11 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do
%{"field" => "HANDLE", "order" => "ASC"}
]

assert %{data: %{"hardwareTypes" => hardware_types}} =
hardware_types_query(tenant: tenant, sort: sort)
assert hardware_types =
[tenant: tenant, sort: sort]
|> hardware_types_query()
|> extract_result!()
|> extract_nodes!()

assert [
%{"handle" => "1"},
Expand All @@ -91,10 +100,18 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do
"""
query HardwareTypes($filter: HardwareTypeFilterInput, $sort: [HardwareTypeSortInput]) {
hardwareTypes(filter: $filter, sort: $sort) {
name
handle
partNumbers {
partNumber
edges {
node {
name
handle
partNumbers {
edges {
node {
partNumber
}
}
}
}
}
}
}
Expand All @@ -111,4 +128,25 @@ defmodule EdgehogWeb.Schema.Query.HardwareTypesTest do

Absinthe.run!(document, EdgehogWeb.Schema, variables: variables, context: %{tenant: tenant})
end

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

assert %{
data: %{
"hardwareTypes" => %{
"edges" => hardware_types
}
}
} = result

assert hardware_types != nil

hardware_types
end

defp extract_nodes!(data) do
Enum.map(data, &Map.fetch!(&1, "node"))
end
end

0 comments on commit 5058660

Please sign in to comment.