Skip to content

Commit c45aa91

Browse files
committed
Add failing spec for nested loader behavior
1 parent 379c36b commit c45aa91

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

spec/graphql/dataloader_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ def book(id:)
7474
def author(id:)
7575
BackendLoader.load_object(@context, id)
7676
end
77+
78+
field :books_count, Integer, null: false do
79+
argument :author_id, ID, required: true
80+
end
81+
82+
def books_count(author_id:)
83+
# Of course this could be done without a load, but I want to test nested loaders
84+
BackendLoader.load_object(@context, author_id).then do |author|
85+
BackendLoader.load_all(@context, nil, author[:book_ids]).then do |books|
86+
books.size
87+
end
88+
end
89+
end
7790
end
7891

7992
class Mutation < GraphQL::Schema::Object
@@ -191,4 +204,21 @@ def exec_query(*args)
191204
expected_log = ['MGET ["b1", "b2"]', 'MGET ["b1", "b3"]']
192205
assert_equal expected_log, log
193206
end
207+
208+
it "works with nested loaders" do
209+
query_str = <<-GRAPHQL
210+
{
211+
a1: booksCount(authorId: "a1")
212+
a2: booksCount(authorId: "a2")
213+
}
214+
GRAPHQL
215+
216+
res = exec_query(query_str)
217+
assert_equal({"data"=>{"a1"=>2, "a2"=>1}}, res)
218+
expected_log = [
219+
'MGET ["a1", "a2"]',
220+
'MGET ["b1", "b2", "b3"]',
221+
]
222+
assert_equal expected_log, log
223+
end
194224
end

0 commit comments

Comments
 (0)