Search code examples
graphqlgraphene-pythongraphql-subscriptionsgraphql-mutation

Creating subscription using graphene relay


I am new to graphQL graphene(python). I would like to know if it is possible to create subscription root type using graphene. Thank you


Solution

  • It is definitely possible. You can find examples on how to do that here: https://github.com/graphql-python/graphql-ws

    Here is an example from that repo:

    import asyncio
    import graphene
    
    
    class Query(graphene.ObjectType):
        base = graphene.String()
    
    
    class Subscription(graphene.ObjectType):
        count_seconds = graphene.Float(up_to=graphene.Int())
    
        async def resolve_count_seconds(root, info, up_to):
            for i in range(up_to):
                yield i
                await asyncio.sleep(1.)
            yield up_to
    
    
    schema = graphene.Schema(query=Query, subscription=Subscription)