graphql-core-legacy icon indicating copy to clipboard operation
graphql-core-legacy copied to clipboard

Execute should return an awaitable with AsyncioExecutor

Open ghost opened this issue 8 years ago • 4 comments

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.

ghost avatar Sep 18 '16 17:09 ghost

Hi @ilponyy, you can use return_promise=True and then await on the promise :)

syrusakbary avatar Sep 18 '16 19:09 syrusakbary

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)

syrusakbary avatar Sep 18 '16 21:09 syrusakbary

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.

ghost avatar Sep 19 '16 05:09 ghost

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

grazor avatar Feb 23 '17 22:02 grazor