Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Use dataloader instead of prefetch_related_objects

Ania Warzechaad248334e909
diff --git a/saleor/graphql/attribute/utils.py b/saleor/graphql/attribute/utils.py
index 3fbfc532e3..2a9b76292b 100644
--- a/saleor/graphql/attribute/utils.py
+++ b/saleor/graphql/attribute/utils.py
@@ -761,8 +761,10 @@ class AttributeAssignmentMixin:
class PageAttributeAssignmentMixin(AttributeAssignmentMixin):
- # TODO: merge the code here with the mixin above
- # when all attribute relations are cleaned up
+ # TODO: Merge the code here with the mixin above
+ # after the refactor of Page <> Attribute and
+ # Product <> Attribute dedicated mixins
+ # should me merged into AttributeAssignmentMixin
@classmethod
def _get_assigned_attribute_value_if_exists(
diff --git a/saleor/graphql/page/dataloaders.py b/saleor/graphql/page/dataloaders.py
index 4b0ae7c8d5..ecb31fbd8c 100644
--- a/saleor/graphql/page/dataloaders.py
+++ b/saleor/graphql/page/dataloaders.py
@@ -1,11 +1,11 @@
from collections import defaultdict
-from django.db.models import prefetch_related_objects
+from promise import Promise
from ...attribute.models import AssignedPageAttributeValue, AttributePage
from ...page.models import Page, PageType
from ...permission.enums import PagePermissions
-from ..attribute.dataloaders import AttributesByAttributeId
+from ..attribute.dataloaders import AttributesByAttributeId, AttributeValueByIdLoader
from ..core.dataloaders import DataLoader
from ..utils import get_user_or_app_from_context
@@ -98,19 +98,21 @@ class AttributeValuesByPageIdLoader(DataLoader):
.filter(page_id__in=keys)
.iterator()
)
- prefetch_related_objects(attribute_values, "value")
+ value_ids = [a.value_id for a in attribute_values]
def with_pages(pages):
pages = [page for page in pages if page]
page_type_ids = [p.page_type_id for p in pages]
- def with_attributes(attribute_pages):
+ def with_attributes_and_values(result):
+ attribute_pages, values = result
page_type_attrubutes = dict(zip(page_type_ids, attribute_pages))
+ values_by_id_map = dict(zip(value_ids, values))
assigned_page_map = defaultdict(list)
for page in pages:
page_values = [
- page_value.value
+ values_by_id_map.get(page_value.value_id)
for page_value in attribute_values
if page_value.page_id == page.id
]
@@ -131,11 +133,11 @@ class AttributeValuesByPageIdLoader(DataLoader):
)
return [assigned_page_map[key] for key in keys]
- return (
- PageAttributesByPageTypeIdLoader(self.context)
- .load_many(page_type_ids)
- .then(with_attributes)
+ attributes = PageAttributesByPageTypeIdLoader(self.context).load_many(
+ page_type_ids
)
+ values = AttributeValueByIdLoader(self.context).load_many(value_ids)
+ return Promise.all([attributes, values]).then(with_attributes_and_values)
return PageByIdLoader(self.context).load_many(keys).then(with_pages)
diff --git a/saleor/graphql/page/tests/mutations/test_page_update.py b/saleor/graphql/page/tests/mutations/test_page_update.py
index ed5851511c..c8fd174695 100644
--- a/saleor/graphql/page/tests/mutations/test_page_update.py
+++ b/saleor/graphql/page/tests/mutations/test_page_update.py
@@ -1139,7 +1139,6 @@ def test_update_page_change_attribute_values_ordering(
for val in [attr_value_2, attr_value_1, attr_value_3]
]
- # page.refresh_from_db()
assert list(
get_page_attribute_values(page, attribute).values_list("id", flat=True)
) == [attr_value_2.pk, attr_value_1.pk, attr_value_3.pk]