Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent loader re-use after mutations #56

Merged
merged 3 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/graphql/batch/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def resolve #:nodoc:
# For Promise#sync
def wait #:nodoc:
if executor
executor.loaders.delete(loader_key)
executor.resolve(self)
else
resolve
Expand Down
2 changes: 1 addition & 1 deletion test/graphql_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,6 @@ def test_mutation_batch_subselection_execution
}
}
assert_equal expected, result
assert_equal ["Product/1,2", "Product/3"], queries
assert_equal ["Product/1,2", "Product/2,3"], queries
end
end
10 changes: 3 additions & 7 deletions test/support/loaders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ def perform(owners)
end

class CounterLoader < GraphQL::Batch::Loader
def initialize(hash)
@hash = hash
end

def load(key=Object.new)
super(key)
def cache_key(counter_array)
counter_array.object_id
end

def perform(keys)
keys.each { |key| fulfill(key, @hash[:counter][0]) }
keys.each { |counter_array| fulfill(counter_array, counter_array[0]) }
end
end

Expand Down
8 changes: 5 additions & 3 deletions test/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,21 @@
end

field :load_value, !types.Int do
resolve ->(_, _, ctx) { CounterLoader.for(ctx).load }
resolve ->(_, _, ctx) { CounterLoader.load(ctx[:counter]) }
end
end

MutationType = GraphQL::ObjectType.define do
name "Mutation"

field :increment_counter, !CounterType do
resolve ->(_, _, ctx) { ctx[:counter][0] += 1; CounterLoader.for(ctx).load }
resolve ->(_, _, ctx) { ctx[:counter][0] += 1; CounterLoader.load(ctx[:counter]) }
end

field :counter_loader, !types.Int do
resolve ->(_, _, ctx) { CounterLoader.for(ctx).load }
resolve ->(_, _, ctx) {
CounterLoader.load(ctx[:counter])
}
end

field :no_op, !QueryType do
Expand Down