-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathlazy_spec.rb
More file actions
349 lines (306 loc) · 8.83 KB
/
lazy_spec.rb
File metadata and controls
349 lines (306 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# frozen_string_literal: true
require "spec_helper"
describe GraphQL::Execution::Lazy do
include LazyHelpers
describe "resolving" do
it "calls value handlers" do
res = run_query('{ int(value: 2, plus: 1) }')
assert_equal 3, res["data"]["int"]
end
it "Works with Query.new" do
query_str = '{ int(value: 2, plus: 1) }'
query = GraphQL::Query.new(LazyHelpers::LazySchema, query_str)
res = query.result
assert_equal 3, res["data"]["int"]
end
it "can do nested lazy values" do
res = run_query %|
{
a: nestedSum(value: 3) {
value
nestedSum(value: 7) {
value
nestedSum(value: 1) {
value
nestedSum(value: -50) {
value
}
}
}
}
b: nestedSum(value: 2) {
value
nestedSum(value: 11) {
value
nestedSum(value: 2) {
value
nestedSum(value: -50) {
value
}
}
}
}
c: listSum(values: [1,2]) {
nestedSum(value: 3) {
value
}
}
}
|
expected_data = {
"a"=>{"value"=>14, "nestedSum"=>{
"value"=>46,
"nestedSum"=>{
"value"=>95,
"nestedSum"=>{"value"=>90}
}
}},
"b"=>{"value"=>14, "nestedSum"=>{
"value"=>46,
"nestedSum"=>{
"value"=>95,
"nestedSum"=>{"value"=>90}
}
}},
"c"=>[
{"nestedSum"=>{"value"=>14}},
{"nestedSum"=>{"value"=>14}}
],
}
assert_equal expected_data, res["data"]
end
[
[1, 2, LazyHelpers::MAGIC_NUMBER_WITH_LAZY_AUTHORIZED_HOOK],
[2, LazyHelpers::MAGIC_NUMBER_WITH_LAZY_AUTHORIZED_HOOK, 1],
[LazyHelpers::MAGIC_NUMBER_WITH_LAZY_AUTHORIZED_HOOK, 1, 2],
].each do |ordered_values|
it "resolves each field at one depth before proceeding to the next depth (using #{ordered_values})" do
res = run_query <<-GRAPHQL, variables: { values: ordered_values }
query($values: [Int!]!) {
listSum(values: $values) {
nestedSum(value: 3) {
value
}
}
}
GRAPHQL
# Even though magic number `44`'s `.authorized?` hook returns a lazy value,
# these fields should be resolved together and return the same value.
assert_equal 56, res["data"]["listSum"][0]["nestedSum"]["value"]
assert_equal 56, res["data"]["listSum"][1]["nestedSum"]["value"]
assert_equal 56, res["data"]["listSum"][2]["nestedSum"]["value"]
end
end
it "Handles fields that return nil" do
values = [
LazyHelpers::MAGIC_NUMBER_THAT_RETURNS_NIL,
LazyHelpers::MAGIC_NUMBER_WITH_LAZY_AUTHORIZED_HOOK,
1,
2,
]
res = run_query <<-GRAPHQL, variables: { values: values }
query($values: [Int!]!) {
listSum(values: $values) {
nullableNestedSum(value: 3) {
value
}
}
}
GRAPHQL
values = res["data"]["listSum"].map { |s| s && s["nullableNestedSum"]["value"] }
assert_equal [nil, 56, 56, 56], values
end
it "propagates nulls to the root" do
res = run_query %|
{
nestedSum(value: 1) {
value
nestedSum(value: 2) {
nestedSum(value: 13) {
value
}
}
}
}|
assert_nil(res["data"])
assert_equal 1, res["errors"].length
end
it "propagates partial nulls" do
res = run_query %|
{
nullableNestedSum(value: 1) {
value
nullableNestedSum(value: 2) {
ns: nestedSum(value: 13) {
value
}
}
}
}|
expected_data = {
"nullableNestedSum" => {
"value" => 1,
"nullableNestedSum" => nil,
}
}
assert_equal(expected_data, res["data"])
assert_equal 1, res["errors"].length
end
it "handles raised errors" do
res = run_query %|
{
a: nullableNestedSum(value: 1) { value }
b: nullableNestedSum(value: 13) { value }
c: nullableNestedSum(value: 2) { value }
}|
expected_data = {
"a" => { "value" => 3 },
"b" => nil,
"c" => { "value" => 3 },
}
assert_equal expected_data, res["data"]
expected_errors = [{
"message"=>"13 is unlucky",
"locations"=>[{"line"=>4, "column"=>9}],
"path"=>["b"],
}]
assert_equal expected_errors, res["errors"]
end
it "resolves mutation fields right away" do
res = run_query %|
{
a: nestedSum(value: 2) { value }
b: nestedSum(value: 4) { value }
c: nestedSum(value: 6) { value }
}|
assert_equal [12, 12, 12], res["data"].values.map { |d| d["value"] }
res = run_query %|
mutation {
a: nestedSum(value: 2) { value }
b: nestedSum(value: 4) { value }
c: nestedSum(value: 6) { value }
}
|
assert_equal [2, 4, 6], res["data"].values.map { |d| d["value"] }
end
end
describe "Schema#sync_lazy(object)" do
it "Passes objects to that hook at runtime" do
res = run_query <<-GRAPHQL
{
a: nullableNestedSum(value: 1001) { value }
b: nullableNestedSum(value: 1013) { value }
c: nullableNestedSum(value: 1002) { value }
}
GRAPHQL
# This odd, non-adding behavior is hacked into `#sync_lazy`
assert_equal 101, res["data"]["a"]["value"]
assert_equal 113, res["data"]["b"]["value"]
assert_equal 102, res["data"]["c"]["value"]
end
end
describe "LazyMethodMap" do
class SubWrapper < LazyHelpers::Wrapper; end
let(:map) { GraphQL::Execution::Lazy::LazyMethodMap.new }
it "finds methods for classes and subclasses" do
map.set(LazyHelpers::Wrapper, :item)
map.set(LazyHelpers::SumAll, :value)
b = LazyHelpers::Wrapper.new(1)
sub_b = LazyHelpers::Wrapper.new(2)
s = LazyHelpers::SumAll.new(3)
assert_equal(:item, map.get(b))
assert_equal(:item, map.get(sub_b))
assert_equal(:value, map.get(s))
end
end
describe "Interface.resolve_type" do
class LazyResolveTypeSchema < GraphQL::Schema
class Loader
LOG = []
DATA = {
1 => { versionable: 3 },
2 => { versionable: 4 },
3 => { foo: "foo" },
4 => { bar: "bar" },
}
def initialize(loading_key)
@loading_key = loading_key
@loading_ids = Set.new
@loaded = {}
end
def self.for(context, loading_key)
l_cache = context[:loader_cache] ||= Hash.new { |h, k| h[k] = Loader.new(k) }
l_cache[loading_key]
end
def load(id)
@loading_ids.add(id)
-> {
resolve
result = @loaded.fetch(id)
if block_given?
yield(result)
else
result
end
}
end
def resolve
if @loading_ids.any?
Loader::LOG << [@loading_key, @loading_ids.to_a]
@loading_ids.to_a.each do |id|
@loaded[id] = DATA[id]
end
@loading_ids.clear
end
end
end
module Version
include GraphQL::Schema::Interface
def self.resolve_type(obj, ctx)
Loader.for(ctx, :versionable).load(obj[:versionable]) do |versionable|
versionable[:foo] ? FooVersionable : BarVersionable
end
end
end
class FooVersionable < GraphQL::Schema::Object
implements Version
field :foo, String
end
class BarVersionable < GraphQL::Schema::Object
implements Version
field :bar, String
end
class VersionReference < GraphQL::Schema::Object
field :version, Version
def version
Loader.for(context, :version).load(object[:version])
end
end
class Query < GraphQL::Schema::Object
field :version_references, [VersionReference]
def version_references
[{ version: 1 }, { version: 2 }]
end
end
lazy_resolve(Proc, :call)
query(Query)
orphan_types FooVersionable, BarVersionable
end
it "resolves lazies efficiently" do
LazyResolveTypeSchema::Loader::LOG.clear
query_str = " {
versionReferences {
version {
... on FooVersionable { foo }
... on BarVersionable { bar }
}
}
}"
res = LazyResolveTypeSchema.execute(query_str)
pp res.to_h
expected_log = [
]
assert_equal expected_log, LazyResolveTypeSchema::Loader::LOG
end
end
end