Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix triggering with custom enum value, fixes #4713 #4716

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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