Search code examples
pythonpublish-subscribegraphqlgraphene-pythongraphql-subscriptions

Example of Subscription class using Graphene and Python


I trying to figure out how to correctly define subscriptions in my schema using graphene-python. So far I have implemented queries and mutations, but how do you define a Subscription class?

Below is what I was originally thinking:

class Subscription(graphene.Subscription):
  name = graphene.String()
  # rest of attributes...

  def subscribe(self, args, context, info):
    pass

Can someone please provide a small example? Any help would be greatly appreciated! Thank you :).

Brian


Solution

  • So after some trial and error, the following code below will work for subscriptions. Essentially a subscription can be treated the same as a query.

    class Subscription(graphene.ObjectType):
      # Define subscription attributes, i.e. what you want the user to subscribe to.
      # This part will most likely be defined by your schema.
      subscription_attr = graphene.Int()
    
      def resolve_events_count(self, args, context, info):
        ## define resolver function once UI provides subscription data...
        return 'Value here defined as graphene class'