-
I'm looking for a way (or work-around) for data-loading (batching) in sync web apps (in our case, we're restricted to use sync because the ORM
which results in N calls to fetch author information from the DB that could be batched. I've found related discussions here and here.
FWIW, here are some pages that I dug out researching for using dataloaders in sync apps: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I found a way to integrate aiodataloader in my WSGI flask app like so: # routes.py
import asyncio
from ariadne import graphql
from flask import Flask, request, jsonify
from .loaders import AuthorLoader
from .schema import full_api_schema
app = Flask(__name__)
# omitting database config with peewee via playhouse.flask_utils
@app.route("/graphql", methods=["POST"])
def graphql_server():
# Start async event loop, required for DataLoader construction, cf.
# https://github.com/graphql-python/graphql-core/issues/71#issuecomment-620106364
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Create DataLoaders and persist them for the time of processing the request
context = {"author_loader": AuthorLoader()}
success, result = loop.run_until_complete(
graphql(
full_api_schema,
data=request.get_json(),
context_value=context,
)
)
status_code = 200 if success else 400
return jsonify(result), status_code
# loaders.py
from aiodataloader import DataLoader
from .author import Author # peewee model definition
class AuthorLoader(DataLoader):
async def batch_load_fn(self, keys):
authors = {a.id: a for a in Author.select()}
return [authors.get(i) for i in keys]
# resolvers.py
from ariadne import ObjectType
blogpost = ObjectType("Blogpost")
@blogpost.field("author")
def resolve_blogpost_author(blogpost_obj, info):
return info.context["author_loader"].load(blogpost_obj.id) |
Beta Was this translation helpful? Give feedback.
-
For the record, there's now a dataloaders reference for Ariadne that documents both async and sync dataloaders: https://ariadnegraphql.org/docs/dataloaders |
Beta Was this translation helpful? Give feedback.
I found a way to integrate aiodataloader in my WSGI flask app like so: