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

Dataloaders not working #1425

Open
alexandrubese opened this issue Jun 12, 2023 · 4 comments
Open

Dataloaders not working #1425

alexandrubese opened this issue Jun 12, 2023 · 4 comments
Labels

Comments

@alexandrubese
Copy link

alexandrubese commented Jun 12, 2023

Hi,
I have read the documentation around data loaders from your site, but I can't make them to work.
I followed exactly your examples but when I run the Query I get:

INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
/Users/alexandrubese/Documents/work/banking_api/venv/lib/python3.10/site-packages/graphql/execution/execute.py:431: RuntimeWarning: coroutine 'Query.resolve_account' was never awaited
result = self.execute_field(
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

account_dataloader.py

from aiodataloader import DataLoader
from core.models import Account

def get_account(id):
    return Account.objects.get(pk=id)


class AccountLoader(DataLoader):
    async def batch_load_fn(self, keys):
        return [get_account(id=key) for key in keys]

schema.py

class Account(ObjectType):
    id = ID()
    name = String()
    email = String()
    city = String()
    canton = String()
    toy_category_preference = List(Int)
    toy_size_preference = List(Int)

class Query(ObjectType):
    hello = String(default_value="Hi!")
    account = Field(Account, id=ID(required=True))

    async def resolve_account(root, info, id):
        account_data = await AccountLoader().load(id)
        return account_data

What am I doing wrong?

@belkka
Copy link
Contributor

belkka commented Jun 22, 2023

Could you provide links to the doc and examples you followed? I found this https://docs.graphene-python.org/en/latest/execution/dataloader/.

RuntimeWarning: coroutine 'Query.resolve_account' was never awaited
What am I doing wrong?

Apparently, you define an async resolver in a query.

Looks like in order to properly run async resolvers in queries, one have to use Schema.execute_async instead of Schema.execute and the latter is poorly documented. In fact, I only found a brief mention of this in API reference, but not in any other parts of the docs, so this is only my assumption.

It also looks like graphene-django only use Schema.execute, as I do not find matches for .execute_async in the source code. The "Subscriptions" section of graphene-django docs says that even for subscriptions you have to install and configure additional tools (you need to use websockets and ASGI).

The last example at https://docs.graphene-python.org/en/latest/execution/dataloader/ has async resolvers in graphene.ObjectType. It may be correct (for queries executed via Schema.execute_async or for subscriptions), but misleading, as documentation does not really mention how to work with async resolvers. I think docs should be updated to provide clarifications on this.

@belkka
Copy link
Contributor

belkka commented Jun 22, 2023

Oh, just noticed this issue is a duplicate of #1390. It also mentions PR #1394, which aims to add support for Schema.execute_async

@pfcodes
Copy link

pfcodes commented Dec 21, 2023

So in other words, until Schema.execute_async is implemented, there's no way to use Dataloaders with graphene_django?

@alexandrubese
Copy link
Author

Any updates?

charn added a commit to City-of-Helsinki/open-city-profile that referenced this issue Feb 2, 2024
Data loaders that exist are not fully compatible with new versions of
graphene and graphene-django. DjangoConnectionField doesn't seem to handle
loaders correctly and instead return errors like:

"Cannot return null for non-nullable field EmailNodeConnection.edges."

So for now, data loaders will be disabled for this field type.

Use graphql-sync-dataloaders to make other types of fields work with
data loaders.

Some GitHub issues for reference:
- graphql-python/graphene-django#1394
- graphql-python/graphene-django#1263
- graphql-python/graphene-django#1425

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

No branches or pull requests

3 participants