graphql-batch 0.3.8 → 0.3.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 235150e8e0307968c17a84dbfe6e3020ab58f0eb
4
- data.tar.gz: c85fa65eb8e23ca836fe1848e9be4e3a5c93fe94
3
+ metadata.gz: b611b5510ff666b9ed400c9066839af2a64d8695
4
+ data.tar.gz: 15aca5ee2baeffd739b1a23192e33d0c50aa31bc
5
5
  SHA512:
6
- metadata.gz: f8336eb9099406bd9172fd88c72300d343fbde12133060e3e90efe0ee0a2d5f88dd3e628eb640728dc1b006223592dfe216a9710393208883f1344a438c6461e
7
- data.tar.gz: 7241576a1d9f864524dde9ff03362f4a3fe850e198b446f0142e1d29f8c6ccb749adac6eacc5fbe7ce6d710046eb5e2c2b335ba611fdab2fd46a6fafb3f6b247
6
+ metadata.gz: bc8fa00ef926af4abbba3c22ebdc8c0a1c91d0ffb3ba2172efaa5ab5628d6fa80245710365128bf0e701340d266d94c9c00d8fa291aa455df8790cc913b14b4a
7
+ data.tar.gz: 697e6c1f7b077a8b7803ba0aab1decfb4e77f699dc96d7d4cb41424faf5d695b5289198831b1b62f5b752c0ce8636aa9482529cbfcae1c898e036527ffbeaa40
@@ -7,15 +7,14 @@ require "promise.rb"
7
7
  module GraphQL
8
8
  module Batch
9
9
  BrokenPromiseError = ::Promise::BrokenError
10
- class NestedError < StandardError; end
10
+ class NoExecutorError < StandardError; end
11
11
 
12
- def self.batch
13
- raise NestedError if GraphQL::Batch::Executor.current
12
+ def self.batch(executor_class: GraphQL::Batch::Executor)
14
13
  begin
15
- GraphQL::Batch::Executor.current = GraphQL::Batch::Executor.new
14
+ GraphQL::Batch::Executor.start_batch(executor_class)
16
15
  ::Promise.sync(yield)
17
16
  ensure
18
- GraphQL::Batch::Executor.current = nil
17
+ GraphQL::Batch::Executor.end_batch
19
18
  end
20
19
  end
21
20
 
@@ -3,12 +3,28 @@ module GraphQL::Batch
3
3
  THREAD_KEY = :"#{name}.batched_queries"
4
4
  private_constant :THREAD_KEY
5
5
 
6
- def self.current
7
- Thread.current[THREAD_KEY]
8
- end
6
+ class << self
7
+ def current
8
+ Thread.current[THREAD_KEY]
9
+ end
10
+
11
+ def current=(executor)
12
+ Thread.current[THREAD_KEY] = executor
13
+ end
14
+
15
+ def start_batch(executor_class)
16
+ executor = Thread.current[THREAD_KEY] ||= executor_class.new
17
+ executor.increment_level
18
+ end
9
19
 
10
- def self.current=(executor)
11
- Thread.current[THREAD_KEY] = executor
20
+ def end_batch
21
+ executor = current
22
+ unless executor
23
+ raise NoExecutorError, 'Cannot end a batch without an Executor.'
24
+ end
25
+ return unless executor.decrement_level < 1
26
+ self.current = nil
27
+ end
12
28
  end
13
29
 
14
30
  # Set to true when performing a batch query, otherwise, it is false.
@@ -19,6 +35,7 @@ module GraphQL::Batch
19
35
  def initialize
20
36
  @loaders = {}
21
37
  @loading = false
38
+ @nesting_level = 0
22
39
  end
23
40
 
24
41
  def loader(key)
@@ -48,6 +65,14 @@ module GraphQL::Batch
48
65
  @loaders.clear
49
66
  end
50
67
 
68
+ def increment_level
69
+ @nesting_level += 1
70
+ end
71
+
72
+ def decrement_level
73
+ @nesting_level -= 1
74
+ end
75
+
51
76
  def around_promise_callbacks
52
77
  # We need to set #loading to false so that any queries that happen in the promise
53
78
  # callback aren't interpreted as being performed in GraphQL::Batch::Loader#perform
@@ -1,15 +1,17 @@
1
1
  module GraphQL::Batch
2
2
  class Loader
3
- class NoExecutorError < StandardError; end
3
+ NoExecutorError = GraphQL::Batch::NoExecutorError
4
+ deprecate_constant :NoExecutorError if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.3")
4
5
 
5
6
  def self.for(*group_args)
6
7
  loader_key = loader_key_for(*group_args)
7
8
  executor = Executor.current
8
9
 
9
10
  unless executor
10
- raise NoExecutorError, "Cannot create loader without an Executor."\
11
- " Wrap the call to `for` with `GraphQL::Batch.batch` or use"\
12
- " `GraphQL::Batch::Setup` as a query instrumenter if using with `graphql-ruby`"
11
+ raise GraphQL::Batch::NoExecutorError, 'Cannot create loader without'\
12
+ ' an Executor. Wrap the call to `for` with `GraphQL::Batch.batch`'\
13
+ ' or use `GraphQL::Batch::Setup` as a query instrumenter if'\
14
+ ' using with `graphql-ruby`'
13
15
  end
14
16
 
15
17
  executor.loader(loader_key) { new(*group_args) }
@@ -2,12 +2,11 @@ module GraphQL::Batch
2
2
  class Setup
3
3
  class << self
4
4
  def start_batching(executor_class)
5
- raise NestedError if GraphQL::Batch::Executor.current
6
- GraphQL::Batch::Executor.current = executor_class.new
5
+ GraphQL::Batch::Executor.start_batch(executor_class)
7
6
  end
8
7
 
9
8
  def end_batching
10
- GraphQL::Batch::Executor.current = nil
9
+ GraphQL::Batch::Executor.end_batch
11
10
  end
12
11
 
13
12
  def instrument_field(schema, type, field)
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Batch
3
- VERSION = "0.3.8"
3
+ VERSION = "0.3.9"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-batch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dylan Thacker-Smith
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-10 00:00:00.000000000 Z
11
+ date: 2018-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  version: '0'
149
149
  requirements: []
150
150
  rubyforge_project:
151
- rubygems_version: 2.5.2.1
151
+ rubygems_version: 2.6.14
152
152
  signing_key:
153
153
  specification_version: 4
154
154
  summary: A query batching executor for the graphql gem