0

all! I try to use lib graphql-ws https://github.com/graphql-python/graphql-ws to create a subscription on a web-socket. I need to set execution option allow_subscriptions to True in execution of subscribe() of custom Observable

class Subscription(graphene.ObjectType):
solution = graphene.Field(
    SolutionType
)

def resolve_solution(self, info):
    return SolutionObserver()   
    
graphql_schema = graphene.Schema(query=Query, mutation=Mutations, subscription=Subscription)

class SolutionObserver(Observable):
is_ready = False
solution = None

async def subscribe(self, observer):
    counter = 0
    while True:
        if SolutionObserver.is_ready:
            logger.info("SolutionObserver: is ready")
            return SolutionObserver.solution
        elif counter > 2000:
            logger.error("SolutionObserver: no solution was created, timeout err")
            return None
        else:
            logger.info("SolutionObserver: zzz")
            await asyncio.sleep(10)
            counter += 1


@classmethod
def reset(cls):
    logger.info("SolutionObserver: reset solution")
    cls.is_ready = False
    cls.solution = None

@classmethod
def set_solution(cls, solution):
    logger.info("SolutionObserver: set solution")
    cls.is_ready = True
    cls.solution = solution

I try to get in graphiql in Chrome.

subscription{solution {
id}}

but became

{ "errors": [ { "message": "Subscriptions are not allowed. You will need to either use the subscribe function or pass allow_subscriptions=True" } ], "data": null }

Calling of subscribe function is in task on server startup

async def subscribe_on_create_solution(solution_observer):
 await solution_observer.subscribe(None)

async def on_startup(app):
 import asyncio
 solution_observer = SolutionObserver()
 asyncio.get_event_loop().create_task(subscribe_on_create_solution(solution_observer))

Have anybody ideas?

Red
  • 26,798
  • 7
  • 36
  • 58
Kate Zz
  • 3,002
  • 5
  • 14
  • 17
  • this helps:class GraphQLCustomCoreBackend(GraphQLCoreBackend): def __init__(self, executor=None): # type: (Optional[Any]) -> None super().__init__(executor) self.execute_params['allow_subscriptions'] = True set_default_backend(GraphQLCustomCoreBackend()) – Kate Zz Sep 22 '20 at 14:49
  • I'm sure plenty of opportunities of you getting an answer didn't happen as the `python` tag is not present. – Red Nov 23 '20 at 20:55

0 Answers0