Skip to content

Commit

Permalink
Merge pull request #23 from openimis/dataloaders
Browse files Browse the repository at this point in the history
feat(dataloaders): Add dataloaders for insuree & family
  • Loading branch information
edarchis committed Mar 13, 2022
2 parents c998d82 + 1d2afc8 commit 24369c9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions insuree/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ def get_insuree_number_modulo_root(cls):
value = cls.insuree_number_modulo_root or cls.__get_from_settings_or_default("INSUREE_NUMBER_MODULE_ROOT")
return int(value) if value else None

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

dataloaders["insuree_loader"] = InsureeLoader()
dataloaders["family_loader"] = FamilyLoader()

@classmethod
def __get_from_settings_or_default(cls, attribute_name, default=None):
return getattr(settings, attribute_name) if hasattr(settings, attribute_name) else default
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 @@ -77,6 +77,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 @@ -113,6 +132,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

0 comments on commit 24369c9

Please sign in to comment.