Skip to content

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

Fix TypeError when resolving default channel slug on product types (#19251)

Marcin Wcisłod6656e0f2a1d
diff --git a/saleor/graphql/product/tests/queries/test_product_query.py b/saleor/graphql/product/tests/queries/test_product_query.py
index 680c1a99a8..25b697051d 100644
--- a/saleor/graphql/product/tests/queries/test_product_query.py
+++ b/saleor/graphql/product/tests/queries/test_product_query.py
@@ -3216,3 +3216,30 @@ def test_applies_limit_on_product_assigned_attributes(
content["data"]["product"]["assignedAttributes"][0]["attribute"]["slug"]
== first_attribute.slug
)
+
+
+CHANNEL_FIELD_QUERY = """
+query getProduct($id: ID!) {
+ product(id: $id) {
+ id
+ channel
+ }
+}
+"""
+
+
+def test_channel_field_with_default_channel_anonymous(api_client, product, channel_USD):
+ """No `channel` argument is passed and the anonymous requestor has no product permissions, so the resolver falls back to the lazy default channel slug."""
+
+ # given
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ variables = {"id": product_id}
+
+ # when
+ response = api_client.post_graphql(CHANNEL_FIELD_QUERY, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["product"]
+ assert data["id"] == product_id
+ assert data["channel"] == channel_USD.slug
diff --git a/saleor/graphql/product/tests/queries/test_variant_query.py b/saleor/graphql/product/tests/queries/test_variant_query.py
index bd3c1bc1ba..36e0458446 100644
--- a/saleor/graphql/product/tests/queries/test_variant_query.py
+++ b/saleor/graphql/product/tests/queries/test_variant_query.py
@@ -1143,3 +1143,30 @@ def test_stocks_resolver_ignores_shipping_zones_excluded_from_stock_calculations
stocks_without_country = content["data"]["productVariant"]["stocksWithoutCountry"]
assert len(stocks_with_country) == expected_stock_count
assert len(stocks_without_country) == expected_stock_count
+
+
+CHANNEL_FIELD_QUERY = """
+query getProductVariant($id: ID!) {
+ productVariant(id: $id) {
+ id
+ channel
+ }
+}
+"""
+
+
+def test_channel_field_with_default_channel_anonymous(api_client, variant, channel_USD):
+ """No `channel` argument is passed and the anonymous requestor has no product permissions, so the resolver falls back to the lazy default channel slug."""
+
+ # given
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ variables = {"id": variant_id}
+
+ # when
+ response = api_client.post_graphql(CHANNEL_FIELD_QUERY, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productVariant"]
+ assert data["id"] == variant_id
+ assert data["channel"] == channel_USD.slug
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index 6d7fef66e2..e24908fcc1 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -489,7 +489,7 @@ class ProductVariant(ChannelContextType[models.ProductVariant]):
@staticmethod
def resolve_channel(root: ChannelContext[models.Product], _info):
- return root.channel_slug
+ return str(root.channel_slug) if root.channel_slug else None
@staticmethod
@load_site_callback
@@ -1139,7 +1139,7 @@ class Product(ChannelContextType[models.Product]):
@staticmethod
def resolve_channel(root: ChannelContext[models.Product], _info):
- return root.channel_slug
+ return str(root.channel_slug) if root.channel_slug else None
@staticmethod
def resolve_default_variant(root: ChannelContext[models.Product], info):