jaydenwindle / graphene-subscriptions

A plug-and-play GraphQL subscription implementation for Graphene + Django built using Django Channels.
MIT License
116 stars 15 forks source link

Add automatic subscriptions for Django models #11

Open jaydenwindle opened 4 years ago

jaydenwindle commented 4 years ago

It would be awesome to not have to manually define created, updated and deleted subscriptions for each Django model type. Instead, we could have a DjangoModelSubscription class that takes in a DjangoObjectType as a metaclass argument and automatically defines subscriptions for each model event.

It could look something like this:

# your_app/graphql/subscriptions.py
from graphene_subscriptions.types import DjangoModelSubscription

class YourModelSubscription(DjangoModelSubscription):
    class Meta:
        type = YourModelType

# your_project/schema.py
import graphene
from your_app.graphql.subscriptions import YourModelSubscription

class Subscription(YourModelSubscription):
    pass

class Query(graphene.ObjectType):
    base = graphene.String()

schema = graphene.Schema(query=Query, subscription=Subscription)