Skip to content

Commit 0d1c2fb

Browse files
committed
Add failing test, start to support args with Dataloader
1 parent 345ebb2 commit 0d1c2fb

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

lib/graphql.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def match?(pattern)
128128
require "graphql/filter"
129129
require "graphql/internal_representation"
130130
require "graphql/static_validation"
131+
require "graphql/dataloader"
131132
require "graphql/introspection"
132133

133134
require "graphql/analysis_error"
@@ -148,7 +149,6 @@ def match?(pattern)
148149
require "graphql/unauthorized_error"
149150
require "graphql/unauthorized_field_error"
150151
require "graphql/load_application_object_failed_error"
151-
require "graphql/dataloader"
152152
require "graphql/deprecation"
153153

154154
module GraphQL

lib/graphql/execution/interpreter/runtime.rb

+5
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ def evaluate_selection(path, result_name, field_ast_nodes_or_ast_node, scoped_co
198198

199199
begin
200200
kwarg_arguments = arguments(object, field_defn, ast_node)
201+
# TODO: I think the thing to do here is, for each argument:
202+
# - enqueue a job to coerce _that_ argument and add it to the result set
203+
# - then check the result set: if it's complete, then call a method to continue execution
204+
# - I guess that'd be for each argument _definition_ -- maybe the counter would
205+
# be to check that each _definition_ has finished being loaded.
201206
rescue GraphQL::ExecutionError, GraphQL::UnauthorizedError => e
202207
continue_value(next_path, e, owner_type, field_defn, return_type.non_null?, ast_node)
203208
return

lib/graphql/query/null_context.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ def visible_field?(t); true; end
99
def visible_type?(t); true; end
1010
end
1111

12-
attr_reader :schema, :query, :warden
12+
attr_reader :schema, :query, :warden, :dataloader
1313

1414
def initialize
1515
@query = nil
16+
@dataloader = GraphQL::Dataloader::NullDataloader.new
1617
@schema = GraphQL::Schema.new
1718
@warden = NullWarden.new(
1819
GraphQL::Filter.new,
@@ -36,7 +37,7 @@ def instance
3637
@instance = self.new
3738
end
3839

39-
def_delegators :instance, :query, :schema, :warden, :interpreter?
40+
def_delegators :instance, :query, :schema, :warden, :interpreter?, :dataloader
4041
end
4142
end
4243
end

spec/graphql/dataloader_spec.rb

+40
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ def common_ingredients(recipe_1_id:, recipe_2_id:)
173173
common_ids = recipe1[:ingredient_ids] & recipe2[:ingredient_ids]
174174
dataloader.with(DataObject).load_all(common_ids)
175175
end
176+
177+
field :common_ingredients_with_load, [Ingredient], null: false do
178+
argument :recipe_1_id, ID, required: true, loads: Recipe
179+
argument :recipe_2_id, ID, required: true, loads: Recipe
180+
end
181+
182+
def common_ingredients_with_load(recipe_1:, recipe_2:)
183+
common_ids = recipe_1[:ingredient_ids] & recipe_2[:ingredient_ids]
184+
dataloader.with(DataObject).load_all(common_ids)
185+
end
176186
end
177187

178188
query(Query)
@@ -394,4 +404,34 @@ def database_log
394404
]
395405
assert_equal expected_log, database_log
396406
end
407+
408+
it "loads arguments in batches" do
409+
query_str = <<-GRAPHQL
410+
{
411+
commonIngredientsWithLoad(recipe1Id: 5, recipe2Id: 6) {
412+
name
413+
}
414+
}
415+
GRAPHQL
416+
417+
res = FiberSchema.execute(query_str)
418+
expected_data = {
419+
"commonIngredientsWithLoad" => [
420+
{"name"=>"Corn"},
421+
{"name"=>"Butter"},
422+
]
423+
}
424+
assert_equal expected_data, res["data"]
425+
426+
expected_log = [
427+
[:mget, ["5", "6"]],
428+
[:mget, ["2", "3"]],
429+
]
430+
assert_equal expected_log, database_log
431+
end
432+
433+
434+
it "Works with analyzing arguments with `loads:`"
435+
436+
it "Works when `loads:` returns `request ...`"
397437
end

0 commit comments

Comments
 (0)