Skip to main content

asyncio + graphql = fast and simple api

Project description

  • asyncio - explicit concurrency to reduce race conditions

  • graphql - all you need and nothing more in one request +auto docs of your api

  • uvloop, protocol - top performance

  • minimal http - unlike REST frameworks that are waste of time for /graphql endpoint

  • pluggable context - for auth, logging, etc

  • exception handling - at all levels, with default or custom handler


pip install aiographql

cat <<'END' >
import asyncio, aiographql, graphene

class User(graphene.ObjectType):
    id = graphene.ID(required=True)
    name = graphene.String()

class Query(graphene.ObjectType):
    me = graphene.Field(User)

    async def resolve_me(self, info):
        await asyncio.sleep(1)  # DB
        return User(id=42, name='John')

schema = graphene.Schema(query=Query, mutation=None)

aiographql.serve(schema, listen=[
    dict(protocol='tcp', port=25100),
    dict(protocol='unix', path='/tmp/worker0'),


curl http://localhost:25100/ --data-binary \
'{"query": "{
    me {
}", "variables": null}'

# OR:
curl --unix-socket /tmp/worker0 http:/ --data-binary ...

# Result:
# 1 second async await for DB and then:

See more examples and tests about JWT auth, concurrent slow DB queries, etc.


import aiographql; help(aiographql.serve)

serve(schema, listen, get_context=None, exception_handler=None, enable_uvloop=True, run=True)
    Configure the stack and start serving requests
  • schema: graphene.Schema - GraphQL schema to serve

  • listen: list - one or more endpoints to listen for connections:

  • get_context: None or [async] callable(loop, context: dict): mixed - to produce GraphQL context like auth from input unified with exception_handler()

  • exception_handler: None or callable(loop, context: dict) - default or custom exception handler as defined in the docs +

    • headers: bytes or None - HTTP headers, if known

    • request: dict or bytes or None - accumulated HTTP request before content length is known, then accumulated content, then GraphQL request

  • enable_uvloop: bool - enable uvloop for top performance, unless you have a better loop

  • run: bool - if True, run the loop; False is good for tests

  • return servers: Servers - await servers.close() to close listening sockets - good for tests

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiographql-0.2.1.tar.gz (7.0 kB view hashes)

Uploaded source

Built Distribution

aiographql-0.2.1-py3-none-any.whl (9.0 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page