Skip to content

Instantly share code, notes, and snippets.

@emwalker
Created November 16, 2017 20:29
Show Gist options
  • Save emwalker/8ea306492e3594db97622b77a04cf77a to your computer and use it in GitHub Desktop.
Save emwalker/8ea306492e3594db97622b77a04cf77a to your computer and use it in GitHub Desktop.
Allow nested queries
commit 39203ff494701c0445cec1139dbebf981c0b5a38
Author: Eric Walker <eric.walker@holacracyone.com>
Date: Thu Nov 16 15:10:08 2017 -0500
Add GraphQL::Batch::Executor.setup, .teardown.
diff --git a/lib/graphql/batch.rb b/lib/graphql/batch.rb
index 89b7359..ee70f75 100644
--- a/lib/graphql/batch.rb
+++ b/lib/graphql/batch.rb
@@ -7,15 +7,13 @@ require "promise.rb"
module GraphQL
module Batch
BrokenPromiseError = ::Promise::BrokenError
- class NestedError < StandardError; end
def self.batch
- raise NestedError if GraphQL::Batch::Executor.current
begin
- GraphQL::Batch::Executor.current = GraphQL::Batch::Executor.new
+ GraphQL::Batch::Executor.setup
Promise.sync(yield)
ensure
- GraphQL::Batch::Executor.current = nil
+ GraphQL::Batch::Executor.teardown
end
end
diff --git a/lib/graphql/batch/executor.rb b/lib/graphql/batch/executor.rb
index e9ff2ff..45db827 100644
--- a/lib/graphql/batch/executor.rb
+++ b/lib/graphql/batch/executor.rb
@@ -3,12 +3,39 @@ module GraphQL::Batch
THREAD_KEY = :"#{name}.batched_queries"
private_constant :THREAD_KEY
- def self.current
- Thread.current[THREAD_KEY]
- end
+ NESTING_LEVEL_KEY = :"#{THREAD_KEY}_nesting_level"
+ private_constant :NESTING_LEVEL_KEY
+
+ class << self
+ def setup
+ increment_level!
+ Thread.current[THREAD_KEY] ||= GraphQL::Batch::Executor.new
+ end
+
+ def teardown
+ if decrement_level! < 1
+ Thread.current[THREAD_KEY] = nil
+ end
+ end
+
+ def current
+ Thread.current[THREAD_KEY]
+ end
+
+ def current=(executor)
+ Thread.current[NESTING_LEVEL_KEY] = executor ? 1 : 0
+ Thread.current[THREAD_KEY] = executor
+ end
- def self.current=(executor)
- Thread.current[THREAD_KEY] = executor
+ def increment_level!
+ level = Thread.current[NESTING_LEVEL_KEY] || 0
+ Thread.current[NESTING_LEVEL_KEY] = level + 1
+ end
+
+ def decrement_level!
+ level = Thread.current[NESTING_LEVEL_KEY] || 1
+ Thread.current[NESTING_LEVEL_KEY] = level - 1
+ end
end
attr_reader :loaders
diff --git a/lib/graphql/batch/setup.rb b/lib/graphql/batch/setup.rb
index 6ec3f50..c70468e 100644
--- a/lib/graphql/batch/setup.rb
+++ b/lib/graphql/batch/setup.rb
@@ -2,12 +2,11 @@ module GraphQL::Batch
class Setup
class << self
def start_batching
- raise NestedError if GraphQL::Batch::Executor.current
- GraphQL::Batch::Executor.current = GraphQL::Batch::Executor.new
+ GraphQL::Batch::Executor.setup
end
def end_batching
- GraphQL::Batch::Executor.current = nil
+ GraphQL::Batch::Executor.teardown
end
def instrument_field(schema, type, field)
diff --git a/test/batch_test.rb b/test/batch_test.rb
index 06f0db9..4e75cc0 100644
--- a/test/batch_test.rb
+++ b/test/batch_test.rb
@@ -10,9 +10,8 @@ class GraphQL::BatchTest < Minitest::Test
def test_nested_batch
GraphQL::Batch.batch do
- assert_raises(GraphQL::Batch::NestedError) do
- GraphQL::Batch.batch {}
- end
+ # It does not raise
+ GraphQL::Batch.batch {}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment