Skip to content

Commit f9f21de

Browse files
committed
feat(LazyExecutionSpecification) add compat suite
1 parent 61a6064 commit f9f21de

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

lib/graphql/compatibility.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require "graphql/compatibility/execution_specification"
2+
require "graphql/compatibility/lazy_execution_specification"
23
require "graphql/compatibility/query_parser_specification"
34
require "graphql/compatibility/schema_parser_specification"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require "graphql/compatibility/lazy_execution_specification/lazy_schema"
2+
3+
module GraphQL
4+
module Compatibility
5+
module LazyExecutionSpecification
6+
# @param execution_strategy [<#new, #execute>] An execution strategy class
7+
# @return [Class<Minitest::Test>] A test suite for this execution strategy
8+
def self.build_suite(execution_strategy)
9+
Class.new(Minitest::Test) do
10+
class << self
11+
attr_accessor :lazy_schema
12+
end
13+
14+
self.lazy_schema = LazySchema.build(execution_strategy)
15+
16+
def test_it_resolves_lazy_values
17+
pushes = []
18+
query_str = %|
19+
{
20+
p1: push(value: 1) {
21+
value
22+
}
23+
p2: push(value: 2) {
24+
push(value: 3) {
25+
value
26+
}
27+
}
28+
p3: push(value: 4) {
29+
push(value: 5) {
30+
value
31+
}
32+
}
33+
}
34+
|
35+
res = self.class.lazy_schema.execute(query_str, context: {pushes: pushes})
36+
37+
expected_data = {
38+
"p1"=>{"value"=>1},
39+
"p2"=>{"push"=>{"value"=>3}},
40+
"p3"=>{"push"=>{"value"=>5}},
41+
}
42+
assert_equal expected_data, res["data"]
43+
44+
expected_pushes = [
45+
[1,2,4], # first level
46+
[3,5], # second level
47+
]
48+
assert_equal expected_pushes, pushes
49+
end
50+
end
51+
end
52+
end
53+
end
54+
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
module GraphQL
2+
module Compatibility
3+
module LazyExecutionSpecification
4+
module LazySchema
5+
class LazyPush
6+
attr_reader :value
7+
def initialize(ctx, value)
8+
@value = value
9+
@context = ctx
10+
pushes = @context[:lazy_pushes] ||= []
11+
pushes << @value
12+
end
13+
14+
def push
15+
if @context[:lazy_pushes].include?(@value)
16+
@context[:pushes] << @context[:lazy_pushes]
17+
@context[:lazy_pushes] = []
18+
end
19+
self
20+
end
21+
end
22+
23+
def self.build(execution_strategy)
24+
lazy_push_type = GraphQL::ObjectType.define do
25+
name "LazyPush"
26+
field :value, types.Int
27+
field :push, lazy_push_type do
28+
argument :value, types.Int
29+
resolve ->(o, a, c) {
30+
LazyPush.new(c, a[:value])
31+
}
32+
end
33+
end
34+
35+
query_type = GraphQL::ObjectType.define do
36+
name "Query"
37+
field :push, lazy_push_type do
38+
argument :value, types.Int
39+
resolve ->(o, a, c) {
40+
LazyPush.new(c, a[:value])
41+
}
42+
end
43+
end
44+
45+
GraphQL::Schema.define do
46+
query(query_type)
47+
query_execution_strategy(execution_strategy)
48+
lazy_resolve(LazyPush, :push)
49+
end
50+
end
51+
end
52+
end
53+
end
54+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require "spec_helper"
2+
3+
LazySpecSuite = GraphQL::Compatibility::LazyExecutionSpecification.build_suite(GraphQL::Execution::Execute)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require "spec_helper"
22

33
ExecuteSuite = GraphQL::Compatibility::ExecutionSpecification.build_suite(GraphQL::Execution::Execute)
4+
LazyExecuteSuite = GraphQL::Compatibility::LazyExecutionSpecification.build_suite(GraphQL::Execution::Execute)

0 commit comments

Comments
 (0)