Skip to content

Commit 379c36b

Browse files
committed
Add graphql-batch's graphql_spec to check compatibility; use graphql-batch's API for loaders
1 parent 5811b74 commit 379c36b

File tree

5 files changed

+723
-12
lines changed

5 files changed

+723
-12
lines changed

lib/graphql/dataloader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def initialize(multiplex)
5353

5454
@loaders = Hash.new do |h, loader_cls|
5555
h[loader_cls] = Hash.new do |h2, loader_key|
56-
h2[loader_key] = loader_cls.new(@multiplex.context, loader_key)
56+
h2[loader_key] = loader_cls.new(@multiplex.context, *loader_key)
5757
end
5858
end
5959
end

lib/graphql/dataloader/loader.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
module GraphQL
44
class Dataloader
55
class Loader
6-
def self.load(context, key, value)
6+
def self.load(context, *key, value)
7+
self.for(context, *key).load(value)
8+
end
9+
10+
def self.for(context, *key_parts)
711
dl = context[:dataloader]
8-
loader = dl.loaders[self][key]
9-
loader.load(value)
12+
dl.loaders[self][key_parts]
1013
end
1114

1215
def self.load_all(context, key, values)
1316
GraphQL::Execution::Lazy.all(values.map { |value| load(context, key, value) })
1417
end
1518

16-
def initialize(context, key)
19+
def initialize(context, *key)
1720
@context = context
1821
@key = key
1922
@promises = {}
@@ -25,21 +28,26 @@ def load(value)
2528
if !@loaded_values.key?(value)
2629
sync
2730
end
31+
# TODO raise if key is missing?
2832
@loaded_values[value]
2933
end
3034
end
3135

3236
def sync
3337
# Promises might be added in the meantime, but they won't be included in this list.
3438
keys_to_load = @promises.keys - @loaded_values.keys
35-
resolved_values = perform(keys_to_load)
36-
keys_to_load.each_with_index do |k, i|
37-
resolved_value = resolved_values[i]
38-
@loaded_values[k] = resolved_value
39-
end
39+
perform(keys_to_load)
4040
nil
4141
end
4242

43+
def fulfill(key, value)
44+
@loaded_values[key] = value
45+
end
46+
47+
def fulfilled?(key)
48+
@loaded_values.key?(key)
49+
end
50+
4351
def perform(values)
4452
raise NotImplementedError, "`#{self.class}#perform` should load `values` for `@key` and return an item for each of `values`"
4553
end

lib/graphql/execution/lazy.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def self.resolve(val)
2323
# If `maybe_lazy` is a Lazy, sync it
2424
def self.sync(maybe_lazy)
2525
if maybe_lazy.is_a?(Lazy)
26-
maybe_lazy.sync
26+
maybe_lazy.value
2727
else
2828
maybe_lazy
2929
end
@@ -79,6 +79,10 @@ def self.all(lazies)
7979
}
8080
end
8181

82+
def inspect
83+
"#<#{self.class.name} #{@field || "unknown-field"} / #{@path || "unknown-path"} @resolved=#{@resolved} @value=#{@value.inspect}>"
84+
end
85+
8286
# This can be used for fields which _had no_ lazy results
8387
# @api private
8488
NullResult = Lazy.new(){}

0 commit comments

Comments
 (0)