Skip to content

Use Promises in execute context #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 26, 2016
Merged

Conversation

syrusakbary
Copy link
Member

This PR improves the executor abstraction by using Promises.
At the same time, removes all the usage of the Middlewares as are no longer necessary.

The Promise implementation is based in aplus with a lot of improvements and fixes like Promise.all, promise_for_dict, promise.do_resolve.

The resolvers can now return a raw object or a promise.

Executors

The following executors are available:

  • graphql.execution.executors.asyncio.AsyncioExecutor: This executor executes the resolvers in the Python asyncio event loop.
  • graphql.execution.executors.asyncio.GeventExecutor: This executor executes the resolvers in the Gevent event loop.
  • graphql.execution.executors.asyncio.ProcessExecutor: This executor executes each resolver as a process
  • graphql.execution.executors.asyncio.ThreadExecutor: This executor executes each resolver in a Thread.
  • graphql.execution.executors.asyncio.SyncExecutor: This executor executes each resolver synchronusly (default).

Usage

You can specify the executor to use via the executor keyword argument in the grapqhl.execution.execute function.

from graphql.execution.execute import execute

execute(schema, ast, executor=SyncExecutor())

If no executor is specified, SyncExecutor will be used by default.

@syrusakbary syrusakbary mentioned this pull request Apr 23, 2016
32 tasks
@Globegitter
Copy link

Globegitter commented Apr 23, 2016

@syrusakbary that is great stuff! So if I use a library that returns a Future should I be able to simply wrap that in a promise?

So what I am imagining is using the SyncExecutor but every resolve function that does some IO would return 'immediately' with a Promise. Inside the resolver we are using a request library (GRPC) that returns a Future for any IO calls. This Future I would then wrap in a Promise, which, via .then() will return the Promise with actual resolve result.

Is that something that should work with this implementation?

@jhgg
Copy link
Member

jhgg commented Apr 24, 2016

Neat!

@jhgg
Copy link
Member

jhgg commented Apr 24, 2016

Process and thread executor should be able to delegate to a pool (shared or individual pool per execution) of finite size. To prevent large queries from blowing the OS

@syrusakbary syrusakbary merged commit b881789 into releases/0.5.0 Apr 26, 2016
@syrusakbary
Copy link
Member Author

syrusakbary commented Apr 26, 2016

@Globegitter is not necessarily that, your resolver function could return a Future too and will work!
(But you need to specify the executor as AsyncioExecutor)

@syrusakbary
Copy link
Member Author

But if you want to use Promises it will work too (even with the SyncExecutor)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants