Skip to content

Commit

Permalink
Fix bug in DataLoader::Source#load_all when sending only falsy values
Browse files Browse the repository at this point in the history
In ruby:
```rb
[nil].any? #=> false
[false].any? #=> false
```

When sending [nil] to load_all, it was making the source think that
there are no pending keys, and raised GraphQL::InvariantError.
## To finalize your edit, press ctrl+Enter.  To cancel, close the view.
  • Loading branch information
Baxxx committed Nov 21, 2024
1 parent 8a21eb1 commit cc2fbb7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/graphql/dataloader/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def load_all(values)
end
}

if pending_keys.any?
if pending_keys.size.positive?
sync(pending_keys)
end

Expand Down
40 changes: 40 additions & 0 deletions spec/graphql/dataloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ def recipe(recipe:)
recipe
end

field :recipe_by_id_using_load, Recipe do
argument :id, ID, required: false
end

def recipe_by_id_using_load(id:)
dataloader.with(DataObject).load(id)
end

field :recipes_by_id_using_load_all, [Recipe] do
argument :ids, [ID, null: true]
end

def recipes_by_id_using_load_all(ids:)
dataloader.with(DataObject).load_all(ids)
end

field :recipes_by_id, [Recipe] do
argument :ids, [ID], loads: Recipe, as: :recipes
end
Expand Down Expand Up @@ -901,6 +917,30 @@ def self.included(child_class)
assert_equal 1, context[:batched_calls_counter].count
end

it "works when passing nil into source" do
query_str = <<-GRAPHQL
query($id: ID) {
recipe: recipeByIdUsingLoad(id: $id) {
name
}
}
GRAPHQL
res = schema.execute(query_str, variables: { id: nil })
expected_data = { "recipe" => nil }
assert_equal expected_data, res["data"]

query_str = <<-GRAPHQL
query($ids: [ID]!) {
recipes: recipesByIdUsingLoadAll(ids: $ids) {
name
}
}
GRAPHQL
res = schema.execute(query_str, variables: { ids: [nil] })
expected_data = { "recipes" => nil }
assert_equal expected_data, res["data"]
end

it "Works with input objects using variables, load and request" do
query_str = <<-GRAPHQL
query($input: CommonIngredientsInput!) {
Expand Down

0 comments on commit cc2fbb7

Please sign in to comment.