Skip to content

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

Update product resolvers which are using default channel (#6525)

Damian Wysocki6370f4a6b8a5
diff --git a/saleor/graphql/menu/tests/test_menu.py b/saleor/graphql/menu/tests/test_menu.py
index 932d31544d..6adf4730d2 100644
--- a/saleor/graphql/menu/tests/test_menu.py
+++ b/saleor/graphql/menu/tests/test_menu.py
@@ -328,6 +328,9 @@ def test_menu_items_collection_in_other_channel(
collection {
name
}
+ menu {
+ slug
+ }
category {
id
}
@@ -352,6 +355,7 @@ def test_menu_items_collection_in_other_channel(
content = get_graphql_content(response)
data = content["data"]["menuItem"]
assert data["name"] == menu_item.name
+ assert data["menu"]["slug"] == menu_item.menu.slug
assert len(data["children"]) == 1
assert data["children"][0]["name"] == child_menu.name
assert not data["collection"]
diff --git a/saleor/graphql/menu/types.py b/saleor/graphql/menu/types.py
index 28d355f913..fb1a723c14 100644
--- a/saleor/graphql/menu/types.py
+++ b/saleor/graphql/menu/types.py
@@ -153,14 +153,18 @@ class MenuItem(ChannelContextType, CountableDjangoObjectType):
def resolve_menu(root: ChannelContext[models.MenuItem], info, **_kwargs):
if root.node.menu_id:
menu = MenuByIdLoader(info.context).load(root.node.menu_id)
- return menu.then(lambda menu: ChannelContext(node=menu, channel_slug=None))
+ return menu.then(
+ lambda menu: ChannelContext(node=menu, channel_slug=root.channel_slug)
+ )
return None
@staticmethod
def resolve_parent(root: ChannelContext[models.MenuItem], info, **_kwargs):
if root.node.parent_id:
menu = MenuItemByIdLoader(info.context).load(root.node.parent_id)
- return menu.then(lambda menu: ChannelContext(node=menu, channel_slug=None))
+ return menu.then(
+ lambda menu: ChannelContext(node=menu, channel_slug=root.channel_slug)
+ )
return None
@staticmethod
diff --git a/saleor/graphql/product/tests/deprecated/test_product.py b/saleor/graphql/product/tests/deprecated/test_product.py
index d92454b8ec..b61ad48f1d 100644
--- a/saleor/graphql/product/tests/deprecated/test_product.py
+++ b/saleor/graphql/product/tests/deprecated/test_product.py
@@ -25,6 +25,9 @@ QUERY_FETCH_ALL_PRODUCTS = """
edges {
node {
name
+ isAvailable
+ availableForPurchase
+ isAvailableForPurchase
}
}
}
@@ -63,8 +66,16 @@ def test_fetch_all_products(user_api_client, product):
with warnings.catch_warnings(record=True) as warns:
response = user_api_client.post_graphql(QUERY_FETCH_ALL_PRODUCTS)
content = get_graphql_content(response)
+ product_channel_listing = product.channel_listings.get()
num_products = Product.objects.count()
- assert content["data"]["products"]["totalCount"] == num_products
+ data = content["data"]["products"]
+ product_data = data["edges"][0]["node"]
+ assert data["totalCount"] == num_products
+ assert product_data["isAvailable"] is True
+ assert product_data["isAvailableForPurchase"] is True
+ assert product_data["availableForPurchase"] == str(
+ product_channel_listing.available_for_purchase
+ )
assert len(content["data"]["products"]["edges"]) == num_products
assert any(
[str(warning.message) == DEPRECATION_WARNING_MESSAGE for warning in warns]
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index 7d3c3cbf9e..e448cbf613 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -644,6 +644,7 @@ class Product(ChannelContextTypeWithMetadata, CountableDjangoObjectType):
def resolve_is_available(root: ChannelContext[models.Product], info):
if not root.channel_slug:
return None
+ channel_slug = str(root.channel_slug)
def calculate_is_available(product_channel_listing):
country = info.context.country
@@ -655,7 +656,7 @@ class Product(ChannelContextTypeWithMetadata, CountableDjangoObjectType):
return (
ProductChannelListingByProductIdAndChannelSlugLoader(info.context)
- .load((root.node.id, root.channel_slug))
+ .load((root.node.id, channel_slug))
.then(calculate_is_available)
)
@@ -719,6 +720,7 @@ class Product(ChannelContextTypeWithMetadata, CountableDjangoObjectType):
def resolve_is_available_for_purchase(root: ChannelContext[models.Product], info):
if not root.channel_slug:
return None
+ channel_slug = str(root.channel_slug)
def calculate_is_available_for_purchase(product_channel_listing):
if not product_channel_listing:
@@ -727,7 +729,7 @@ class Product(ChannelContextTypeWithMetadata, CountableDjangoObjectType):
return (
ProductChannelListingByProductIdAndChannelSlugLoader(info.context)
- .load((root.node.id, root.channel_slug))
+ .load((root.node.id, channel_slug))
.then(calculate_is_available_for_purchase)
)
@@ -735,6 +737,7 @@ class Product(ChannelContextTypeWithMetadata, CountableDjangoObjectType):
def resolve_available_for_purchase(root: ChannelContext[models.Product], info):
if not root.channel_slug:
return None
+ channel_slug = str(root.channel_slug)
def calculate_available_for_purchase(product_channel_listing):
if not product_channel_listing:
@@ -743,7 +746,7 @@ class Product(ChannelContextTypeWithMetadata, CountableDjangoObjectType):
return (
ProductChannelListingByProductIdAndChannelSlugLoader(info.context)
- .load((root.node.id, root.channel_slug))
+ .load((root.node.id, channel_slug))
.then(calculate_available_for_purchase)
)
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 77d838ba20..bbfd5c3e54 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -159,7 +159,7 @@ class ProductsQueryset(models.QuerySet):
query = ProductVariantChannelListing.objects.filter(
variant_id=OuterRef("variants__id"), channel__slug=str(channel_slug)
).values_list("variant", flat=True)
- return published.filter(variants__in=query)
+ return published.filter(variants__in=query).distinct()
def visible_to_user(self, user: "User", channel_slug: str):
if self.user_has_access_to_all(user):