1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
# frozen_string_literal: true
require "spec_helper"
describe GraphQL::Dataloader::ActiveRecordAssociationSource do
if testing_rails?
class VulfpeckSchema < GraphQL::Schema
class Album < GraphQL::Schema::Object
field :name, String
end
class Band < GraphQL::Schema::Object
field :albums, [Album] do
argument :genre, String, required: false
argument :reverse, Boolean, required: false, default_value: false
argument :unscoped, Boolean, required: false, default_value: false
end
def albums(genre: nil, reverse:, unscoped:)
if unscoped
scope = nil
else
scope = ::Album
if genre
scope = scope.where(band_genre: genre)
end
scope = if reverse
scope.order(name: :desc)
else
scope.order(:name)
end
end
dataload_association(:albums, scope: scope)
end
end
class Query < GraphQL::Schema::Object
field :band, Band do
argument :name, String
end
def band(name:)
::Band.find_by(name: name)
end
end
query(Query)
use GraphQL::Dataloader
end
it "works with different scopes on the same object at runtime" do
query_str = <<~GRAPHQL
{
band(name: "Vulfpeck") {
allAlbums: albums {
name
}
unscopedAlbums: albums(unscoped: true) {
name
}
reverseAlbums: albums(reverse: true) {
name
}
countryAlbums: albums(genre: "country") {
name
}
}
}
GRAPHQL
result = VulfpeckSchema.execute(query_str)
assert_equal ["Mit Peck", "My First Car"], result["data"]["band"]["allAlbums"].map { |a| a["name"] }
assert_equal ["Mit Peck", "My First Car"], result["data"]["band"]["unscopedAlbums"].map { |a| a["name"] }
assert_equal ["My First Car", "Mit Peck"], result["data"]["band"]["reverseAlbums"].map { |a| a["name"] }
assert_equal [], result["data"]["band"]["countryAlbums"]
end
it_dataloads "queries for associated records when the association isn't already loaded" do |d|
my_first_car = ::Album.find(2)
homey = ::Album.find(4)
log = with_active_record_log(colorize: false) do
vulfpeck, chon = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey])
assert_equal "Vulfpeck", vulfpeck.name
assert_equal "Chon", chon.name
end
assert_includes log, '[["id", 1], ["id", 3]]'
toms_story = ::Album.find(3)
log = with_active_record_log(colorize: false) do
vulfpeck, chon, toms_story_band = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey, toms_story])
assert_equal "Vulfpeck", vulfpeck.name
assert_equal "Chon", chon.name
assert_equal "Tom's Story", toms_story_band.name
end
assert_includes log, '[["id", 2]]'
end
it_dataloads "doesn't load records that are already cached by ActiveRecordSource" do |d|
d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load_all([1,2,3])
my_first_car = ::Album.find(2)
homey = ::Album.find(4)
toms_story = ::Album.find(3)
log = with_active_record_log(colorize: false) do
vulfpeck, chon, toms_story_band = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey, toms_story])
assert_equal "Vulfpeck", vulfpeck.name
assert_equal "Chon", chon.name
assert_equal "Tom's Story", toms_story_band.name
end
assert_equal "", log
end
it_dataloads "warms the cache for ActiveRecordSource" do |d|
my_first_car = ::Album.find(2)
homey = ::Album.find(4)
toms_story = ::Album.find(3)
d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey, toms_story])
log = with_active_record_log(colorize: false) do
d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load_all([1,2,3])
end
assert_equal "", log
end
it_dataloads "doesn't warm the cache when a scope is given" do |d|
my_first_car = ::Album.find(2)
homey = ::Album.find(4)
summerteeth = ::Album.find(6)
results = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band, ::Band.country).load_all([my_first_car, homey, summerteeth])
assert_equal [nil, nil, ::Band.find(4)], results
log = with_active_record_log(colorize: false) do
d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load_all([1,2,4])
end
assert_includes log, "SELECT \"bands\".* FROM \"bands\" WHERE \"bands\".\"id\" IN (?, ?, ?) [[\"id\", 1], [\"id\", 2], [\"id\", 4]]"
end
it_dataloads "doesn't pause when the association is already loaded" do |d|
source = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band)
assert_equal 0, source.results.size
assert_equal 0, source.pending.size
my_first_car = ::Album.find(2)
vulfpeck = my_first_car.band
vulfpeck2 = source.load(my_first_car)
assert_equal vulfpeck, vulfpeck2
assert_equal 0, source.results.size
assert_equal 0, source.pending.size
my_first_car.reload
vulfpeck3 = source.load(my_first_car)
assert_equal vulfpeck, vulfpeck3
assert_equal 1, source.results.size
assert_equal 0, source.pending.size
end
it_dataloads "raises an error with a non-existent association" do |d|
my_first_car = ::Album.find(2)
source = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :tour_bus)
assert_raises ActiveRecord::AssociationNotFoundError do
source.load(my_first_car)
end
end
it_dataloads "works with polymorphic associations" do |d|
wilco = ::Band.find(4)
vulfpeck = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :thing).load(wilco)
assert_equal ::Band.find(1), vulfpeck
end
it_dataloads "works with collection associations" do |d|
wilco = ::Band.find(4)
chon = ::Band.find(3)
albums_by_band = nil
log = with_active_record_log(colorize: false) do
albums_by_band = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :albums).load_all([wilco, chon])
end
assert_equal [[6], [4, 5]], albums_by_band.map { |al| al.map(&:id) }
assert_includes log, 'SELECT "albums".* FROM "albums" WHERE "albums"."band_id" IN (?, ?) [["band_id", 4], ["band_id", 3]]'
albums = nil
log = with_active_record_log(colorize: false) do
albums = d.with(GraphQL::Dataloader::ActiveRecordSource, Album).load_all([3,4,5,6])
end
assert_equal [3,4,5,6], albums.map(&:id)
assert_includes log, 'WHERE "albums"."id" = ? [["id", 3]]'
end
it_dataloads "works with collection associations with scope" do |d|
wilco = ::Band.find(4)
chon = ::Band.find(3)
albums_by_band = nil
one_month_ago = nil
log = with_active_record_log(colorize: false) do
one_month_ago = 1.month.ago.end_of_day
albums_by_band_1 = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :albums, Album.where("created_at >= ?", one_month_ago)).request(wilco)
albums_by_band_2 = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :albums, Album.where("created_at >= ?", one_month_ago)).request(chon)
albums_by_band_3 = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :albums, Album.where("created_at <= ?", one_month_ago)).request(wilco)
albums_by_band = [albums_by_band_1.load, albums_by_band_2.load, albums_by_band_3.load]
end
assert_equal [[6], [4, 5], []], albums_by_band.map { |al| al.map(&:id) }
expected_log = if Rails::VERSION::STRING > "8"
'SELECT "albums".* FROM "albums" WHERE (created_at >= ?) AND "albums"."band_id" IN (?, ?)'
else
'SELECT "albums".* FROM "albums" WHERE (created_at >= ' + one_month_ago.utc.strftime("'%Y-%m-%d %H:%M:%S.%6N'") + ') AND "albums"."band_id" IN (?, ?)'
end
assert_includes log, expected_log
albums = nil
log = with_active_record_log(colorize: false) do
albums = d.with(GraphQL::Dataloader::ActiveRecordSource, Album).load_all([3,4,5,6])
end
assert_equal [3,4,5,6], albums.map(&:id)
assert_includes log, 'WHERE "albums"."id" IN (?, ?, ?, ?) [["id", 3], ["id", 4], ["id", 5], ["id", 6]]'
end
if Rails::VERSION::STRING > "7.1" # not supported in <7.1
it_dataloads "loads with composite primary keys and warms the cache" do |d|
my_first_car = ::Album.find(2)
homey = ::Album.find(4)
log = with_active_record_log(colorize: false) do
vulfpeck, chon = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :composite_band).load_all([my_first_car, homey])
assert_equal "Vulfpeck", vulfpeck.name
assert_equal "Chon", chon.name
end
assert_includes log, '[["name", "Vulfpeck"], ["name", "Chon"], ["genre", 0]]'
log = with_active_record_log(colorize: false) do
d.with(GraphQL::Dataloader::ActiveRecordSource, CompositeBand).load_all([["Vulfpeck", "rock"], ["Chon", :rock]])
end
assert_equal "", log
end
end
end
end
|