Skip to content

Commit

Permalink
feat!(pagination): paginate system_models
Browse files Browse the repository at this point in the history
Part of #779
Paginate system_model's `part_numbers` and `system_models` query with
relay

Signed-off-by: Luca Zaninotto <[email protected]>
  • Loading branch information
lusergit committed Jan 9, 2025
1 parent 6d3e198 commit d51982a
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 44 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 @@ -60,7 +60,8 @@ defmodule Edgehog.Devices do

list SystemModel, :system_models, :read do
description "Returns a list of system models."
paginate_with nil
relay? true
paginate_with :keyset
end
end

Expand Down
2 changes: 2 additions & 0 deletions backend/lib/edgehog/devices/system_model/system_model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ defmodule Edgehog.Devices.SystemModel do

graphql do
type :system_model

paginate_relationship_with part_numbers: :relay
end

actions do
Expand Down
2 changes: 2 additions & 0 deletions backend/lib/edgehog/devices/system_model_part_number.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ defmodule Edgehog.Devices.SystemModelPartNumber do

graphql do
type :system_model_part_number

paginate_relationship_with devices: :relay
end

actions do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ defmodule EdgehogWeb.Schema.Mutation.CreateSystemModelTest do
"name" => "Foobar",
"handle" => "foobar",
"pictureUrl" => nil,
"partNumbers" => part_numbers,
"partNumbers" => %{"edges" => part_numbers},
"hardwareType" => %{
"id" => ^hardware_type_id
}
} = system_model

assert length(part_numbers) == 2
assert %{"partNumber" => "123"} in part_numbers
assert %{"partNumber" => "456"} in part_numbers
assert %{"node" => %{"partNumber" => "123"}} in part_numbers
assert %{"node" => %{"partNumber" => "456"}} in part_numbers
end

test "allows passing localized descriptions", %{tenant: tenant} do
Expand Down Expand Up @@ -233,7 +233,11 @@ defmodule EdgehogWeb.Schema.Mutation.CreateSystemModelTest do
handle
pictureUrl
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
hardwareType {
id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do
"id" => _id,
"name" => "Updated Name",
"handle" => "updatedhandle",
"partNumbers" => [
%{"partNumber" => "updated-1234"}
]
"partNumbers" => %{
"edges" => [
%{
"node" => %{"partNumber" => "updated-1234"}
}
]
}
} = system_model
end

Expand All @@ -78,13 +82,15 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do
assert %{
"name" => "Only Name Update",
"handle" => ^old_handle,
"partNumbers" => part_numbers
"partNumbers" => %{
"edges" => part_numbers
}
} = system_model

assert length(part_numbers) == length(old_part_numbers)

Enum.each(old_part_numbers, fn pn ->
assert %{"partNumber" => pn} in part_numbers
assert %{"node" => %{"partNumber" => pn}} in part_numbers
end)
end

Expand All @@ -100,10 +106,10 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do

system_model = extract_result!(result)

assert %{"partNumbers" => part_numbers} = system_model
assert %{"partNumbers" => %{"edges" => part_numbers}} = system_model
assert length(part_numbers) == 2
assert %{"partNumber" => "B"} in part_numbers
assert %{"partNumber" => "D"} in part_numbers
assert %{"node" => %{"partNumber" => "B"}} in part_numbers
assert %{"node" => %{"partNumber" => "D"}} in part_numbers
end

test "allows updating localized descriptions", %{tenant: tenant} do
Expand Down Expand Up @@ -387,7 +393,11 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do
handle
pictureUrl
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions backend/test/edgehog_web/schema/query/device_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ defmodule EdgehogWeb.Schema.Query.DeviceTest do
systemModel {
id
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
}
}
Expand All @@ -71,7 +75,10 @@ defmodule EdgehogWeb.Schema.Query.DeviceTest do
|> extract_result!()

assert device["systemModel"]["id"] == system_model_id
assert device["systemModel"]["partNumbers"] == [%{"partNumber" => part_number}]

assert device["systemModel"]["partNumbers"]["edges"] == [
%{"node" => %{"partNumber" => part_number}}
]
end

