From 1ba5e3d5dfe4e7deb841511fb0ca6ce09e84e1bc Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Thu, 30 Nov 2023 11:48:57 -0500 Subject: [PATCH] Fix triggering with custom enum value, fixes #4713 --- lib/graphql/subscriptions/event.rb | 8 +++- spec/graphql/subscriptions_spec.rb | 67 ++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/lib/graphql/subscriptions/event.rb b/lib/graphql/subscriptions/event.rb index 0e01bafa25..ff79f6d0be 100644 --- a/lib/graphql/subscriptions/event.rb +++ b/lib/graphql/subscriptions/event.rb @@ -126,7 +126,13 @@ def stringify_args(arg_owner, args, context) when GraphQL::Schema::InputObject stringify_args(arg_owner, args.to_h, context) else - args + if arg_owner.is_a?(Class) && arg_owner < GraphQL::Schema::Enum + # `prepare:` may have made the value something other than + # a defined value of this enum -- use _that_ in this case. + arg_owner.coerce_isolated_input(args) || args + else + args + end end end diff --git a/spec/graphql/subscriptions_spec.rb b/spec/graphql/subscriptions_spec.rb index f253a7e5e5..03c10a6b86 100644 --- a/spec/graphql/subscriptions_spec.rb +++ b/spec/graphql/subscriptions_spec.rb @@ -1001,4 +1001,71 @@ def self.parse_error(err, context) assert res end end + + describe "Triggering with custom enum values" do + module SubscriptionEnum + class InMemorySubscriptions < GraphQL::Subscriptions + attr_reader :write_subscription_events, :execute_all_events + + def initialize(...) + super + reset + end + + def write_subscription(_query, events) + @write_subscription_events.concat(events) + end + + def execute_all(event, _object) + @execute_all_events.push(event) + end + + def reset + @write_subscription_events = [] + @execute_all_events = [] + end + end + + class MyEnumType < GraphQL::Schema::Enum + value "ONE", value: "one" + value "TWO", value: "two" + end + + class MySubscription < GraphQL::Schema::Subscription + argument :my_enum, MyEnumType, required: true + field :my_enum, MyEnumType + end + + class SubscriptionType < GraphQL::Schema::Object + field :my_subscription, resolver: MySubscription + end + + class Schema < GraphQL::Schema + subscription SubscriptionType + use InMemorySubscriptions + end + end + + let(:schema) { SubscriptionEnum::Schema } + let(:implementation) { schema.subscriptions } + let(:write_subscription_events) { implementation.write_subscription_events } + let(:execute_all_events) { implementation.execute_all_events } + + it "builds matching event names" do + query_str = <<-GRAPHQL + subscription ($myEnum: MyEnum!) { + mySubscription (myEnum: $myEnum) { + myEnum + } + } + GRAPHQL + + schema.execute(query_str, variables: { "myEnum" => "ONE" }) + + schema.subscriptions.trigger(:mySubscription, { "myEnum" => "ONE" }, nil) + + assert_equal(":mySubscription:myEnum:one", write_subscription_events[0].topic) + assert_equal(":mySubscription:myEnum:one", execute_all_events[0].topic) + end + end end