Skip to content
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

Document an example of async usage? #103

Closed
ajhyndman opened this issue Feb 2, 2019 · 5 comments
Closed

Document an example of async usage? #103

ajhyndman opened this issue Feb 2, 2019 · 5 comments

Comments

@ajhyndman
Copy link
Contributor

I'm not an expert in the python ecosystem, and am not sure what's required from me to support asynchronous resolvers in an Ariadne schema. If it's possible, I'd love a brief example illustrating a working async resolver!


What I've tried so far (using python 3.7):

from aiodataloader import DataLoader
from ariadne import ResolverMap, gql, start_simple_server

# DATA LOADERS
heroesLoader = HeroesLoader()

class HeroesLoader(DataLoader):
    async def batch_load_fn(self, keys):
        # return await batch_get_heroes()

# SCHEMA DEFINITION
type_defs = gql("""
type Query {
    heroes: [Hero!]!
}

type Hero {
    name: String!
    shortName: String
    title: String
}
""")

# RESOLVERS
query = ResolverMap("Query")
hero = ResolverMap("Hero")

@query.field("heroes")
async def resolve_heroes(*_):
    return await heroesLoader.load('_')

start_simple_server(type_defs, [query, hero])
@ajhyndman ajhyndman changed the title Document and example of async usage? Document an example of async usage? Feb 2, 2019
@rafalp
Copy link
Contributor

rafalp commented Feb 2, 2019

Hello,

start_simple_server is purely synchronous at this time, so you can't use async with it, but we have changes in the pipeline introducing proper support for async simple server we want to ship in next release:

@ajhyndman
Copy link
Contributor Author

ajhyndman commented Feb 2, 2019

Cool! So I would need to implement my own WSGI layer which supports async execution, until that lands?

Also, I would love to get involved and help out with this project. Do you think there are any good starting points. in which I might be at all helpful?

@rafalp
Copy link
Contributor

rafalp commented Feb 2, 2019

If you don't need subscriptions in your GraphQL server, you can create custom WSGI middleware inheriting from GraphQLMiddleware's but using graphql instead of graphql_sync for query execution: https://github.com/mirumee/ariadne/blob/master/ariadne/wsgi_middleware.py#L154

@rafalp
Copy link
Contributor

rafalp commented Feb 2, 2019

If you want to help, please take a look on issues marked with Help wanted.

@ajhyndman
Copy link
Contributor Author

I'll close this issue, since it looks like it's already on the team's radar!

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

No branches or pull requests

2 participants