Skip to content

Commit 9d3b532

Browse files
authored
fix: fix :none relationship pagination strategy, and improve tests for it (#337)
1 parent 5554229 commit 9d3b532

File tree

4 files changed

+71
-29
lines changed

4 files changed

+71
-29
lines changed

lib/graphql/resolver.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2287,7 +2287,7 @@ defmodule AshGraphql.Graphql.Resolver do
22872287
nested = Enum.map(Enum.reverse([selection | path]), & &1.name)
22882288

22892289
related_query =
2290-
if pagination_strategy do
2290+
if pagination_strategy && pagination_strategy != :none do
22912291
case page_opts(
22922292
resolution,
22932293
relationship.destination,

lib/resource/resource.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,10 @@ defmodule AshGraphql.Resource do
15631563

15641564
defp pagination_strategy(strategy, action, allow_relay? \\ false)
15651565

1566+
defp pagination_strategy(:none, _action, _allow_relay?) do
1567+
:none
1568+
end
1569+
15661570
defp pagination_strategy(_strategy, %{pagination: pagination}, _allow_relay?)
15671571
when pagination in [nil, false] do
15681572
nil
@@ -4495,7 +4499,8 @@ defmodule AshGraphql.Resource do
44954499
end)
44964500
end
44974501

4498-
defp related_list_type(nil, type, resource, relationship) do
4502+
defp related_list_type(value, type, resource, relationship)
4503+
when is_nil(value) or value == :none do
44994504
inner_type = %Absinthe.Blueprint.TypeReference.List{
45004505
of_type: %Absinthe.Blueprint.TypeReference.NonNull{
45014506
of_type: type

test/relationship_pagination_test.exs

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,34 +223,69 @@ defmodule AshGraphql.RelationshipPaginationTest do
223223
assert [%{"name" => "Award 2"} | _] = results
224224
end
225225

226-
test "works with :none strategy" do
227-
%{id: post_id} =
228-
AshGraphql.Test.Post
229-
|> Ash.Changeset.for_create(:create, text: "Post", published: true, score: 9.8)
230-
|> Ash.create!()
226+
describe ":none strategy" do
227+
setup do
228+
%{id: post_id} =
229+
AshGraphql.Test.Post
230+
|> Ash.Changeset.for_create(:create, text: "Post", published: true, score: 9.8)
231+
|> Ash.create!()
231232

232-
document =
233-
"""
234-
query GetPost($id: ID!) {
235-
getPostWithNoCommentPagination(id: $id) {
236-
id
237-
comments {
233+
[post_id: post_id]
234+
end
235+
236+
test "works with :none strategy", %{post_id: post_id} do
237+
document =
238+
"""
239+
query GetPost($id: ID!) {
240+
getPost(id: $id) {
238241
id
242+
unpaginatedComments {
243+
id
244+
}
239245
}
240246
}
241-
}
242-
"""
247+
"""
243248

244-
assert {:ok,
245-
%{
246-
data: %{
247-
"getPostWithNoCommentPagination" => %{
248-
"comments" => [],
249-
"id" => ^post_id
249+
assert {:ok,
250+
%{
251+
data: %{
252+
"getPost" => %{
253+
"unpaginatedComments" => [],
254+
"id" => ^post_id
255+
}
250256
}
251-
}
252-
}} =
253-
Absinthe.run(document, AshGraphql.Test.Schema, variables: %{"id" => post_id})
257+
}} =
258+
Absinthe.run(document, AshGraphql.Test.Schema, variables: %{"id" => post_id})
259+
end
260+
261+
test "can't supply limit/offset with :none strategy", %{post_id: post_id} do
262+
document =
263+
"""
264+
query GetPost($id: ID!) {
265+
getPost(id: $id) {
266+
id
267+
unpaginatedComments(limit: 1, offset: 0) {
268+
id
269+
}
270+
}
271+
}
272+
"""
273+
274+
assert {:ok,
275+
%{
276+
errors: [
277+
%{
278+
message:
279+
"Unknown argument \"limit\" on field \"unpaginatedComments\" of type \"Post\"."
280+
},
281+
%{
282+
message:
283+
"Unknown argument \"offset\" on field \"unpaginatedComments\" of type \"Post\"."
284+
}
285+
]
286+
}} =
287+
Absinthe.run(document, AshGraphql.Test.Schema, variables: %{"id" => post_id})
288+
end
254289
end
255290

256291
describe "works when nested" do

test/support/resources/post.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,10 @@ defmodule AshGraphql.Test.Post do
193193
field_names text_1_and_2: :text1_and2
194194
keyset_field :keyset
195195

196+
paginate_relationship_with unpaginated_comments: :none
197+
196198
queries do
197199
get :get_post, :read
198-
199-
get :get_post_with_no_comment_pagination, :read do
200-
paginate_relationship_with(comments: :none)
201-
end
202-
203200
get :get_post_with_custom_description, :read, description: "A custom description"
204201
list :post_library, :library
205202
list :paginated_posts, :paginated
@@ -657,6 +654,11 @@ defmodule AshGraphql.Test.Post do
657654
has_many(:sponsored_comments, AshGraphql.Test.SponsoredComment, public?: true)
658655
has_many(:paginated_comments, AshGraphql.Test.Comment, read_action: :paginated, public?: true)
659656

657+
has_many(:unpaginated_comments, AshGraphql.Test.Comment,
658+
public?: true,
659+
destination_attribute: :post_id
660+
)
661+
660662
many_to_many(:tags, AshGraphql.Test.Tag,
661663
through: AshGraphql.Test.PostTag,
662664
source_attribute_on_join_resource: :post_id,

0 commit comments

Comments
 (0)