graphene-subscriptions icon indicating copy to clipboard operation
graphene-subscriptions copied to clipboard

WIP: Allow users to control which groups a client is added to

Open jaydenwindle opened this issue 5 years ago • 2 comments

This PR fixes #6 by allowing users to manually control which Channel Layers groups a subscribed client is added to, and which groups subscription events are broadcast over.

The consumer instance is now passed to each subscription resolver as a the root value, and exposes a function called subscribe. When called, this function will add the currently connected client to the specified group and return an Observable of all values sent to that group.

A model mixin called SubscriptionModelMixin has been created, which uses django-lifecycle instead of Django signals to automatically trigger subscription events when model instances are created, updated, or deleted.

This PR also removes the legacy SubscriptionEvent and ModelSubscriptionEvent classes, and removes the default post_save_subscription and post_delete_subscription signal handlers, as they are no longer used.

Example:

# your_app/graphql/subscriptions.py
class MessageCreatedSubscription(graphene.ObjectType):
    message_created = graphene.Field(MessageType)
    message_updated = graphene.Field(MessageType, id=graphene.String())

    def resolve_message_created(root, info):
        return root.subscribe("messageCreated")
    
    def resolve_message_created(root, info, id):
        return root.subscribe(f"messageCreated.{id}")

# your_app/models.py
from django.db import models
from graphene_subscriptions.models import SubscriptionModelMixin

class Message(SubscriptionModelMixin, models.Model):
    # ...

This PR also fixes #8 and #2.

jaydenwindle avatar Feb 14 '20 18:02 jaydenwindle

@jaydenwindle Will this get released any soon? This will come handy in a project I am working on.

oldani avatar Apr 26 '20 21:04 oldani

Hey @oldani! Yes, I'm planning to merge this PR and release v2 as soon as I get a chance to write a v1 -> v2 migration guide for the docs, since there are several breaking changes. I've been a bit busy with other projects recently so haven't had time to finish it.

If you'd like to help with the migration guide I'd definitely accept a PR!

jaydenwindle avatar Apr 27 '20 15:04 jaydenwindle