Created
November 14, 2017 19:02
-
-
Save emwalker/444f36ba88dc4ad4c11e2896df99ed44 to your computer and use it in GitHub Desktop.
Allow nested execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit ca10b17221be21f6ec71138da25059a5611e3eb9 | |
Author: Eric Walker <eric.walker@holacracyone.com> | |
Date: Tue Nov 14 13:54:08 2017 -0500 | |
Allow nested execution. | |
diff --git a/lib/graphql/batch.rb b/lib/graphql/batch.rb | |
index 89b7359..2a3118d 100644 | |
--- a/lib/graphql/batch.rb | |
+++ b/lib/graphql/batch.rb | |
@@ -10,12 +10,11 @@ module GraphQL | |
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.push GraphQL::Batch::Executor.new | |
Promise.sync(yield) | |
ensure | |
- GraphQL::Batch::Executor.current = nil | |
+ GraphQL::Batch::Executor.pop | |
end | |
end | |
diff --git a/lib/graphql/batch/executor.rb b/lib/graphql/batch/executor.rb | |
index e9ff2ff..d426130 100644 | |
--- a/lib/graphql/batch/executor.rb | |
+++ b/lib/graphql/batch/executor.rb | |
@@ -4,11 +4,23 @@ module GraphQL::Batch | |
private_constant :THREAD_KEY | |
def self.current | |
- Thread.current[THREAD_KEY] | |
+ stack.last | |
end | |
def self.current=(executor) | |
- Thread.current[THREAD_KEY] = executor | |
+ push executor | |
+ end | |
+ | |
+ def self.pop | |
+ stack.pop | |
+ end | |
+ | |
+ def self.push(executor) | |
+ stack.push executor | |
+ end | |
+ | |
+ def self.stack | |
+ Thread.current[THREAD_KEY] ||= [] | |
end | |
attr_reader :loaders | |
diff --git a/lib/graphql/batch/setup.rb b/lib/graphql/batch/setup.rb | |
index 6ec3f50..1c7e269 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.push GraphQL::Batch::Executor.new | |
end | |
def end_batching | |
- GraphQL::Batch::Executor.current = nil | |
+ GraphQL::Batch::Executor.pop | |
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