|
25 | 25 | GRAPHQL |
26 | 26 | end |
27 | 27 |
|
28 | | - context 'when cache_fragment is disabled' do |
29 | | - it 'should have minimal query count with effective batching' do |
30 | | - queries = [] |
| 28 | + describe 'N+1 for database' do |
| 29 | + context 'when cache_fragment is disabled' do |
| 30 | + it 'should have minimal query count with effective batching' do |
| 31 | + queries = [] |
31 | 32 |
|
32 | | - counter = ->(_name, _started, _finished, _unique_id, payload) do |
33 | | - sql = payload[:sql].to_s |
34 | | - if sql.start_with?("SELECT") |
35 | | - queries << sql |
| 33 | + counter = ->(_name, _started, _finished, _unique_id, payload) do |
| 34 | + sql = payload[:sql].to_s |
| 35 | + if sql.start_with?("SELECT") |
| 36 | + queries << sql |
| 37 | + end |
36 | 38 | end |
37 | | - end |
38 | 39 |
|
39 | | - ActiveSupport::Notifications.subscribed(counter, "sql.active_record") do |
40 | | - post '/graphql', params: { |
41 | | - query: query, |
42 | | - variables: { useCache: false }.to_json |
43 | | - } |
44 | | - end |
| 40 | + ActiveSupport::Notifications.subscribed(counter, "sql.active_record") do |
| 41 | + post '/graphql', params: { |
| 42 | + query: query, |
| 43 | + variables: { useCache: false }.to_json |
| 44 | + } |
| 45 | + end |
45 | 46 |
|
46 | | - puts "\n=== Queries without cache ===" |
47 | | - queries.each.with_index(1) do |sql, i| |
48 | | - puts "\n#{i}. #{sql}" |
49 | | - end |
| 47 | + puts "\n=== Queries without cache ===" |
| 48 | + queries.each.with_index(1) do |sql, i| |
| 49 | + puts "\n#{i}. #{sql}" |
| 50 | + end |
50 | 51 |
|
51 | | - expect(response).to have_http_status(:success) |
52 | | - json_response = JSON.parse(response.body) |
53 | | - expect(json_response["errors"]).to be_nil |
54 | | - expect(json_response["data"]["users"]).to be_present |
55 | | - expect(queries.size).to be 2 |
| 52 | + expect(response).to have_http_status(:success) |
| 53 | + json_response = JSON.parse(response.body) |
| 54 | + expect(json_response["errors"]).to be_nil |
| 55 | + expect(json_response["data"]["users"]).to be_present |
| 56 | + expect(queries.size).to be 2 |
| 57 | + end |
56 | 58 | end |
57 | | - end |
58 | 59 |
|
59 | | - context 'when cache_fragment is enabled' do |
60 | | - it 'should have minimal query count with effective batching' do |
61 | | - GraphQL::FragmentCache.cache_store = ActiveSupport::Cache.lookup_store(:memory_store) |
| 60 | + context 'when cache_fragment is enabled' do |
| 61 | + it 'should have minimal query count with effective batching' do |
| 62 | + GraphQL::FragmentCache.cache_store = ActiveSupport::Cache.lookup_store(:memory_store) |
62 | 63 |
|
63 | | - queries = [] |
| 64 | + queries = [] |
64 | 65 |
|
65 | | - counter = ->(_name, _started, _finished, _unique_id, payload) do |
66 | | - sql = payload[:sql].to_s |
67 | | - if sql.start_with?("SELECT") |
68 | | - queries << sql |
| 66 | + counter = ->(_name, _started, _finished, _unique_id, payload) do |
| 67 | + sql = payload[:sql].to_s |
| 68 | + if sql.start_with?("SELECT") |
| 69 | + queries << sql |
| 70 | + end |
69 | 71 | end |
70 | | - end |
71 | 72 |
|
72 | 73 |
|
73 | | - ActiveSupport::Notifications.subscribed(counter, "sql.active_record") do |
74 | | - post '/graphql', params: { |
75 | | - query: query, |
76 | | - variables: { useCache: true }.to_json |
77 | | - } |
78 | | - end |
| 74 | + ActiveSupport::Notifications.subscribed(counter, "sql.active_record") do |
| 75 | + post '/graphql', params: { |
| 76 | + query: query, |
| 77 | + variables: { useCache: true }.to_json |
| 78 | + } |
| 79 | + end |
79 | 80 |
|
80 | | - puts "\n=== Queries with cache ===" |
81 | | - queries.each.with_index(1) do |sql, i| |
82 | | - puts "\n#{i}. #{sql}" |
83 | | - end |
| 81 | + puts "\n=== Queries with cache ===" |
| 82 | + queries.each.with_index(1) do |sql, i| |
| 83 | + puts "\n#{i}. #{sql}" |
| 84 | + end |
84 | 85 |
|
85 | 86 |
|
86 | | - expect(response).to have_http_status(:success) |
87 | | - json_response = JSON.parse(response.body) |
88 | | - expect(json_response["errors"]).to be_nil |
89 | | - expect(json_response["data"]["users"]).to be_present |
90 | | - expect(queries.size).to be 2 |
| 87 | + expect(response).to have_http_status(:success) |
| 88 | + json_response = JSON.parse(response.body) |
| 89 | + expect(json_response["errors"]).to be_nil |
| 90 | + expect(json_response["data"]["users"]).to be_present |
| 91 | + expect(queries.size).to be 2 |
| 92 | + end |
91 | 93 | end |
92 | 94 | end |
93 | 95 | end |
0 commit comments