graphql-core-legacy
graphql-core-legacy copied to clipboard
Execute should return an awaitable with AsyncioExecutor
The current graphql.execution.execute.execute implementation forces AsyncioExecutor to use loop.run_until_complete in wait_until_finished. This is a bit ugly.
It would be better to let the executor decide what is returned from the execute method. It can be given a function to call after the data is ready as an argument.
With this AsyncioExecutor could return an awaitable and execute would be used as one would expect
result = await execute(schema, ast, executor=AsyncioExecutor())
It could even have an argument to get the new way to keep compatibility with existing uses.
This would also make TwistedExecutor much easier to implement.
Hi @ilponyy, you can use return_promise=True
and then await on the promise :)
However if you have an idea of how to make the execute work in the following environments:
- Python 2.7 without await and synchronous executor.
- Python 2.7 without await custom event event loop (gevent, twisted, threads, ...)
- Python 3.5 with
await
I will super happy to consider it! (this part is a little bit tricky as we have to support multiple "executors" and syntax at the same time)
Didn't notice that return_promise. That should work. It actually should be the preferred way with asyncio instead of that wait_until_finished hack.
Hi @syrusakbary, return_promise=True
solution works fine for me, but if you are looking for inspiration on implementation with await
keyword, you might want to check out Jinja2's way to add async support: https://github.com/pallets/jinja/blob/master/jinja2/asyncsupport.py