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

Pass and merge subscription's event and query ctxs #4242

Merged

Conversation

gastonmorixe
Copy link
Contributor

@gastonmorixe gastonmorixe commented Nov 8, 2022

When a subscription is triggered, there's an param to pass a context: but it's lost because it's not forwarded to the Subscription::Event.

Now when Subscription#execute_update is executed, we can now merge query's context with event's context.

This allows to pass an id of the sender of a query and compare it with the receiver of a subscription in the #update method, for example to prevent the sender of the subscription to receive its own updates by skipping it when the ids match.

Example:

module Subscriptions
  class ExampleSubscription < ::Types::Base::Subscription
    payload_type ::Types::ExampleSubscriptionPayload

    argument :model_id, ID, required: true, loads: ::Types::Models::MyModel, can_can_action: :read

    def subscribe(model:)
      super
    end

    def update(model:)
      # skip update when the sender is the same as the receiver of the update
      return :no_update if self.context[:client_id].present? && 
            self.context[:sender_client_id].present? && 
            self.context[:client_id] == self.context[:sender_client_id]

      super
    end
  end
end
MySchema.subscriptions.trigger(
   :my_update,
   # args:
   {
      model_id: "MODEL_ID"
   },
   # payload:
   {
      model: "MODEL_INSTANCE"
   },
   context: { sender_client_id: "SENDER_ID" }
)

When a subscription trigger is executed, there's an param to pass context but it's lost because it's not forwarded in the `Subscription::Event`. 
Now when `Subscription#execute_update` is executed, we can now merge query's context with event's context. 
This allows to pass an id of the sender of a query and compare it with the receiver of a subscription in the `#update` method, for example to prevent the sender of the subscription to receive it's own updates by skipping it when the ids match.

Example:

````ruby
module Subscriptions
  class ExampleSubscription < ::Types::Base::Subscription
    payload_type ::Types::Models::ExampleSubscriptionPayload

    argument :model_id, ID, required: true, loads: ::Types::Models::MyModel, can_can_action: :read

    def subscribe(model:)
      super
    end

    def update(journey:)
      # skip update when the sender is the same as the receiver of the update
      return :no_update if self.context[:client_id].present? && self.context[:sender_client_id].present? && self.context[:client_id] == self.context[:sender_client_id]

      super
    end
  end
end
````
@rmosolgo rmosolgo added this to the 2.0.16 milestone Nov 9, 2022
@rmosolgo
Copy link
Owner

rmosolgo commented Nov 9, 2022

This looks great -- thanks for the fix!

@rmosolgo rmosolgo merged commit acb31cf into rmosolgo:master Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants