Skip to content

Commit

Permalink
feat!(pagination): paginate base_image_collection
Browse files Browse the repository at this point in the history
Part of #779
Paginates base_image_collection's `:base_images` relationship and the
`base_images_collections` query

Signed-off-by: Luca Zaninotto <[email protected]>
  • Loading branch information
lusergit committed Jan 9, 2025
1 parent 6d3e198 commit 4c68371
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
2 changes: 2 additions & 0 deletions backend/lib/edgehog/base_images/base_image_collection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ defmodule Edgehog.BaseImages.BaseImageCollection do

graphql do
type :base_image_collection

paginate_relationship_with base_images: :relay
end

actions do
Expand Down
2 changes: 1 addition & 1 deletion backend/lib/edgehog/base_images/base_images.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule Edgehog.BaseImages do

list BaseImageCollection, :base_image_collections, :read do
description "Returns a list of base image collections."
paginate_with nil
relay? true
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ defmodule EdgehogWeb.Schema.Mutation.CreateBaseImageCollectionTest do
"id" => ^system_model_id,
"name" => ^system_model_name,
"handle" => ^system_model_handle
},
"baseImages" => %{
"edges" => []
}
} = base_image_collection
end
Expand Down Expand Up @@ -159,6 +162,15 @@ defmodule EdgehogWeb.Schema.Mutation.CreateBaseImageCollectionTest do
name
handle
}
baseImages {
count
edges {
node {
id
version
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,17 @@ defmodule EdgehogWeb.Schema.Query.BaseImageCollectionTest do
result = base_image_collection_query(tenant: tenant, id: base_image_collection_id)

assert %{
"baseImages" => [
%{
"id" => ^base_image_id,
"version" => "2.0.0"
}
]
"baseImages" => %{
"count" => 1,
"edges" => [
%{
"node" => %{
"id" => ^base_image_id,
"version" => "2.0.0"
}
}
]
}
} = extract_result!(result)
end
end
Expand All @@ -94,8 +99,13 @@ defmodule EdgehogWeb.Schema.Query.BaseImageCollectionTest do
name
handle
baseImages {
id
version
count
edges {
node {
id
version
}
}
}
systemModel {
id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ defmodule EdgehogWeb.Schema.Query.BaseImageCollectionsTest do

describe "baseImageCollections field" do
test "returns empty base image collections", %{tenant: tenant} do
assert %{data: %{"baseImageCollections" => []}} ==
base_image_collections_query(tenant: tenant)
assert [] = [tenant: tenant] |> base_image_collections_query() |> extract_result!()
end

test "returns base image collections if they're present", %{tenant: tenant} do
Expand All @@ -39,8 +38,8 @@ defmodule EdgehogWeb.Schema.Query.BaseImageCollectionsTest do
system_model_id: system_model.id
)

assert %{data: %{"baseImageCollections" => [base_image_collection]}} =
base_image_collections_query(tenant: tenant)
assert [base_image_collection] =
[tenant: tenant] |> base_image_collections_query() |> extract_result!()

assert base_image_collection["name"] == fixture.name
assert base_image_collection["handle"] == fixture.handle
Expand All @@ -55,10 +54,15 @@ defmodule EdgehogWeb.Schema.Query.BaseImageCollectionsTest do
"""
query BaseImageCollections($filter: BaseImageCollectionFilterInput, $sort: [BaseImageCollectionSortInput]) {
baseImageCollections(filter: $filter, sort: $sort) {
name
handle
systemModel {
id
count
edges {
node {
name
handle
systemModel {
id
}
}
}
}
}
Expand All @@ -75,4 +79,15 @@ defmodule EdgehogWeb.Schema.Query.BaseImageCollectionsTest do

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

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

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

assert length(base_image_collections) == count

base_image_collections
end
end

0 comments on commit 4c68371

Please sign in to comment.