Skip to content

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

Fix variant resolver on CheckoutLine and OrderLine (#6641)

Filip Owczarek9057e82cea23
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 2eb3281359..30404fcd0b 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -2828,3 +2828,33 @@ def test_clean_checkout_no_payment(checkout_with_item, shipping_method, address)
msg = "Provided payment methods can not cover the checkout's total amount"
assert e.value.error_list[0].message == msg
+
+
+def test_get_variant_data_from_checkout_line_variant_hidden_in_listings(
+ checkout_with_item, api_client
+):
+ # given
+ query = """
+ query getCheckout($token: UUID!){
+ checkout(token: $token){
+ id
+ lines{
+ id
+ variant{
+ id
+ }
+ }
+ }
+ }
+ """
+ checkout = checkout_with_item
+ variant = checkout.lines.get().variant
+ variant.product.channel_listings.update(visible_in_listings=False)
+ variables = {"token": checkout.token}
+
+ # when
+ response = api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ assert content["data"]["checkout"]["lines"][0]["variant"]["id"]
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index 4296071c8a..ea2d0219f5 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -24,7 +24,6 @@ from ..product.dataloaders import (
ProductVariantByIdLoader,
VariantChannelListingByVariantIdAndChannelSlugLoader,
)
-from ..product.resolvers import resolve_variant
from ..shipping.dataloaders import (
ShippingMethodByIdLoader,
ShippingMethodChannelListingByShippingMethodIdAndChannelSlugLoader,
@@ -85,8 +84,12 @@ class CheckoutLine(CountableDjangoObjectType):
@staticmethod
def resolve_variant(root: models.CheckoutLine, info):
- dataloader = ChannelByCheckoutLineIDLoader(info.context)
- return resolve_variant(info, root, dataloader)
+ variant = ProductVariantByIdLoader(info.context).load(root.variant_id)
+ channel = ChannelByCheckoutLineIDLoader(info.context).load(root.id)
+
+ return Promise.all([variant, channel]).then(
+ lambda data: ChannelContext(node=data[0], channel_slug=data[1].slug)
+ )
@staticmethod
def resolve_total_price(root, info):
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index ae8de39f8c..61926b25d6 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -4755,3 +4755,218 @@ def test_draft_orders_query_with_filter_search_by_id(
response = staff_api_client.post_graphql(draft_orders_query_with_filter, variables)
content = get_graphql_content(response)
assert content["data"]["draftOrders"]["totalCount"] == 1
+
+
+QUERY_GET_VARIANTS_FROM_ORDER = """
+{
+ me{
+ orders(first:10){
+ edges{
+ node{
+ lines{
+ variant{
+ id
+ }
+ }
+ }
+ }
+ }
+ }
+}
+"""
+
+
+def test_get_variant_from_order_line_variant_published_as_customer(
+ user_api_client, order_line
+):
+ # given
+
+ # when
+ response = user_api_client.post_graphql(QUERY_GET_VARIANTS_FROM_ORDER, {})
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["me"]["orders"]["edges"]
+ assert orders[0]["node"]["lines"][0]["variant"]["id"]
+
+
+def test_get_variant_from_order_line_variant_published_as_admin(
+ staff_api_client, order_line, permission_manage_products
+):
+ # given
+ order = order_line.order
+ order.user = staff_api_client.user
+ order.save()
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_GET_VARIANTS_FROM_ORDER,
+ {},
+ permissions=(permission_manage_products,),
+ check_no_permissions=False,
+ )
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["me"]["orders"]["edges"]
+ assert orders[0]["node"]["lines"][0]["variant"]["id"]
+
+
+def test_get_variant_from_order_line_variant_not_published_as_customer(
+ user_api_client, order_line
+):
+ # given
+ product = order_line.variant.product
+ product.channel_listings.update(is_published=False)
+
+ # when
+ response = user_api_client.post_graphql(QUERY_GET_VARIANTS_FROM_ORDER, {})
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["me"]["orders"]["edges"]
+ assert orders[0]["node"]["lines"][0]["variant"] is None
+
+
+def test_get_variant_from_order_line_variant_not_published_as_admin(
+ staff_api_client, order_line, permission_manage_products
+):
+ # given
+ order = order_line.order
+ order.user = staff_api_client.user
+ order.save()
+ product = order_line.variant.product
+ product.channel_listings.update(is_published=False)
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_GET_VARIANTS_FROM_ORDER,
+ {},
+ permissions=(permission_manage_products,),
+ check_no_permissions=False,
+ )
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["me"]["orders"]["edges"]
+ assert orders[0]["node"]["lines"][0]["variant"]["id"]
+
+
+def test_get_variant_from_order_line_variant_not_assigned_to_channel_as_customer(
+ user_api_client, order_line
+):
+ # given
+ product = order_line.variant.product
+ product.channel_listings.all().delete()
+
+ # when
+ response = user_api_client.post_graphql(QUERY_GET_VARIANTS_FROM_ORDER, {})
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["me"]["orders"]["edges"]
+ assert orders[0]["node"]["lines"][0]["variant"] is None
+
+
+def test_get_variant_from_order_line_variant_not_assigned_to_channel_as_admin(
+ staff_api_client, order_line, permission_manage_products
+):
+ # given
+ order = order_line.order
+ order.user = staff_api_client.user
+ order.save()
+ product = order_line.variant.product
+ product.channel_listings.all().delete()
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_GET_VARIANTS_FROM_ORDER,
+ {},
+ permissions=(permission_manage_products,),
+ check_no_permissions=False,
+ )
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["me"]["orders"]["edges"]
+ assert orders[0]["node"]["lines"][0]["variant"]["id"]
+
+
+def test_get_variant_from_order_line_variant_not_visible_in_listings_as_customer(
+ user_api_client, order_line
+):
+ # given
+ product = order_line.variant.product
+ product.channel_listings.update(visible_in_listings=False)
+
+ # when
+ response = user_api_client.post_graphql(QUERY_GET_VARIANTS_FROM_ORDER, {})
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["me"]["orders"]["edges"]
+ assert orders[0]["node"]["lines"][0]["variant"]["id"]
+
+
+def test_get_variant_from_order_line_variant_not_visible_in_listings_as_admin(
+ staff_api_client, order_line, permission_manage_products
+):
+ # given
+ order = order_line.order
+ order.user = staff_api_client.user
+ order.save()
+ product = order_line.variant.product
+ product.channel_listings.update(visible_in_listings=False)
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_GET_VARIANTS_FROM_ORDER,
+ {},
+ permissions=(permission_manage_products,),
+ check_no_permissions=False,
+ )
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["me"]["orders"]["edges"]
+ assert orders[0]["node"]["lines"][0]["variant"]["id"]
+
+
+def test_get_variant_from_order_line_variant_not_exists_as_customer(
+ user_api_client, order_line
+):
+ # given
+ order_line.variant = None
+ order_line.save()
+
+ # when
+ response = user_api_client.post_graphql(QUERY_GET_VARIANTS_FROM_ORDER, {})
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["me"]["orders"]["edges"]
+ assert orders[0]["node"]["lines"][0]["variant"] is None
+
+
+def test_get_variant_from_order_line_variant_not_exists_as_staff(
+ staff_api_client, order_line, permission_manage_products
+):
+ # given
+ order = order_line.order
+ order.user = staff_api_client.user
+ order.save()
+ order_line.variant = None
+ order_line.save()
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_GET_VARIANTS_FROM_ORDER,
+ {},
+ permissions=(permission_manage_products,),
+ check_no_permissions=False,
+ )
+
+ # then
+ content = get_graphql_content(response)
+ orders = content["data"]["me"]["orders"]["edges"]
+ assert orders[0]["node"]["lines"][0]["variant"] is None
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 9e6c8eee14..b1f5d2710a 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -12,6 +12,7 @@ from ...order import OrderStatus, models
from ...order.models import FulfillmentStatus
from ...order.utils import get_order_country, get_valid_shipping_methods_for_order
from ...plugins.manager import get_plugins_manager
+from ...product.models import Product
from ...product.templatetags.product_images import get_product_image_thumbnail
from ...warehouse import models as warehouse_models
from ..account.types import User
@@ -27,7 +28,10 @@ from ..giftcard.types import GiftCard
from ..invoice.types import Invoice
from ..meta.types import ObjectWithMetadata
from ..payment.types import OrderAction, Payment, PaymentChargeStatusEnum
-from ..product.resolvers import resolve_variant
+from ..product.dataloaders import (
+ ProductChannelListingByProductIdAndChannelSlugLoader,
+ ProductVariantByIdLoader,
+)
from ..product.types import ProductVariant
from ..shipping.dataloaders import ShippingMethodByIdLoader
from ..shipping.types import ShippingMethod
@@ -316,8 +320,35 @@ class OrderLine(CountableDjangoObjectType):
@staticmethod
def resolve_variant(root: models.OrderLine, info):
- dataloader = ChannelByOrderLineIdLoader(info.context)
- return resolve_variant(info, root, dataloader)
+ context = info.context
+ if not root.variant_id:
+ return None
+
+ def requestor_has_access_to_variant(data):
+ variant, channel = data
+
+ requester = get_user_or_app_from_context(context)
+ requestor_has_access_to_all = Product.objects.user_has_access_to_all(
+ requester
+ )
+ if requestor_has_access_to_all:
+ return ChannelContext(node=variant, channel_slug=channel.slug)
+
+ def product_is_available(product_channel_listing):
+ if product_channel_listing and product_channel_listing.is_visible:
+ return ChannelContext(node=variant, channel_slug=channel.slug)
+ return None
+
+ return (
+ ProductChannelListingByProductIdAndChannelSlugLoader(context)
+ .load((variant.product_id, channel.slug))
+ .then(product_is_available)
+ )
+
+ variant = ProductVariantByIdLoader(context).load(root.variant_id)
+ channel = ChannelByOrderLineIdLoader(context).load(root.id)
+
+ return Promise.all([variant, channel]).then(requestor_has_access_to_variant)
@staticmethod
@one_of_permissions_required(
diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.py
index 016a7c5bda..bce9969560 100644
--- a/saleor/graphql/product/resolvers.py
+++ b/saleor/graphql/product/resolvers.py
@@ -1,22 +1,9 @@
-from typing import Union
-
from django.db.models import Sum
-from promise import Promise
-from ...checkout.models import CheckoutLine
from ...order import OrderStatus
-from ...order.models import OrderLine
from ...product import models
-from ..channel import ChannelContext, ChannelQsContext
-from ..channel.dataloaders import (
- ChannelByCheckoutLineIDLoader,
- ChannelByOrderLineIdLoader,
-)
-from ..product.dataloaders import (
- ProductChannelListingByProductIdAndChannelSlugLoader,
- ProductVariantByIdLoader,
-)
-from ..utils import get_database_id, get_user_or_app_from_context
+from ..channel import ChannelQsContext
+from ..utils import get_database_id
from ..utils.filters import filter_by_period
from .filters import filter_products_by_stock_availability
@@ -148,41 +135,3 @@ def resolve_report_product_sales(period, channel_slug) -> ChannelQsContext:
)
qs = qs.order_by("-quantity_ordered")
return ChannelQsContext(qs=qs, channel_slug=channel_slug)
-
-
-def resolve_variant(
- info,
- root: Union[CheckoutLine, OrderLine],
- channel_dataloader: Union[
- ChannelByCheckoutLineIDLoader, ChannelByOrderLineIdLoader
- ],
-):
- context = info.context
- if not root.variant_id:
- return None
-
- def requestor_has_access_to_variant(data):
- variant, channel = data
-
- def product_is_available(product_channel_listing):
- if not product_channel_listing:
- return None
- requester = get_user_or_app_from_context(context)
- visible_in_listings = product_channel_listing.visible_in_listings
- requestor_has_access_to_all = models.Product.objects.user_has_access_to_all(
- requester
- )
- if visible_in_listings or requestor_has_access_to_all:
- return ChannelContext(node=variant, channel_slug=channel.slug)
- return None
-
- return (
- ProductChannelListingByProductIdAndChannelSlugLoader(context)
- .load((variant.product_id, channel.slug))
- .then(product_is_available)
- )
-
- variant = ProductVariantByIdLoader(context).load(root.variant_id)
- channel = channel_dataloader.load(root.id)
-
- return Promise.all([variant, channel]).then(requestor_has_access_to_variant)