Using Custom Context method for Subscription scoping
Context
Currently, Subscription scoping only supports scoping from key on the context hash data that was passed in the Schema#execute original, not methods on a custom context that you may have defined. SubscriptionRegistrationResolve is passed an instance of FieldResolutionContext. The SubscriptionRegistrationResolve call method creates a new instance of Subscriptions::Event, passing in an instance of the FieldResolutionContext. The event class using that context to setup the subscription scope, and is just using [] to access values, which is delegated to the main level context, which would be an instance of the context you are using for your GraphQL API.
Example
class CustomContext < GraphQL::Query::Context
def current_user_id
self[:current_user].id
end
end
class SubscriptionType < GraphQL::Schema::Object
field :published_posts, Types::Post, null: false,
description: "Return published posts scoped by author",
subscription_scope: :current_user_id
def published_posts()
end
end
# GraphQL Controller...
current_user = User.find(1)
MySchema.execute(
params[:query],
operation_name: params[:operationName],
variables: ensure_hash(params[:variables]),
context: { current_user: current_user }
)
Result
The result of this is when you set up the registration call for this mutation you end up with a subscription event with a topic that does not account for the subscription_scope. In the above example we end up with a topic that matches :published_posts: instead of 1:published_posts:. What I would have expected is the subscription_scope would have used the method defined on the custom context.
Proposed Solution
Right now the FieldResolutionContext only delegate some methods to the original schema level context, and the main context is hidden in a private instance variable of the class. I propose that we set attr_reader on FieldResolutionContext that allows you to access the main level context. That way we can use it in the event to see if the subscription_scope matches a defined method on your custom context, if not fall back too using [].
Let me know your thoughts, I would be happy to help out with a PR for these changes 😄
I am using graphql (1.9.3) with [fairly new way of writing a subscription] https://github.com/rmosolgo/graphql-ruby/pull/1930
https://graphql-ruby.org/subscriptions/subscription_classes.html

And I still see subscription_scope: in SubscriptionType < GraphQL::Schema::Object is unused.
(so setting values in context variable in GraphqlController doesn't affect subscription scopings)
More specifically the channel the client will connect becomes ..
graphql-event:[here no subscription_scope even if you set it]:[your subscription method name]:[argument_name:value]
eg: graphql-event::methodName:argName:42
~~
So when you actually trigger the subscription with a proper scope name, the channel it will broadcast will be different.
Schema.subscriptions.trigger("methodName", {arg_name: 42}, scope: :custom_scope)
# this will broadcast to
# methodName:custom_scope:methodName:argName:42
# client is waiting at
# methodName::methodName:argName:42
Is subscription_scope going to be deprecated?
This problem seems to only be related to the older implement of subscriptions. Going this close the issue.