Skip to content

Commit afc292a

Browse files
Adds Context#scoped_set
1 parent 080db92 commit afc292a

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/graphql/query/context.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ def scoped_merge!(hash)
215215
@scoped_context = @scoped_context.merge(hash)
216216
end
217217

218+
def scoped_set!(key, value)
219+
scoped_merge!(key => value)
220+
end
221+
218222
class FieldResolutionContext
219223
include SharedMethods
220224
include Tracing::Traceable

spec/graphql/query/context_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,5 +506,16 @@ class ContextSchema < GraphQL::Schema
506506
assert_equal(expected_value, context.fetch(expected_key))
507507
assert_equal(expected_value, context.dig(expected_key)) if RUBY_VERSION >= '2.3.0'
508508
end
509+
510+
it "sets a value using #scoped_set!" do
511+
expected_key = :a
512+
expected_value = :test
513+
514+
context = GraphQL::Query::Context.new(query: OpenStruct.new(schema: schema), values: nil, object: nil)
515+
assert_nil(context[expected_key])
516+
517+
context.scoped_set!(expected_key, expected_value)
518+
assert_equal(expected_value, context[expected_key])
519+
end
509520
end
510521
end

0 commit comments

Comments
 (0)