-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: disable data loaders for DjangoConnectionField
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
- Loading branch information
Showing
10 changed files
with
86 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,48 @@ | ||
import uuid | ||
from collections import defaultdict | ||
|
||
from promise import Promise | ||
from promise.dataloader import DataLoader | ||
from typing import Callable, List | ||
|
||
from profiles.models import Address, Email, Phone | ||
|
||
|
||
def loader_for_profile(model): | ||
class BaseByProfileIdLoader(DataLoader): | ||
def batch_load_fn(self, profile_ids): | ||
items_by_profile_ids = defaultdict(list) | ||
for item in model.objects.filter(profile_id__in=profile_ids).iterator(): | ||
items_by_profile_ids[item.profile_id].append(item) | ||
return Promise.resolve( | ||
[items_by_profile_ids.get(profile_id, []) for profile_id in profile_ids] | ||
) | ||
def loader_for_profile(model) -> Callable: | ||
def batch_load_fn(profile_ids: List[uuid.UUID]) -> List[List[model]]: | ||
items_by_profile_ids = defaultdict(list) | ||
for item in model.objects.filter(profile_id__in=profile_ids).iterator(): | ||
items_by_profile_ids[item.profile_id].append(item) | ||
|
||
return [items_by_profile_ids[profile_id] for profile_id in profile_ids] | ||
|
||
return BaseByProfileIdLoader | ||
return batch_load_fn | ||
|
||
|
||
def loader_for_profile_primary(model): | ||
class BaseByProfileIdPrimaryLoader(DataLoader): | ||
def batch_load_fn(self, profile_ids): | ||
items_by_profile_ids = defaultdict() | ||
for item in model.objects.filter( | ||
profile_id__in=profile_ids, primary=True | ||
).iterator(): | ||
items_by_profile_ids[item.profile_id] = item | ||
def loader_for_profile_primary(model) -> Callable: | ||
def batch_load_fn(profile_ids: List[uuid.UUID]) -> List[model]: | ||
items_by_profile_ids = {} | ||
for item in model.objects.filter( | ||
profile_id__in=profile_ids, primary=True | ||
).iterator(): | ||
items_by_profile_ids[item.profile_id] = item | ||
|
||
return Promise.resolve( | ||
[items_by_profile_ids.get(profile_id) for profile_id in profile_ids] | ||
) | ||
return [items_by_profile_ids.get(profile_id) for profile_id in profile_ids] | ||
|
||
return BaseByProfileIdPrimaryLoader | ||
return batch_load_fn | ||
|
||
|
||
EmailsByProfileIdLoader = loader_for_profile(Email) | ||
PhonesByProfileIdLoader = loader_for_profile(Phone) | ||
AddressesByProfileIdLoader = loader_for_profile(Address) | ||
addresses_by_profile_id_loader = loader_for_profile(Address) | ||
emails_by_profile_id_loader = loader_for_profile(Email) | ||
phones_by_profile_id_loader = loader_for_profile(Phone) | ||
|
||
PrimaryEmailForProfileLoader = loader_for_profile_primary(Email) | ||
PrimaryPhoneForProfileLoader = loader_for_profile_primary(Phone) | ||
PrimaryAddressForProfileLoader = loader_for_profile_primary(Address) | ||
primary_address_for_profile_loader = loader_for_profile_primary(Address) | ||
primary_email_for_profile_loader = loader_for_profile_primary(Email) | ||
primary_phone_for_profile_loader = loader_for_profile_primary(Phone) | ||
|
||
|
||
__all__ = [ | ||
"AddressesByProfileIdLoader", | ||
"EmailsByProfileIdLoader", | ||
"PhonesByProfileIdLoader", | ||
"PrimaryAddressForProfileLoader", | ||
"PrimaryEmailForProfileLoader", | ||
"PrimaryPhoneForProfileLoader", | ||
"addresses_by_profile_id_loader", | ||
"emails_by_profile_id_loader", | ||
"phones_by_profile_id_loader", | ||
"primary_address_for_profile_loader", | ||
"primary_email_for_profile_loader", | ||
"primary_phone_for_profile_loader", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters