Skip to content

Commit 182e360

Browse files
committed
Confirm that N+1 issue is resolved with dataloader: true option
1 parent df49d64 commit 182e360

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

app/graphql/types/user_type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class UserType < Types::BaseObject
1010
end
1111
def posts(use_cache:)
1212
if use_cache
13-
cache_fragment(expires_in: 1.minute) do
13+
cache_fragment(expires_in: 1.minute, dataloader: true) do
1414
dataloader.with(Sources::PostsByUser).load(object.id)
1515
end
1616
else

spec/requests/graphql_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@
2727

2828
context 'when cache_fragment is disabled' do
2929
it 'should have minimal query count with effective batching' do
30-
queries_count = 0
3130
queries = []
3231

3332
counter = ->(_name, _started, _finished, _unique_id, payload) do
3433
sql = payload[:sql].to_s
3534
if sql.start_with?("SELECT")
36-
queries_count += 1
3735
queries << sql
3836
end
3937
end
@@ -54,21 +52,19 @@
5452
json_response = JSON.parse(response.body)
5553
expect(json_response["errors"]).to be_nil
5654
expect(json_response["data"]["users"]).to be_present
57-
expect(queries_count).to be 2
55+
expect(queries.size).to be 2
5856
end
5957
end
6058

6159
context 'when cache_fragment is enabled' do
62-
it 'should show increased query count due to N+1' do
60+
it 'should have minimal query count with effective batching' do
6361
GraphQL::FragmentCache.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
6462

65-
queries_count = 0
6663
queries = []
6764

6865
counter = ->(_name, _started, _finished, _unique_id, payload) do
6966
sql = payload[:sql].to_s
7067
if sql.start_with?("SELECT")
71-
queries_count += 1
7268
queries << sql
7369
end
7470
end
@@ -91,7 +87,7 @@
9187
json_response = JSON.parse(response.body)
9288
expect(json_response["errors"]).to be_nil
9389
expect(json_response["data"]["users"]).to be_present
94-
expect(queries_count).to be 3
90+
expect(queries.size).to be 2
9591
end
9692
end
9793
end

0 commit comments

Comments
 (0)