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

feat(dataloaders): Add dataloaders for insuree & family #23

Merged
merged 3 commits into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions insuree/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,13 @@ def get_insuree_number_length(cls):

@classmethod
def get_insuree_number_modulo_root(cls):
return cls.insuree_number_modulo_root or\
(hasattr(settings, "INSUREE_NUMBER_MODULE_ROOT") and settings.INSUREE_NUMBER_MODULE_ROOT)
return cls.insuree_number_modulo_root or (
hasattr(settings, "INSUREE_NUMBER_MODULE_ROOT")
and settings.INSUREE_NUMBER_MODULE_ROOT
)

def set_dataloaders(self, dataloaders):
from .dataloaders import InsureeLoader, FamilyLoader

dataloaders["insuree_loader"] = InsureeLoader()
dataloaders["family_loader"] = FamilyLoader()
18 changes: 18 additions & 0 deletions insuree/dataloaders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from promise.dataloader import DataLoader
from promise import Promise

from .models import Insuree, Family


class InsureeLoader(DataLoader):
def batch_load_fn(self, keys):
insurees = {
insuree.id: insuree for insuree in Insuree.objects.filter(id__in=keys)
}
return Promise.resolve([insurees.get(insuree_id) for insuree_id in keys])


class FamilyLoader(DataLoader):
def batch_load_fn(self, keys):
families = {family.id: family for family in Family.objects.filter(id__in=keys)}
return Promise.resolve([families.get(family_id) for family_id in keys])
27 changes: 27 additions & 0 deletions insuree/gql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ class InsureeGQLType(DjangoObjectType):
age = graphene.Int(source='age')
client_mutation_id = graphene.String()

def resolve_current_village(self, info):
if "location_loader" in info.context.dataloaders and self.current_village_id:
return info.context.dataloaders["location_loader"].load(
self.current_village_id
)
return self.current_village

def resolve_family(self, info):
if "family_loader" in info.context.dataloaders and self.family_id:
return info.context.dataloaders["family_loader"].load(self.family_id)
return self.family

def resolve_health_facility(self, info):
if "health_facililty" in info.context.dataloaders and self.health_facility_id:
return info.context.dataloaders["health_facility"].load(
self.health_facility_id
)
return self.health_facility

class Meta:
model = Insuree
filter_fields = {
Expand Down Expand Up @@ -111,6 +130,14 @@ def get_queryset(cls, queryset, info):
class FamilyGQLType(DjangoObjectType):
client_mutation_id = graphene.String()

def resolve_location(self, info):
if "location_loader" in info.context.dataloaders:
return info.context.dataloaders["location_loader"].load(self.location_id)

def resolve_head_insuree(self, info):
if "insuree_loader" in info.context.dataloaders:
return info.context.dataloaders["insuree_loader"].load(self.head_insuree_id)

class Meta:
model = Family
filter_fields = {
Expand Down