diff --git a/spec/graphql/dataloader/async_dataloader_spec.rb b/spec/graphql/dataloader/async_dataloader_spec.rb index 59d8172cba..033ac012a9 100644 --- a/spec/graphql/dataloader/async_dataloader_spec.rb +++ b/spec/graphql/dataloader/async_dataloader_spec.rb @@ -389,5 +389,70 @@ class TraceAsyncSchema < AsyncSchema check_snapshot(data, if_exec_next("example-next.json", "example.json")) end end + + if testing_rails? && ISOLATION_LEVEL_FIBER + describe "with activerecord" do + class ActiveRecordAsyncSchema < GraphQL::Schema + class Author < GraphQL::Schema::Object + field :name, String + end + + class Book < GraphQL::Schema::Object + field :title, String + field :author, Author + end + + Author.field(:books, Book.connection_type) + + class Query < GraphQL::Schema::Object + field :book, Book do + argument :title, String + end + + def book(title:) + dataload_record(::Book, title, find_by: :title) + end + + field :author, Author do + argument :name, String + end + + def author(name:) + dataload_record(::Author, name, find_by: :name) + end + end + + query(Query) + use GraphQL::Dataloader::AsyncDataloader + end + + it "works with repeated queries" do + query_str = <<~GRAPHQL + { + author(name: "William Shakespeare") { name } + b1: book(title: "A Midsummer Night's Dream") { title author { name } } + b2: book(title: "Hamlet") { title author { name } } + } + GRAPHQL + + results = [] + 10.times do + stdout, stderr = capture_io do + result = ActiveRecordAsyncSchema.execute(query_str) + results << [ + result["data"]["author"]["name"], + result["data"]["b2"]["title"] + ] + end + assert_equal "", stdout, "Nothing to stdout" + assert_equal "", stderr, "Nothing to stderr (like warnings from Task errors)" + rescue + :failed + end + + assert_equal Array.new(10, ["William Shakespeare", "Hamlet"]), results + end + end + end end end diff --git a/spec/integration/rails/graphql/dataloader/async_dataloader_spec.rb b/spec/integration/rails/graphql/dataloader/async_dataloader_spec.rb index 68f4734db6..ed027c28d1 100644 --- a/spec/integration/rails/graphql/dataloader/async_dataloader_spec.rb +++ b/spec/integration/rails/graphql/dataloader/async_dataloader_spec.rb @@ -89,7 +89,7 @@ def role end before { - skip("Only test when isolation_level = :fiber") unless ENV["ISOLATION_LEVEL_FIBER"] + skip("Only test when isolation_level = :fiber") unless ISOLATION_LEVEL_FIBER } it "cleans up database connections" do diff --git a/spec/integration/rails/spec_helper.rb b/spec/integration/rails/spec_helper.rb index 722dd73226..fa13d6750b 100644 --- a/spec/integration/rails/spec_helper.rb +++ b/spec/integration/rails/spec_helper.rb @@ -10,9 +10,12 @@ require "sqlite3" end -if ENV["ISOLATION_LEVEL_FIBER"] +ISOLATION_LEVEL_FIBER = if ENV["ISOLATION_LEVEL_FIBER"] ActiveSupport::IsolatedExecutionState.isolation_level = :fiber puts "ActiveSupport::IsolatedExecutionState: #{ActiveSupport::IsolatedExecutionState.isolation_level}" + true +else + false end require_relative "generators/base_generator_test"