graphql-sync-dataloaders

raw JSON →
0.1.1 verified Fri May 01 auth: no python

Use DataLoaders in Python GraphQL servers that run in a synchronous context (e.g., Django). Current version 0.1.1. Infrequent releases.

pip install graphql-sync-dataloaders
error ImportError: cannot import name 'SyncDataLoader' from 'graphql_sync_dataloaders'
cause Incorrect import path or outdated version; SyncDataLoader is available from version 0.1.0 onwards.
fix
Upgrade to latest version: pip install --upgrade graphql-sync-dataloaders. Then import: from graphql_sync_dataloaders import SyncDataLoader
error TypeError: batch_load_fn() missing 1 required positional argument: 'keys'
cause The batch_load_fn method signature expects exactly one argument (keys), but the class may not be properly subclassed or the method is defined incorrectly.
fix
Define batch_load_fn(self, keys) as a method of your subclass. Example: class MyLoader(SyncDataLoader): def batch_load_fn(self, keys): return [...]
gotcha SyncDataLoader.batch_load_fn must return a list of values in the exact same order as the input keys. Returning a dict and mapping may produce incorrect order.
fix Always ensure batch_load_fn returns a list parallel to keys, using list comprehension or similar.
deprecated The package has no known deprecations, but it is a very small library with low activity. Check for updates before relying on it for critical production systems.
fix Monitor the GitHub repository for new releases or consider contributing fixes.

Example using SyncDataLoader with Django ORM. batch_load_fn must return a list of results parallel to keys.

from graphql_sync_dataloaders import SyncDataLoader

class UserLoader(SyncDataLoader):
    def batch_load_fn(self, keys):
        # keys is a list of user IDs; return list of users in same order
        users = {user.id: user for user in User.objects.filter(id__in=keys)}
        return [users.get(key) for key in keys]

loader = UserLoader()
user = loader.load(1)  # Synchronous call
users = loader.load_many([1, 2, 3])