Skip to content

Commit 9a1d95c

Browse files
authored
Merge pull request #3935 from rmosolgo/better-context-for-subscriptions-trigger
Build out a Query::Context instance for .trigger(...)
2 parents 1a90cec + 1f2027d commit 9a1d95c

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

lib/graphql/subscriptions.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ def initialize(schema:, broadcast: false, default_broadcastable: false, **rest)
5858
# @param scope [Symbol, String]
5959
# @param context [Hash]
6060
# @return [void]
61-
def trigger(event_name, args, object, scope: nil, context: GraphQL::Query::NullContext)
61+
def trigger(event_name, args, object, scope: nil, context: {})
62+
# Make something as context-like as possible, even though there isn't a current query:
63+
context = @schema.context_class.new(
64+
query: GraphQL::Query.new(@schema, "", validate: false),
65+
object: nil,
66+
values: context
67+
)
6268
event_name = event_name.to_s
6369

6470
# Try with the verbatim input first:

spec/graphql/schema/subscription_spec.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def in_memory_subscription_count
395395
GRAPHQL
396396
assert_equal 1, in_memory_subscription_count
397397
obj = OpenStruct.new(toot: { body: "I am a C programmer" }, user: SubscriptionFieldSchema::USERS["matz"])
398-
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj, context: {})
398+
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj)
399399

400400
mailbox = res.context[:subscription_mailbox]
401401
update_payload = mailbox.first
@@ -414,7 +414,7 @@ def in_memory_subscription_count
414414
GRAPHQL
415415

416416
assert_equal 1, in_memory_subscription_count
417-
SubscriptionFieldSchema.subscriptions.trigger(:users_joined, {}, {new_users: [{handle: "eileencodes"}, {handle: "tenderlove"}]}, context: {})
417+
SubscriptionFieldSchema.subscriptions.trigger(:users_joined, {}, {new_users: [{handle: "eileencodes"}, {handle: "tenderlove"}]})
418418

419419
update = res.context[:subscription_mailbox].first
420420
assert_equal [{"handle" => "eileencodes"}, {"handle" => "tenderlove"}], update["data"]["usersJoined"]["users"]
@@ -434,7 +434,7 @@ def in_memory_subscription_count
434434
assert_equal 2, in_memory_subscription_count
435435

436436
obj = OpenStruct.new(toot: { body: "Merry Christmas, here's a new Ruby version" }, user: SubscriptionFieldSchema::USERS["matz"])
437-
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj, context: {})
437+
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj)
438438

439439
mailbox1 = res1.context[:subscription_mailbox]
440440
mailbox2 = res2.context[:subscription_mailbox]
@@ -456,7 +456,7 @@ def in_memory_subscription_count
456456
GRAPHQL
457457
assert_equal 1, in_memory_subscription_count
458458
obj = OpenStruct.new(toot: { body: "I am a C programmer" }, user: SubscriptionFieldSchema::USERS["matz"])
459-
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj, context: {})
459+
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj)
460460

461461
# Get 1 successful update
462462
mailbox = res.context[:subscription_mailbox]
@@ -467,7 +467,7 @@ def in_memory_subscription_count
467467
# Then cause a not-found and update again
468468
matz = SubscriptionFieldSchema::USERS.delete("matz")
469469
obj = OpenStruct.new(toot: { body: "Merry Christmas, here's a new Ruby version" }, user: matz)
470-
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj, context: {})
470+
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj)
471471
# there was no subsequent update
472472
assert_equal 1, mailbox.size
473473
# The database was cleaned up
@@ -485,7 +485,7 @@ def in_memory_subscription_count
485485
assert_equal 1, in_memory_subscription_count
486486
matz = SubscriptionFieldSchema::USERS["matz"]
487487
obj = OpenStruct.new(toot: { body: "I am a C programmer" }, user: matz)
488-
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj, context: {})
488+
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj)
489489

