|
1 | 1 | require 'rails_helper' |
2 | 2 |
|
3 | 3 | RSpec.describe 'FragmentCache + Dataloader N+1 Investigation', type: :request do |
| 4 | + let(:users) { 5.times.map { |i| User.create!(name: "User #{i}") } } |
| 5 | + |
4 | 6 | before do |
5 | | - user1 = User.create!(name: "Alice") |
6 | | - user2 = User.create!(name: "Bob") |
7 | | - 5.times do |i| |
8 | | - user1.posts.create!(title: "Alice Post #{i}") |
9 | | - user2.posts.create!(title: "Bob Post #{i}") |
| 7 | + users.each do |user| |
| 8 | + 2.times do |i| |
| 9 | + user.posts.create!(title: "Post #{i}") |
| 10 | + end |
10 | 11 | end |
11 | 12 | end |
12 | 13 |
|
13 | 14 | let(:query) do |
14 | 15 | <<~GRAPHQL |
15 | | - query($useCache: Boolean!) { |
| 16 | + query($useCache: Boolean!, $useDataloader: Boolean!) { |
16 | 17 | users { |
17 | 18 | id |
18 | 19 | name |
19 | | - posts(useCache: $useCache) { |
| 20 | + posts(useCache: $useCache, useDataloader: $useDataloader) { |
20 | 21 | id |
21 | 22 | title |
22 | 23 | } |
|
25 | 26 | GRAPHQL |
26 | 27 | end |
27 | 28 |
|
28 | | - describe 'N+1 for database' do |
| 29 | + describe.skip 'N+1 for database' do |
29 | 30 | context 'when cache_fragment is disabled' do |
30 | 31 | it 'should have minimal query count with effective batching' do |
31 | 32 | queries = [] |
|
40 | 41 | ActiveSupport::Notifications.subscribed(counter, "sql.active_record") do |
41 | 42 | post '/graphql', params: { |
42 | 43 | query: query, |
43 | | - variables: { useCache: false }.to_json |
| 44 | + variables: { useCache: false, useDataloader: false }.to_json |
44 | 45 | } |
45 | 46 | end |
46 | 47 |
|
|
74 | 75 | ActiveSupport::Notifications.subscribed(counter, "sql.active_record") do |
75 | 76 | post '/graphql', params: { |
76 | 77 | query: query, |
77 | | - variables: { useCache: true }.to_json |
| 78 | + variables: { useCache: true, useDataloader: true }.to_json |
78 | 79 | } |
79 | 80 | end |
80 | 81 |
|
|
92 | 93 | end |
93 | 94 | end |
94 | 95 | end |
| 96 | + |
| 97 | + describe 'N+1 for cache' do |
| 98 | + class LoggingMemoryStore < ActiveSupport::Cache::MemoryStore |
| 99 | + attr_reader :read_multi_calls, :exist_calls |
| 100 | + |
| 101 | + def initialize |
| 102 | + super |
| 103 | + @read_multi_calls = [] |
| 104 | + @exist_calls = [] |
| 105 | + end |
| 106 | + |
| 107 | + def read_multi(*names) |
| 108 | + @read_multi_calls << names |
| 109 | + super |
| 110 | + end |
| 111 | + |
| 112 | + def exist?(name, options = nil) |
| 113 | + @exist_calls << name |
| 114 | + super |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + let(:cache_store) { LoggingMemoryStore.new } |
| 119 | + |
| 120 | + before do |
| 121 | + GraphQL::FragmentCache.cache_store = cache_store |
| 122 | + end |
| 123 | + |
| 124 | + context 'without dataloader option' do |
| 125 | + it 'calls 1 read_multi' do |
| 126 | + post '/graphql', params: { |
| 127 | + query: query, |
| 128 | + variables: { useCache: true, useDataloader: false }.to_json |
| 129 | + } |
| 130 | + |
| 131 | + expect(response).to have_http_status(:success) |
| 132 | + json_response = JSON.parse(response.body) |
| 133 | + expect(json_response["errors"]).to be_nil |
| 134 | + expect(json_response["data"]["users"]).to be_present |
| 135 | + |
| 136 | + expect(cache_store.read_multi_calls.size).to be 1 |
| 137 | + expect(cache_store.exist_calls.size).to be 0 |
| 138 | + |
| 139 | + puts "\n=== Cache operations without dataloader ===" |
| 140 | + puts "read_multi calls:" |
| 141 | + cache_store.read_multi_calls.each.with_index(1) do |names, i| |
| 142 | + puts " #{i}. #{names.join(', ')}" |
| 143 | + end |
| 144 | + puts "exist? calls:" |
| 145 | + cache_store.exist_calls.each.with_index(1) do |name, i| |
| 146 | + puts " #{i}. #{name}" |
| 147 | + end |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + context 'with dataloader option' do |
| 152 | + it 'calls 1 read_multi and N exist?' do |
| 153 | + post '/graphql', params: { |
| 154 | + query: query, |
| 155 | + variables: { useCache: true, useDataloader: true }.to_json |
| 156 | + } |
| 157 | + |
| 158 | + expect(response).to have_http_status(:success) |
| 159 | + json_response = JSON.parse(response.body) |
| 160 | + expect(json_response["errors"]).to be_nil |
| 161 | + expect(json_response["data"]["users"]).to be_present |
| 162 | + |
| 163 | + expect(cache_store.read_multi_calls.size).to be 1 |
| 164 | + expect(cache_store.exist_calls.size).to be users.size |
| 165 | + |
| 166 | + puts "\n=== Cache operations with dataloader ===" |
| 167 | + puts "read_multi calls:" |
| 168 | + cache_store.read_multi_calls.each.with_index(1) do |names, i| |
| 169 | + puts " #{i}. #{names.join(', ')}" |
| 170 | + end |
| 171 | + puts "exist? calls:" |
| 172 | + cache_store.exist_calls.each.with_index(1) do |name, i| |
| 173 | + puts " #{i}. #{name}" |
| 174 | + end |
| 175 | + end |
| 176 | + end |
| 177 | + end |
95 | 178 | end |
0 commit comments