Skip to content

Commit 874be93

Browse files
authored
Support graphql-ruby lazy resolution API (#37)
Also support the future removal of support for custom execution strategies
1 parent d1454e9 commit 874be93

File tree

7 files changed

+72
-19
lines changed

7 files changed

+72
-19
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/.bundle/
22
/.yardoc
3-
/Gemfile.lock
3+
/Gemfile*.lock
44
/_yardoc/
55
/coverage/
66
/doc/

.travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ language: ruby
22
rvm:
33
- 2.1
44
- 2.2
5-
before_install: gem install bundler -v 1.11.2
5+
gemfile:
6+
- Gemfile
7+
- Gemfile.graphql13
8+
before_install: gem install bundler -v 1.13.3

Gemfile.graphql13

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec
4+
5+
gem 'graphql', github: 'rmosolgo/graphql-ruby', branch: 'master'

lib/graphql/batch.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ def self.batch
2222
require_relative "batch/loader"
2323
require_relative "batch/executor"
2424
require_relative "batch/promise"
25-
require_relative "batch/execution_strategy"
26-
require_relative "batch/mutation_execution_strategy"
25+
require_relative "batch/setup"
26+
27+
# Allow custom execution strategies to be removed upstream
28+
if defined?(GraphQL::Query::SerialExecution)
29+
require_relative "batch/execution_strategy"
30+
require_relative "batch/mutation_execution_strategy"
31+
end

lib/graphql/batch/setup.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module GraphQL::Batch
2+
module Setup
3+
extend self
4+
5+
def before_query(query)
6+
raise NestedError if GraphQL::Batch::Executor.current
7+
GraphQL::Batch::Executor.current = GraphQL::Batch::Executor.new
8+
end
9+
10+
def after_query(query)
11+
GraphQL::Batch::Executor.current = nil
12+
end
13+
end
14+
end

test/graphql_test.rb

+19-15
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ def teardown
1212
QueryNotifier.subscriber = nil
1313
end
1414

15+
def schema
16+
::Schema
17+
end
18+
1519
def test_no_queries
1620
query_string = '{ constant }'
17-
result = Schema.execute(query_string)
21+
result = schema.execute(query_string)
1822
expected = {
1923
"data" => {
2024
"constant" => "constant value"
@@ -33,7 +37,7 @@ def test_single_query
3337
}
3438
}
3539
GRAPHQL
36-
result = Schema.execute(query_string)
40+
result = schema.execute(query_string)
3741
expected = {
3842
"data" => {
3943
"product" => {
@@ -53,7 +57,7 @@ def test_batched_find_by_id
5357
product2: product(id: "2") { id, title }
5458
}
5559
GRAPHQL
56-
result = Schema.execute(query_string)
60+
result = schema.execute(query_string)
5761
expected = {
5862
"data" => {
5963
"product1" => { "id" => "1", "title" => "Shirt" },
@@ -73,7 +77,7 @@ def test_record_missing
7377
}
7478
}
7579
GRAPHQL
76-
result = Schema.execute(query_string)
80+
result = schema.execute(query_string)
7781
expected = { "data" => { "product" => nil } }
7882
assert_equal expected, result
7983
assert_equal ["Product/123"], queries
@@ -88,7 +92,7 @@ def test_non_null_field_that_raises_on_nullable_parent
8892
}
8993
}
9094
GRAPHQL
91-
result = Schema.execute(query_string)
95+
result = schema.execute(query_string)
9296
expected = { 'data' => { 'product' => nil }, 'errors' => [{ 'message' => 'Error', 'locations' => [{ 'line' => 4, 'column' => 11 }], 'path' => ['product', 'nonNullButRaises'] }] }
9397
assert_equal expected, result
9498
end
@@ -101,13 +105,13 @@ def test_non_null_field_that_raises_on_query_root
101105
}
102106
}
103107
GRAPHQL
104-
result = Schema.execute(query_string)
108+
result = schema.execute(query_string)
105109
expected = { 'data' => nil, 'errors' => [{ 'message' => 'Error', 'locations' => [{ 'line' => 2, 'column' => 9 }], 'path' => ['nonNullButRaises'] }] }
106110
assert_equal expected, result
107111
end
108112

109113
def test_non_null_field_promise_raises
110-
result = Schema.execute('{ nonNullButPromiseRaises }')
114+
result = schema.execute('{ nonNullButPromiseRaises }')
111115
expected = { 'data' => nil, 'errors' => [{ 'message' => 'Error', 'locations' => [{ 'line' => 1, 'column' => 3 }], 'path' => ['nonNullButPromiseRaises'] }] }
112116
assert_equal expected, result
113117
end
@@ -125,7 +129,7 @@ def test_batched_association_preload
125129
}
126130
}
127131
GRAPHQL
128-
result = Schema.execute(query_string)
132+
result = schema.execute(query_string)
129133
expected = {
130134
"data" => {
131135
"products" => [
@@ -167,7 +171,7 @@ def test_query_group_with_single_query
167171
}
168172
}
169173
GRAPHQL
170-
result = Schema.execute(query_string)
174+
result = schema.execute(query_string)
171175
expected = {
172176
"data" => {
173177
"products" => [
@@ -203,7 +207,7 @@ def test_sub_queries
203207
product_variants_count(id: "2")
204208
}
205209
GRAPHQL
206-
result = Schema.execute(query_string)
210+
result = schema.execute(query_string)
207211
expected = {
208212
"data" => {
209213
"product_variants_count" => 3
@@ -221,7 +225,7 @@ def test_query_group_with_sub_queries
221225
}
222226
}
223227
GRAPHQL
224-
result = Schema.execute(query_string)
228+
result = schema.execute(query_string)
225229
expected = {
226230
"data" => {
227231
"product" => {
@@ -249,7 +253,7 @@ def test_load_list_of_objects_with_loaded_field
249253
}
250254
}
251255
GRAPHQL
252-
result = Schema.execute(query_string)
256+
result = schema.execute(query_string)
253257
expected = {
254258
"data" => {
255259
"products" => [
@@ -282,7 +286,7 @@ def test_load_error
282286
load_execution_error
283287
}
284288
GRAPHQL
285-
result = Schema.execute(query_string)
289+
result = schema.execute(query_string)
286290
expected = {
287291
"data" => { "constant"=>"constant value", "load_execution_error" => nil },
288292
"errors" => [{ "message" => "test error message", "locations"=>[{"line"=>3, "column"=>9}], "path" => ["load_execution_error"] }],
@@ -299,7 +303,7 @@ def test_mutation_execution
299303
incr2: increment_counter { value, load_value }
300304
}
301305
GRAPHQL
302-
result = Schema.execute(query_string, context: { counter: [0] })
306+
result = schema.execute(query_string, context: { counter: [0] })
303307
expected = {
304308
"data" => {
305309
"count1" => 0,
@@ -324,7 +328,7 @@ def test_mutation_batch_subselection_execution
324328
}
325329
}
326330
GRAPHQL
327-
result = Schema.execute(query_string)
331+
result = schema.execute(query_string)
328332
expected = {
329333
"data" => {
330334
"mutation1" => {

test/lazy_resolve_test.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require_relative 'graphql_test'
2+
3+
# Feature detection to only run these tests against graphql-ruby master
4+
has_lazy_resolve = nil
5+
GraphQL::Schema.define do
6+
has_lazy_resolve = respond_to?(:lazy_resolve)
7+
end
8+
9+
if has_lazy_resolve
10+
class GraphQL::LazyResolveTest < GraphQL::GraphQLTest
11+
LazyResolveSchema = GraphQL::Schema.define do
12+
query QueryType
13+
mutation MutationType
14+
lazy_resolve(Promise, :sync)
15+
instrument(:query, GraphQL::Batch::Setup)
16+
end
17+
18+
def schema
19+
LazyResolveSchema
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)