|
106 | 106 | assert_equal ::Band.find(1), vulfpeck |
107 | 107 | end |
108 | 108 |
|
| 109 | + it_dataloads "works with collection associations" do |d| |
| 110 | + wilco = ::Band.find(4) |
| 111 | + chon = ::Band.find(3) |
| 112 | + albums_by_band = nil |
| 113 | + log = with_active_record_log(colorize: false) do |
| 114 | + albums_by_band = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :albums).load_all([wilco, chon]) |
| 115 | + end |
| 116 | + |
| 117 | + assert_equal [[6], [4, 5]], albums_by_band.map { |al| al.map(&:id) } |
| 118 | + assert_includes log, 'SELECT "albums".* FROM "albums" WHERE "albums"."band_id" IN (?, ?) [["band_id", 4], ["band_id", 3]]' |
| 119 | + |
| 120 | + albums = nil |
| 121 | + log = with_active_record_log(colorize: false) do |
| 122 | + albums = d.with(GraphQL::Dataloader::ActiveRecordSource, Album).load_all([3,4,5,6]) |
| 123 | + end |
| 124 | + |
| 125 | + assert_equal [3,4,5,6], albums.map(&:id) |
| 126 | + assert_includes log, 'WHERE "albums"."id" = ? [["id", 3]]' |
| 127 | + end |
| 128 | + |
| 129 | + it_dataloads "works with collection associations with scope" do |d| |
| 130 | + wilco = ::Band.find(4) |
| 131 | + chon = ::Band.find(3) |
| 132 | + albums_by_band = nil |
| 133 | + one_month_ago = nil |
| 134 | + log = with_active_record_log(colorize: false) do |
| 135 | + one_month_ago = 1.month.ago.end_of_day |
| 136 | + albums_by_band_1 = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :albums, Album.where("created_at >= ?", one_month_ago)).request(wilco) |
| 137 | + albums_by_band_2 = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :albums, Album.where("created_at >= ?", one_month_ago)).request(chon) |
| 138 | + albums_by_band = [albums_by_band_1.load, albums_by_band_2.load] |
| 139 | + end |
| 140 | + |
| 141 | + assert_equal [[6], [4, 5]], albums_by_band.map { |al| al.map(&:id) } |
| 142 | + expected_log = if Rails::VERSION::STRING > "8" |
| 143 | + 'SELECT "albums".* FROM "albums" WHERE (created_at >= ?) AND "albums"."band_id" IN (?, ?)' |
| 144 | + else |
| 145 | + 'SELECT "albums".* FROM "albums" WHERE (created_at >= ' + one_month_ago.utc.strftime("'%Y-%m-%d %H:%M:%S.%6N'") + ') AND "albums"."band_id" IN (?, ?)' |
| 146 | + end |
| 147 | + |
| 148 | + assert_includes log, expected_log |
| 149 | + |
| 150 | + albums = nil |
| 151 | + log = with_active_record_log(colorize: false) do |
| 152 | + albums = d.with(GraphQL::Dataloader::ActiveRecordSource, Album).load_all([3,4,5,6]) |
| 153 | + end |
| 154 | + |
| 155 | + assert_equal [3,4,5,6], albums.map(&:id) |
| 156 | + assert_includes log, 'WHERE "albums"."id" IN (?, ?, ?, ?) [["id", 3], ["id", 4], ["id", 5], ["id", 6]]' |
| 157 | + end |
| 158 | + |
109 | 159 | if Rails::VERSION::STRING > "7.1" # not supported in <7.1 |
110 | 160 | it_dataloads "loads with composite primary keys and warms the cache" do |d| |
111 | 161 | my_first_car = ::Album.find(2) |
|
0 commit comments