Skip to content

Commit 00952e8

Browse files
committed
Add dataloader uses to context spec
1 parent 16fa555 commit 00952e8

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

spec/graphql/query/context_spec.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,15 @@ def value
153153
end
154154
end
155155

156-
class ContextKeySource < GraphQL::Dataloader::Source
157-
def initialize(context)
158-
@context = context
156+
class PassthroughSource < GraphQL::Dataloader::Source
157+
def fetch(keys)
158+
keys
159159
end
160+
end
160161

162+
class IntArraySource < GraphQL::Dataloader::Source
161163
def fetch(keys)
162-
keys.map { |k| @context[k] }
164+
keys.map { |k| k.times.map { |i| i } }
163165
end
164166
end
165167

@@ -193,14 +195,15 @@ def set_scoped_context(key:, value:, lazy:)
193195
}
194196
else
195197
context.scoped_merge!(key => value)
196-
self
198+
context.dataloader.with(PassthroughSource).load(self)
197199
end
198200
end
199201

200202
field :int_list, [ContextQuery], null: false
201203

202204
def int_list
203-
[1, 2, 3, 4]
205+
context.scoped_set!("int_list", "assigned")
206+
context.dataloader.with(IntArraySource).load(4)
204207
end
205208

206209
field :set_scoped_int, ContextQuery, null: false
@@ -358,17 +361,18 @@ class ContextSchema < GraphQL::Schema
358361
before: getScopedContext(key: "int")
359362
setScopedInt {
360363
inside: getScopedContext(key: "int")
364+
inside2: getScopedContext(key: "int_list")
361365
}
362366
after: getScopedContext(key: "int")
363367
}
364368
}
365369
GRAPHQL
366370

367371
expected_data = {"intList"=>
368-
[{"setScopedInt"=>{"inside"=>"1"}, "before"=>nil, "after"=>nil},
369-
{"setScopedInt"=>{"inside"=>"2"}, "before"=>nil, "after"=>nil},
370-
{"setScopedInt"=>{"inside"=>"3"}, "before"=>nil, "after"=>nil},
371-
{"setScopedInt"=>{"inside"=>"4"}, "before"=>nil, "after"=>nil}]}
372+
[{"setScopedInt"=>{"inside"=>"0", "inside2" => "assigned"}, "before"=>nil, "after"=>nil},
373+
{"setScopedInt"=>{"inside"=>"1", "inside2" => "assigned"}, "before"=>nil, "after"=>nil},
374+
{"setScopedInt"=>{"inside"=>"2", "inside2" => "assigned"}, "before"=>nil, "after"=>nil},
375+
{"setScopedInt"=>{"inside"=>"3", "inside2" => "assigned"}, "before"=>nil, "after"=>nil}]}
372376
result = ContextSchema.execute(query_str)
373377
assert_equal(expected_data, result["data"])
374378
end

0 commit comments

Comments
 (0)