490490
# Get 1 successful update
491491
mailbox = res.context[:subscription_mailbox]
@@ -496,7 +496,7 @@ def in_memory_subscription_count
496496
# Cause an authorized failure
497497
matz[:private] = true
498498
obj = OpenStruct.new(toot: { body: "Merry Christmas, here's a new Ruby version" }, user: matz)
499-
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj, context: {})
499+
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj)
500500
assert_equal 2, mailbox.size
501501
assert_equal ["Can't subscribe to private user ([\"tootWasTooted\"])"], mailbox.last["errors"].map { |e| e["message"] }
502502
# The subscription remains in place
@@ -516,7 +516,7 @@ def in_memory_subscription_count
516516
assert_equal 1, in_memory_subscription_count
517517
matz = SubscriptionFieldSchema::USERS["matz"]
518518
obj = OpenStruct.new(toot: { likes_count: 42 }, user: matz)
519-
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj, context: {})
519+
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj)
520520

521521
# Get 1 successful update
522522
mailbox = res.context[:subscription_mailbox]
@@ -531,7 +531,7 @@ def in_memory_subscription_count
531531
assert_equal [], SubscriptionFieldSchema::InMemorySubscriptions::EVENT_REGISTRY.keys
532532
matz = SubscriptionFieldSchema::USERS["matz"]
533533
obj = OpenStruct.new(toot: { body: "I am a C programmer" }, user: matz)
534-
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj, context: {})
534+
SubscriptionFieldSchema.subscriptions.trigger(:toot_was_tooted, {handle: "matz"}, obj)
535535
assert_equal [":tootWasTooted:user:matz"], SubscriptionFieldSchema::InMemorySubscriptions::EVENT_REGISTRY.keys
536536
end
537537
end
@@ -568,7 +568,7 @@ def in_memory_subscription_count
568568

569569
obj = OpenStruct.new(toot: { body: "Hello from matz!" }, user: SubscriptionFieldSchema::USERS["matz"])
570570
err = assert_raises GraphQL::Subscriptions::SubscriptionScopeMissingError do
571-
SubscriptionFieldSchema.subscriptions.trigger(:direct_toot_was_tooted, {}, obj, context: {})
571+
SubscriptionFieldSchema.subscriptions.trigger(:direct_toot_was_tooted, {}, obj)
572572
end
573573
assert_equal expected_message, err.message
574574
end
@@ -594,11 +594,11 @@ def in_memory_subscription_count
594594

595595
obj = OpenStruct.new(toot: { body: "Hello from matz!" }, user: SubscriptionFieldSchema::USERS["matz"])
596596

597-
SubscriptionFieldSchema.subscriptions.trigger(:direct_toot_was_tooted_with_optional_scope, {}, obj, context: {})
597+
SubscriptionFieldSchema.subscriptions.trigger(:direct_toot_was_tooted_with_optional_scope, {}, obj)
598598
assert_equal 0, res.context[:subscription_mailbox].length
599599
assert_equal 1, res2.context[:subscription_mailbox].length
600600

601-
SubscriptionFieldSchema.subscriptions.trigger(:direct_toot_was_tooted_with_optional_scope, {}, obj, scope: :me, context: {})
601+
SubscriptionFieldSchema.subscriptions.trigger(:direct_toot_was_tooted_with_optional_scope, {}, obj, scope: :me)
602602
assert_equal 1, res.context[:subscription_mailbox].length
603603
assert_equal 1, res2.context[:subscription_mailbox].length
604604
end
@@ -615,8 +615,8 @@ def in_memory_subscription_count
615615

616616
# Only the subscription with scope :me should be in the mailbox
617617
obj = OpenStruct.new(toot: { body: "Hello from matz!" }, user: SubscriptionFieldSchema::USERS["matz"])
618-
SubscriptionFieldSchema.subscriptions.trigger(:direct_toot_was_tooted, {}, obj, scope: :me, context: {})
619-
SubscriptionFieldSchema.subscriptions.trigger(:direct_toot_was_tooted, {}, obj, scope: :not_me, context: {})
618+
SubscriptionFieldSchema.subscriptions.trigger(:direct_toot_was_tooted, {}, obj, scope: :me)
619+
SubscriptionFieldSchema.subscriptions.trigger(:direct_toot_was_tooted, {}, obj, scope: :not_me)
620620
mailbox = res.context[:subscription_mailbox]
621621

622622
assert_equal 1, mailbox.length

0 commit comments

Comments
 (0)