Skip to content

Commit

Permalink
Merge pull request #56 from Shopify/fix-mutation-execution-test
Browse files Browse the repository at this point in the history
Prevent loader re-use after mutations
  • Loading branch information
dylanahsmith authored Jun 8, 2017
2 parents 3da0a0e + bdc21a0 commit bbe92c3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
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

0 comments on commit bbe92c3

Please sign in to comment.