File tree 8 files changed +398
-347
lines changed
8 files changed +398
-347
lines changed Original file line number Diff line number Diff line change @@ -109,13 +109,19 @@ end
109
109
110
110
## Unit Testing
111
111
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.
112
+ Your loaders can be tested outside of a GraphQL query by doing the
113
+ batch loads in a block passed to GraphQL::Batch.batch. That method
114
+ will set up thread-local state to store the loaders, batch load any
115
+ promise returned from the block then clear the thread-local state
116
+ to avoid leaking state between tests.
113
117
114
118
``` ruby
115
119
def test_single_query
116
120
product = products(:snowboard )
117
- query = RecordLoader .for(Product ).load (args[" id" ]).then(& :title )
118
- assert_equal product.title, query.sync
121
+ title = GraphQL ::Batch .batch do
122
+ RecordLoader .for(Product ).load (product.id).then(& :title )
123
+ end
124
+ assert_equal product.title, title
119
125
end
120
126
```
121
127
Original file line number Diff line number Diff line change 4
4
module GraphQL
5
5
module Batch
6
6
BrokenPromiseError = ::Promise ::BrokenError
7
+ class NestedError < StandardError ; end
8
+
9
+ def self . batch
10
+ raise NestedError if GraphQL ::Batch ::Executor . current
11
+ begin
12
+ GraphQL ::Batch ::Executor . current = GraphQL ::Batch ::Executor . new
13
+ Promise . sync ( yield )
14
+ ensure
15
+ GraphQL ::Batch ::Executor . current = nil
16
+ end
17
+ end
7
18
end
8
19
end
9
20
Original file line number Diff line number Diff line change 1
1
module GraphQL ::Batch
2
2
class ExecutionStrategy < GraphQL ::Query ::SerialExecution
3
3
def execute ( _ , _ , query )
4
- deep_sync ( super )
4
+ GraphQL ::Batch . batch do
5
+ as_promise_unless_resolved ( super )
6
+ end
5
7
rescue GraphQL ::InvalidNullError => err
6
8
err . parent_error? || query . context . errors . push ( err )
7
9
nil
8
- ensure
9
- GraphQL ::Batch ::Executor . current . clear
10
10
end
11
11
12
- def deep_sync ( result )
12
+ # Needed for MutationExecutionStrategy
13
+ def deep_sync ( result ) #:nodoc:
13
14
Promise . sync ( as_promise_unless_resolved ( result ) )
14
15
end
15
16
Original file line number Diff line number Diff line change @@ -4,7 +4,11 @@ class Executor
4
4
private_constant :THREAD_KEY
5
5
6
6
def self . current
7
- Thread . current [ THREAD_KEY ] ||= new
7
+ Thread . current [ THREAD_KEY ]
8
+ end
9
+
10
+ def self . current = ( executor )
11
+ Thread . current [ THREAD_KEY ] = executor
8
12
end
9
13
10
14
attr_reader :loaders
You can’t perform that action at this time.
0 commit comments