Skip to content

Commit

Permalink
Test subscriptions on different fields with the same topic
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Oct 7, 2024
1 parent eff5c91 commit 879096d
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions spec/graphql/subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def read_subscription(subscription_id)
validate_update: query.context[:validate_update],
other_int: query.context[:other_int],
hidden_event: query.context[:hidden_event],
shared_stream: query.context[:shared_stream],
},
transport: :socket,
}
Expand Down Expand Up @@ -168,6 +169,19 @@ def self.topic_for(arguments:, field:, scope:)
end
end

class SharedEvent < GraphQL::Schema::Subscription
subscription_scope :shared_stream

field :ok, Boolean

def self.topic_for(arguments:, field:, scope:)
scope.to_s
end
end

class OtherSharedEvent < SharedEvent
end

class Subscription < GraphQL::Schema::Object
field :payload, Payload, null: false do
argument :id, ID
Expand Down Expand Up @@ -198,6 +212,9 @@ def visible?(context)
!!context[:hidden_event]
end
end

field :shared_event, subscription: SharedEvent
field :other_shared_event, subscription: OtherSharedEvent
end

class Query < GraphQL::Schema::Object
Expand Down Expand Up @@ -283,12 +300,11 @@ def to_param
end

describe GraphQL::Subscriptions do
before do
schema.subscriptions.reset
end

[ClassBasedInMemoryBackend, FromDefinitionInMemoryBackend].each do |in_memory_backend_class|
describe "using #{in_memory_backend_class}" do
before do
schema.subscriptions.reset
end
let(:root_object) {
OpenStruct.new(
payload: in_memory_backend_class::SubscriptionPayload.new,
Expand Down Expand Up @@ -841,6 +857,26 @@ def str
end
end

it "can share topics" do
schema = ClassBasedInMemoryBackend::Schema
schema.subscriptions.reset
schema.execute("subscription { sharedEvent { ok } }", context: { shared_stream: "stream-1", socket: "1" } )
schema.execute("subscription { otherSharedEvent { ok __typename } }", context: { shared_stream: "stream-1", socket: "2" } )

schema.subscriptions.trigger(:shared_event, {}, OpenStruct.new(ok: true), scope: "stream-1")
schema.subscriptions.trigger(:other_shared_event, {}, OpenStruct.new(ok: false), scope: "stream-1")

pushed_results = schema.subscriptions.deliveries.map do |socket, results|
[socket, results.map { |r| r["data"] }]
end

expected_results = [
["1", [{ "sharedEvent" => { "ok" => true } }, { "sharedEvent" => { "ok" => false } }]],
["2", [{ "otherSharedEvent" => {"ok" => true, "__typename" => "OtherSharedEventPayload" } }, { "otherSharedEvent" => { "ok" => false, "__typename" => "OtherSharedEventPayload" } }]]
]
assert_equal expected_results, pushed_results
end

describe "broadcast: true" do
let(:schema) { BroadcastTrueSchema }

Expand Down

0 comments on commit 879096d

Please sign in to comment.