-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate async data loaders for box product, size, and tags
- create loaders and run async query execution when dispatching GraphQL request - obtain loaders from GraphQL context in resolvers - split product resolver due to different authz enforcement cf. graphql-python/graphql-server#66 https://lightrun.com/answers/graphql-python-graphene-consider-supporting-promise-based-dataloaders-in-v3 graphql-python/graphql-core#71
- Loading branch information
Showing
6 changed files
with
84 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from collections import defaultdict | ||
|
||
from aiodataloader import DataLoader | ||
|
||
from .enums import TaggableObjectType | ||
from .models.definitions.product import Product | ||
from .models.definitions.size import Size | ||
from .models.definitions.tag import Tag | ||
from .models.definitions.tags_relation import TagsRelation | ||
|
||
|
||
class ProductLoader(DataLoader): | ||
async def batch_load_fn(self, keys): | ||
products = {p.id: p for p in Product.select().where(Product.id << keys)} | ||
return [products.get(i) for i in keys] | ||
|
||
|
||
class SizeLoader(DataLoader): | ||
async def batch_load_fn(self, keys): | ||
sizes = {s.id: s for s in Size.select()} | ||
return [sizes.get(i) for i in keys] | ||
|
||
|
||
class TagsForBoxLoader(DataLoader): | ||
async def batch_load_fn(self, keys): | ||
tags = defaultdict(list) | ||
# maybe need different join type | ||
for relation in ( | ||
TagsRelation.select() | ||
.join(Tag) | ||
.where(TagsRelation.object_type == TaggableObjectType.Box) | ||
): | ||
tags[relation.object_id].append(relation.tag) | ||
|
||
# keys are in fact box IDs. Return empty list if box has no tags assigned | ||
return [tags.get(i, []) for i in keys] |
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 |
---|---|---|
|
@@ -8,4 +8,5 @@ peewee-moves==2.1.0 | |
python-dateutil==2.8.2 | ||
python-dotenv==0.20.0 | ||
python-jose==3.3.0 | ||
aiodataloader==0.2.1 | ||
gunicorn |
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