Skip to content

Commit 19047c9

Browse files
authored
Take advantage of improvements in promise.rb (#34)
1 parent b33555a commit 19047c9

File tree

7 files changed

+11
-28
lines changed

7 files changed

+11
-28
lines changed

README.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ end
109109

110110
## Unit Testing
111111

112-
GraphQL::Batch::Promise#sync can be used to wait for a promise to be resolved and return its result. This can be useful for debugging and unit testing loaders.
112+
Promise#sync can be used to wait for a promise to be resolved and return its result. This can be useful for debugging and unit testing loaders.
113113

114114
```ruby
115115
def test_single_query
@@ -119,19 +119,6 @@ GraphQL::Batch::Promise#sync can be used to wait for a promise to be resolved an
119119
end
120120
```
121121

122-
Use GraphQL::Batch::Promise.all instead of Promise.all to be able to call sync on the returned promise.
123-
124-
```ruby
125-
def test_batch_query
126-
products = [products(:snowboard), products(:jacket)]
127-
query1 = RecordLoader.for(Product).load(products(:snowboard).id).then(&:title)
128-
query2 = RecordLoader.for(Product).load(products(:jacket).id).then(&:title)
129-
results = GraphQL::Batch::Promise.all([query1, query2]).sync
130-
assert_equal products(:snowboard).title, results[0]
131-
assert_equal products(:jacket).title, results[1]
132-
end
133-
```
134-
135122
## Development
136123

137124
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

graphql-batch.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
1919
spec.require_paths = ["lib"]
2020

2121
spec.add_runtime_dependency "graphql", ">= 0.8", "< 2"
22-
spec.add_runtime_dependency "promise.rb", "~> 0.7.0.rc2"
22+
spec.add_runtime_dependency "promise.rb", "~> 0.7.2"
2323

2424
spec.add_development_dependency "byebug" if RUBY_ENGINE == 'ruby'
2525
spec.add_development_dependency "bundler", "~> 1.10"

lib/graphql/batch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module GraphQL
55
module Batch
6-
BrokenPromiseError = Class.new(StandardError)
6+
BrokenPromiseError = ::Promise::BrokenError
77
end
88
end
99

lib/graphql/batch/execution_strategy.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class ExecutionStrategy < GraphQL::Query::SerialExecution
33
attr_accessor :disable_batching
44

55
def execute(_, _, query)
6-
as_promise(super).sync
6+
Promise.sync(as_promise_unless_resolved(super))
77
rescue GraphQL::InvalidNullError => err
88
err.parent_error? || query.context.errors.push(err)
99
nil
@@ -13,10 +13,6 @@ def execute(_, _, query)
1313

1414
private
1515

16-
def as_promise(result)
17-
GraphQL::Batch::Promise.resolve(as_promise_unless_resolved(result))
18-
end
19-
2016
def as_promise_unless_resolved(result)
2117
all_promises = []
2218
each_promise(result) do |obj, key, promise|

lib/graphql/batch/executor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def tick
3030
def wait(promise)
3131
tick while promise.pending? && !loaders.empty?
3232
if promise.pending?
33-
promise.reject(BrokenPromiseError.new("Promise wasn't fulfilled after all queries were loaded"))
33+
promise.reject(::Promise::BrokenError.new("Promise wasn't fulfilled after all queries were loaded"))
3434
end
3535
end
3636

lib/graphql/batch/loader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def each_pending_promise(load_keys)
9191

9292
def check_for_broken_promises(load_keys)
9393
each_pending_promise(load_keys) do |key, promise|
94-
promise.reject(BrokenPromiseError.new("#{self.class} didn't fulfill promise for key #{key.inspect}"))
94+
promise.reject(::Promise::BrokenError.new("#{self.class} didn't fulfill promise for key #{key.inspect}"))
9595
end
9696
end
9797
end

test/loader_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_single_query
4444
end
4545

4646
def test_query_group
47-
group = GraphQL::Batch::Promise.all([
47+
group = Promise.all([
4848
GroupCountLoader.for('two').load(:a),
4949
GroupCountLoader.for('one').load(:a),
5050
GroupCountLoader.for('two').load(:b),
@@ -57,15 +57,15 @@ def test_query_many
5757
end
5858

5959
def test_empty_group_query
60-
assert_equal [], GraphQL::Batch::Promise.all([]).sync
60+
assert_equal [], Promise.all([]).sync
6161
end
6262

6363
def test_group_query_with_non_queries
64-
assert_equal [1, :a, 'b'], GraphQL::Batch::Promise.all([1, :a, 'b']).sync
64+
assert_equal [1, :a, 'b'], Promise.all([1, :a, 'b']).sync
6565
end
6666

6767
def test_group_query_with_some_queries
68-
group = GraphQL::Batch::Promise.all([
68+
group = Promise.all([
6969
GroupCountLoader.for("two").load(:a),
7070
'one',
7171
GroupCountLoader.for("two").load(:b),
@@ -112,7 +112,7 @@ def test_broken_promise_loader_check
112112
end
113113

114114
def test_loader_class_grouping
115-
group = GraphQL::Batch::Promise.all([
115+
group = Promise.all([
116116
EchoLoader.load(:a),
117117
IncrementLoader.load(1),
118118
])

0 commit comments

Comments
 (0)