graphql-batch 0.3.5 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b725a9f32a436a6e9ee155242dbcff2b576c6b8
4
- data.tar.gz: d2e42e370abdd6c28014ca0e10e10ae958854e28
3
+ metadata.gz: d459ffa3cad4df54bc36547ef5dcc0b6f3ecf8fd
4
+ data.tar.gz: e166361d06b235a13e34c843d682d1153124b1c4
5
5
  SHA512:
6
- metadata.gz: aee2377e4fa101ddeec55f33a679af48776262a80087c0b9e033e280c49d7a02cd2ae225b15b3f466c242c359d429ec60e03291e5d6ab0f74dcd4044eb7ecac9
7
- data.tar.gz: 200b341c757155b9fb23aa87e22cd228dc22bfca8d90c05102ddc54ef806034228472a9fc7d45f329cdc6be3ce1361096e9fa9f4985271ce2f51afc74d3042d1
6
+ metadata.gz: f00c79b9623279179dbf000d54f1133ed0b65a73757f9c469c3643d6d64cf06139017e0eb919797368eb2f10fef252f0d4b9a1285202dce37ab0c8e767c4e3c6
7
+ data.tar.gz: b3bd7dcf7ce3d4a653f5bc46dc4ea0a4a53774be63124c7a4d94211a5af8912ef44f3801576750d7408f77422cebde292ddc1b9a02b4ad4853fb3eb6fc2be907
data/.travis.yml CHANGED
@@ -2,4 +2,6 @@ language: ruby
2
2
  rvm:
3
3
  - 2.1
4
4
  - 2.2
5
+ - 2.3
6
+ - 2.4
5
7
  before_install: gem install bundler -v 1.13.3
@@ -11,10 +11,7 @@ class RecordLoader < GraphQL::Batch::Loader
11
11
  end
12
12
 
13
13
  def perform(keys)
14
- query(keys).each do |record|
15
- value = @column_type.cast(record.public_send(@column))
16
- fulfill(value, record)
17
- end
14
+ query(keys).each { |record| fulfill(record.public_send(@column), record) }
18
15
  keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
19
16
  end
20
17
 
@@ -21,7 +21,9 @@ Gem::Specification.new do |spec|
21
21
  spec.add_runtime_dependency "graphql", ">= 0.8", "< 2"
22
22
  spec.add_runtime_dependency "promise.rb", "~> 0.7.2"
23
23
 
24
- spec.add_development_dependency "byebug" if RUBY_ENGINE == 'ruby'
24
+ if RUBY_ENGINE == 'ruby' && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.2")
25
+ spec.add_development_dependency "byebug"
26
+ end
25
27
  spec.add_development_dependency "bundler", "~> 1.10"
26
28
  spec.add_development_dependency "rake", "~> 10.0"
27
29
  spec.add_development_dependency "minitest"
data/lib/graphql/batch.rb CHANGED
@@ -13,20 +13,20 @@ module GraphQL
13
13
  raise NestedError if GraphQL::Batch::Executor.current
14
14
  begin
15
15
  GraphQL::Batch::Executor.current = GraphQL::Batch::Executor.new
16
- Promise.sync(yield)
16
+ ::Promise.sync(yield)
17
17
  ensure
18
18
  GraphQL::Batch::Executor.current = nil
19
19
  end
20
20
  end
21
21
 
22
- def self.use(schema_defn)
22
+ def self.use(schema_defn, executor_class: GraphQL::Batch::Executor)
23
23
  schema = schema_defn.target
24
24
  if GraphQL::VERSION >= "1.6.0"
25
- instrumentation = GraphQL::Batch::SetupMultiplex.new(schema)
25
+ instrumentation = GraphQL::Batch::SetupMultiplex.new(schema, executor_class: executor_class)
26
26
  schema_defn.instrument(:multiplex, instrumentation)
27
27
  schema_defn.instrument(:field, instrumentation)
28
28
  else
29
- instrumentation = GraphQL::Batch::Setup.new(schema)
29
+ instrumentation = GraphQL::Batch::Setup.new(schema, executor_class: executor_class)
30
30
  schema_defn.instrument(:query, instrumentation)
31
31
  schema_defn.instrument(:field, instrumentation)
32
32
  end
@@ -13,7 +13,7 @@ module GraphQL::Batch
13
13
 
14
14
  # Needed for MutationExecutionStrategy
15
15
  def deep_sync(result) #:nodoc:
16
- Promise.sync(as_promise_unless_resolved(result))
16
+ ::Promise.sync(as_promise_unless_resolved(result))
17
17
  end
18
18
 
19
19
  private
@@ -11,8 +11,6 @@ module GraphQL::Batch
11
11
  Thread.current[THREAD_KEY] = executor
12
12
  end
13
13
 
14
- attr_reader :loaders
15
-
16
14
  # Set to true when performing a batch query, otherwise, it is false.
17
15
  #
18
16
  # Can be used to detect unbatched queries in an ActiveSupport::Notifications.subscribe block.
@@ -23,42 +21,41 @@ module GraphQL::Batch
23
21
  @loading = false
24
22
  end
25
23
 
26
- def resolve(loader)
27
- with_loading(true) { loader.resolve }
24
+ def loader(key)
25
+ @loaders[key] ||= yield.tap do |loader|
26
+ loader.executor = self
27
+ loader.loader_key = key
28
+ end
28
29
  end
29
30
 
30
- def shift
31
- @loaders.shift.last
31
+ def resolve(loader)
32
+ was_loading = @loading
33
+ @loading = true
34
+ loader.resolve
35
+ ensure
36
+ @loading = was_loading
32
37
  end
33
38
 
34
39
  def tick
35
- resolve(shift)
40
+ resolve(@loaders.shift.last)
36
41
  end
37
42
 
38
43
  def wait_all
39
- tick until loaders.empty?
44
+ tick until @loaders.empty?
40
45
  end
41
46
 
42
47
  def clear
43
- loaders.clear
48
+ @loaders.clear
44
49
  end
45
50
 
46
- def defer
47
- # Since we aren't actually deferring callbacks, we need to set #loading to false so that any queries
48
- # that happen in the callback aren't interpreted as being performed in GraphQL::Batch::Loader#perform
49
- with_loading(false) { yield }
50
- end
51
-
52
- private
53
-
54
- def with_loading(loading)
51
+ def around_promise_callbacks
52
+ # We need to set #loading to false so that any queries that happen in the promise
53
+ # callback aren't interpreted as being performed in GraphQL::Batch::Loader#perform
55
54
  was_loading = @loading
56
- begin
57
- @loading = loading
58
- yield
59
- ensure
60
- @loading = was_loading
61
- end
55
+ @loading = false
56
+ yield
57
+ ensure
58
+ @loading = was_loading
62
59
  end
63
60
  end
64
61
  end
@@ -12,10 +12,7 @@ module GraphQL::Batch
12
12
  " `GraphQL::Batch::Setup` as a query instrumenter if using with `graphql-ruby`"
13
13
  end
14
14
 
15
- executor.loaders[loader_key] ||= new(*group_args).tap do |loader|
16
- loader.loader_key = loader_key
17
- loader.executor = executor
18
- end
15
+ executor.loader(loader_key) { new(*group_args) }
19
16
  end
20
17
 
21
18
  def self.loader_key_for(*group_args)
@@ -35,7 +32,7 @@ module GraphQL::Batch
35
32
  def load(key)
36
33
  cache[cache_key(key)] ||= begin
37
34
  queue << key
38
- Promise.new.tap { |promise| promise.source = self }
35
+ ::Promise.new.tap { |promise| promise.source = self }
39
36
  end
40
37
  end
41
38
 
@@ -50,9 +47,7 @@ module GraphQL::Batch
50
47
  perform(load_keys)
51
48
  check_for_broken_promises(load_keys)
52
49
  rescue => err
53
- each_pending_promise(load_keys) do |key, promise|
54
- promise.reject(err)
55
- end
50
+ reject_pending_promises
56
51
  end
57
52
 
58
53
  # For Promise#sync
@@ -72,7 +67,15 @@ module GraphQL::Batch
72
67
 
73
68
  # Fulfill the key with provided value, for use in #perform
74
69
  def fulfill(key, value)
75
- promise_for(key).fulfill(value)
70
+ finish_resolve(key) do |promise|
71
+ promise.fulfill(value)
72
+ end
73
+ end
74
+
75
+ def reject(key, reason)
76
+ finish_resolve(key) do |promise|
77
+ promise.reject(reason)
78
+ end
76
79
  end
77
80
 
78
81
  # Returns true when the key has already been fulfilled, otherwise returns false
@@ -92,6 +95,14 @@ module GraphQL::Batch
92
95
 
93
96
  private
94
97
 
98
+ def finish_resolve(key)
99
+ promise = promise_for(key)
100
+ return yield(promise) unless executor
101
+ executor.around_promise_callbacks do
102
+ yield promise
103
+ end
104
+ end
105
+
95
106
  def cache
96
107
  @cache ||= {}
97
108
  end
@@ -104,18 +115,16 @@ module GraphQL::Batch
104
115
  cache.fetch(cache_key(load_key))
105
116
  end
106
117
 
107
- def each_pending_promise(load_keys)
118
+ def reject_pending_promises
108
119
  load_keys.each do |key|
109
- promise = promise_for(key)
110
- if promise.pending?
111
- yield key, promise
112
- end
120
+ # promise.rb ignores reject if promise isn't pending
121
+ reject(key, err)
113
122
  end
114
123
  end
115
124
 
116
125
  def check_for_broken_promises(load_keys)
117
- each_pending_promise(load_keys) do |key, promise|
118
- promise.reject(::Promise::BrokenError.new("#{self.class} didn't fulfill promise for key #{key.inspect}"))
126
+ load_keys.each do |key|
127
+ reject(key, ::Promise::BrokenError.new("#{self.class} didn't fulfill promise for key #{key.inspect}"))
119
128
  end
120
129
  end
121
130
  end
@@ -12,7 +12,7 @@ module GraphQL::Batch
12
12
  GraphQL::Batch::Executor.current.clear
13
13
  begin
14
14
  strategy.enable_batching = true
15
- strategy.deep_sync(Promise.sync(super))
15
+ strategy.deep_sync(::Promise.sync(super))
16
16
  ensure
17
17
  strategy.enable_batching = false
18
18
  GraphQL::Batch::Executor.current.clear
@@ -1,8 +1,6 @@
1
1
  module GraphQL::Batch
2
- class Promise < ::Promise
3
- def defer
4
- executor = Executor.current
5
- executor ? executor.defer { super } : super
6
- end
2
+ Promise = ::Promise
3
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.3")
4
+ deprecate_constant :Promise
7
5
  end
8
6
  end
@@ -1,9 +1,9 @@
1
1
  module GraphQL::Batch
2
2
  class Setup
3
3
  class << self
4
- def start_batching
4
+ def start_batching(executor_class)
5
5
  raise NestedError if GraphQL::Batch::Executor.current
6
- GraphQL::Batch::Executor.current = GraphQL::Batch::Executor.new
6
+ GraphQL::Batch::Executor.current = executor_class.new
7
7
  end
8
8
 
9
9
  def end_batching
@@ -17,7 +17,7 @@ module GraphQL::Batch
17
17
  resolve ->(obj, args, ctx) {
18
18
  GraphQL::Batch::Executor.current.clear
19
19
  begin
20
- Promise.sync(old_resolve_proc.call(obj, args, ctx))
20
+ ::Promise.sync(old_resolve_proc.call(obj, args, ctx))
21
21
  ensure
22
22
  GraphQL::Batch::Executor.current.clear
23
23
  end
@@ -27,7 +27,7 @@ module GraphQL::Batch
27
27
 
28
28
  def before_query(query)
29
29
  warn "Deprecated graphql-batch setup `instrument(:query, GraphQL::Batch::Setup)`, replace with `use GraphQL::Batch`"
30
- start_batching
30
+ start_batching(GraphQL::Batch::Executor)
31
31
  end
32
32
 
33
33
  def after_query(query)
@@ -35,12 +35,13 @@ module GraphQL::Batch
35
35
  end
36
36
  end
37
37
 
38
- def initialize(schema)
38
+ def initialize(schema, executor_class:)
39
39
  @schema = schema
40
+ @executor_class = executor_class
40
41
  end
41
42
 
42
43
  def before_query(query)
43
- Setup.start_batching
44
+ Setup.start_batching(@executor_class)
44
45
  end
45
46
 
46
47
  def after_query(query)
@@ -1,11 +1,12 @@
1
1
  module GraphQL::Batch
2
2
  class SetupMultiplex
3
- def initialize(schema)
3
+ def initialize(schema, executor_class:)
4
4
  @schema = schema
5
+ @executor_class = executor_class
5
6
  end
6
7
 
7
8
  def before_multiplex(multiplex)
8
- Setup.start_batching
9
+ Setup.start_batching(@executor_class)
9
10
  end
10
11
 
11
12
  def after_multiplex(multiplex)
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Batch
3
- VERSION = "0.3.5"
3
+ VERSION = "0.3.7"
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.5
4
+ version: 0.3.7
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-08-28 00:00:00.000000000 Z
11
+ date: 2017-11-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
151
+ rubygems_version: 2.5.2.1
152
152
  signing_key:
153
153
  specification_version: 4
154
154
  summary: A query batching executor for the graphql gem