|
| 1 | +# frozen_string_literal: true |
| 2 | +require "spec_helper" |
| 3 | + |
| 4 | +describe GraphQL::Dataloader do |
| 5 | + if defined?(ActiveRecord::Promise) && ENV['DATABASE'] == 'POSTGRESQL' # Rails 7.1+ |
| 6 | + class RailsPromiseSchema < GraphQL::Schema |
| 7 | + class LoadAsyncSource < GraphQL::Dataloader::Source |
| 8 | + LOG = [] |
| 9 | + def fetch(relations) |
| 10 | + relations.each do |rel| |
| 11 | + LOG << Time.now |
| 12 | + rel.load_async |
| 13 | + end |
| 14 | + dataloader.yield |
| 15 | + relations.each do |rel| |
| 16 | + rel.load |
| 17 | + LOG << Time.now |
| 18 | + end |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + class SleepSource < GraphQL::Dataloader::Source |
| 23 | + def initialize(duration) |
| 24 | + @duration = duration |
| 25 | + end |
| 26 | + |
| 27 | + def fetch(durations) |
| 28 | + # puts "[#{Time.now.to_f}] Starting #{durations}" |
| 29 | + promise = ::Food.async_find_by_sql("SELECT pg_sleep(#{durations.first})") |
| 30 | + # puts "[#{Time.now.to_f}] Yielding #{durations}" |
| 31 | + dataloader.yield |
| 32 | + # puts "[#{Time.now.to_f}] Finishing #{durations}" |
| 33 | + promise.value |
| 34 | + durations |
| 35 | + end |
| 36 | + end |
| 37 | + class Query < GraphQL::Schema::Object |
| 38 | + field :sleep, Float do |
| 39 | + argument :duration, Float |
| 40 | + end |
| 41 | + |
| 42 | + def sleep(duration:) |
| 43 | + dataloader.with(SleepSource, duration).load(duration) |
| 44 | + end |
| 45 | + |
| 46 | + field :things, Integer do |
| 47 | + argument :first, Integer |
| 48 | + end |
| 49 | + |
| 50 | + def things(first:) |
| 51 | + relation = Food |
| 52 | + .where(name: "Zucchini") |
| 53 | + .select("pg_sleep(0.3)") |
| 54 | + .limit(first) |
| 55 | + items = dataloader.with(LoadAsyncSource).load(relation) |
| 56 | + items.size |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + query(Query) |
| 61 | + use GraphQL::Dataloader |
| 62 | + end |
| 63 | + |
| 64 | + before do |
| 65 | + Food.create!(name: "Zucchini") |
| 66 | + RailsPromiseSchema::LoadAsyncSource::LOG.clear |
| 67 | + end |
| 68 | + |
| 69 | + after do |
| 70 | + Food.find_by(name: "Zucchini").destroy |
| 71 | + end |
| 72 | + |
| 73 | + it "Supports async queries" do |
| 74 | + assert ActiveRecord::Base.connection.async_enabled?, "ActiveRecord must support real async queries" |
| 75 | + end |
| 76 | + |
| 77 | + describe "using ActiveRecord::Promise for manual parallelism" do |
| 78 | + it "runs queries in parallel" do |
| 79 | + query_str = " |
| 80 | + { |
| 81 | + s1: sleep(duration: 0.1) |
| 82 | + s2: sleep(duration: 0.2) |
| 83 | + s3: sleep(duration: 0.3) |
| 84 | + }" |
| 85 | + t1 = Time.now |
| 86 | + result = RailsPromiseSchema.execute(query_str) |
| 87 | + t2 = Time.now |
| 88 | + assert_equal({ "s1" => 0.1, "s2" => 0.2, "s3" => 0.3}, result["data"]) |
| 89 | + assert_in_delta 0.3, t2 - t1, 0.05, "Sleeps were in parallel" |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + describe "using load_async for parallelism" do |
| 94 | + it "runs queries in parallel" do |
| 95 | + query_str = " |
| 96 | + { |
| 97 | + t1: things(first: 5) |
| 98 | + t2: things(first: 10) |
| 99 | + t3: things(first: 100) |
| 100 | + }" |
| 101 | + t1 = Time.now |
| 102 | + result = RailsPromiseSchema.execute(query_str) |
| 103 | + t2 = Time.now |
| 104 | + |
| 105 | + load_async_1, load_async_2, load_async_3, load_1, load_2, load_3 = RailsPromiseSchema::LoadAsyncSource::LOG |
| 106 | + assert_in_delta load_async_1, load_async_2, 0.05, "load_async happened first" |
| 107 | + assert_in_delta load_async_1, load_async_3, 0.05, "the third load_async happened right after" |
| 108 | + |
| 109 | + assert_in_delta load_async_1, load_1, 0.35, "load came 0.3s after" |
| 110 | + assert_in_delta load_1, load_2, 0.05, "the second load didn't have to wait because it was already done" |
| 111 | + assert_in_delta load_1, load_3, 0.05, "the third load didn't have to wait because it was already done" |
| 112 | + |
| 113 | + assert_equal({ "t1" => 1, "t2" => 1, "t3" => 1}, result["data"]) |
| 114 | + assert_in_delta 0.3, t2 - t1, 0.05, "Sleeps were in parallel" |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments