Skip to content

Commit

Permalink
Merge pull request #4716 from rmosolgo/fix-subscription-trigger-enums
Browse files Browse the repository at this point in the history
Fix triggering with custom enum value, fixes #4713
  • Loading branch information
rmosolgo authored Nov 30, 2023
2 parents 8300420 + 1ba5e3d commit 7265bd6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/graphql/subscriptions/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
67 changes: 67 additions & 0 deletions spec/graphql/subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7265bd6

Please sign in to comment.