test "queries associated OTA operations", %{tenant: tenant} do
Expand Down
25 changes: 17 additions & 8 deletions backend/test/edgehog_web/schema/query/devices_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,36 @@ defmodule EdgehogWeb.Schema.Query.DevicesTest do
systemModel {
id
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
}
}
}
"""

devices =
system_models =
[document: document, tenant: tenant]
|> devices_query()
|> extract_result!()
|> Enum.flat_map(fn device ->
system_model = get_in(device, ["systemModel", "partNumbers", "edges"])

if system_model,
do: system_model,
else: []
end)

assert Enum.count(devices, fn device ->
%{"partNumber" => part_number_1} in (device["systemModel"]["partNumbers"] || [])
assert Enum.count(system_models, fn element ->
element == %{"node" => %{"partNumber" => part_number_1}}
end) == 1

assert Enum.count(devices, fn device ->
%{"partNumber" => part_number_2} in (device["systemModel"]["partNumbers"] || [])
assert Enum.count(system_models, fn element ->
element == %{"node" => %{"partNumber" => part_number_2}}
end) == 2

assert Enum.count(devices, fn device -> device["systemModel"] == nil end) == 1
end

test "allows filtering", %{tenant: tenant} do
Expand Down
10 changes: 7 additions & 3 deletions backend/test/edgehog_web/schema/query/system_model_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ defmodule EdgehogWeb.Schema.Query.SystemModelTest do
assert system_model["name"] == fixture.name
assert system_model["handle"] == fixture.handle
assert system_model["pictureUrl"] == fixture.picture_url
assert length(system_model["partNumbers"]) == length(fixture.part_number_strings)
assert length(system_model["partNumbers"]["edges"]) == length(fixture.part_number_strings)

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

assert system_model["hardwareType"]["id"] ==
Expand Down Expand Up @@ -164,7 +164,11 @@ defmodule EdgehogWeb.Schema.Query.SystemModelTest do
handle
pictureUrl
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
hardwareType {
id
Expand Down
56 changes: 39 additions & 17 deletions backend/test/edgehog_web/schema/query/system_models_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule EdgehogWeb.Schema.Query.SystemModelsTest do

describe "systemModels query" do
test "returns empty system models", %{tenant: tenant} do
assert %{data: %{"systemModels" => []}} == system_models_query(tenant: tenant)
assert [] == [tenant: tenant] |> system_models_query() |> extract_result!()
end

test "returns system models if they're present", %{tenant: tenant} do
Expand All @@ -40,15 +40,15 @@ defmodule EdgehogWeb.Schema.Query.SystemModelsTest do
|> system_model_fixture()
|> Ash.load!(:part_number_strings)

assert %{data: %{"systemModels" => [system_model]}} = system_models_query(tenant: tenant)
assert [system_model] = [tenant: tenant] |> system_models_query() |> extract_result!()

assert system_model["name"] == fixture.name
assert system_model["handle"] == fixture.handle
assert system_model["pictureUrl"] == fixture.picture_url
assert length(system_model["partNumbers"]) == length(fixture.part_number_strings)
assert length(system_model["partNumbers"]["edges"]) == length(fixture.part_number_strings)

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

assert system_model["hardwareType"]["id"] ==
Expand All @@ -63,12 +63,14 @@ defmodule EdgehogWeb.Schema.Query.SystemModelsTest do
filter = %{
"or" => [
%{"handle" => %{"eq" => "foo"}},
%{"partNumbers" => %{"partNumber" => %{"eq" => "123-bar"}}}
%{
"partNumbers" => %{"partNumber" => %{"eq" => "123-bar"}}
}
]
}

assert %{data: %{"systemModels" => system_models}} =
system_models_query(tenant: tenant, filter: filter)
assert system_models =
[tenant: tenant, filter: filter] |> system_models_query() |> extract_result!()

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

assert %{data: %{"systemModels" => system_models}} =
system_models_query(tenant: tenant, sort: sort)
assert system_models =
[tenant: tenant, sort: sort] |> system_models_query() |> extract_result!()

assert [
%{"handle" => "1"},
Expand All @@ -101,14 +103,23 @@ defmodule EdgehogWeb.Schema.Query.SystemModelsTest do
"""
query SystemModels($filter: SystemModelFilterInput, $sort: [SystemModelSortInput]) {
systemModels(filter: $filter, sort: $sort) {
name
handle
pictureUrl
partNumbers {
partNumber
}
hardwareType {
id
count
edges {
node {
name
handle
pictureUrl
partNumbers {
edges {
node {
partNumber
}
}
}
hardwareType {
id
}
}
}
}
}
Expand All @@ -125,4 +136,15 @@ defmodule EdgehogWeb.Schema.Query.SystemModelsTest do

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

defp extract_result!(result) do
assert %{data: %{"systemModels" => %{"count" => count, "edges" => edges}}} = result
refute :errors in Map.keys(result)

system_models = Enum.map(edges, & &1["node"])

assert length(system_models) == count

system_models
end
end

0 comments on commit d51982a

Please sign in to comment.