Skip to content

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

Remove external shipping methods call when resolving delivery method (#9049)

Krzysztof Kulig9b12f92e4b25
diff --git a/saleor/graphql/checkout/tests/benchmark/test_homepage.py b/saleor/graphql/checkout/tests/benchmark/test_homepage.py
index bac90cf9bb..6e75a4d084 100644
--- a/saleor/graphql/checkout/tests/benchmark/test_homepage.py
+++ b/saleor/graphql/checkout/tests/benchmark/test_homepage.py
@@ -1,5 +1,8 @@
+from unittest.mock import patch
+
import pytest
+from .....checkout.utils import set_external_shipping_id
from ....tests.utils import get_graphql_content
@@ -139,3 +142,54 @@ def test_user_checkout_details(user_api_client, customer_checkout, count_queries
}
"""
get_graphql_content(user_api_client.post_graphql(query))
+
+
+@patch("saleor.plugins.webhook.tasks.send_webhook_request_sync")
+def test_user_checkout_details_with_external_shipping_method(
+ mock_send_request,
+ user_api_client,
+ customer_checkout,
+ shipping_app,
+ settings,
+):
+ # given
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ external_id = "abcd"
+ mock_json_response = [
+ {
+ "id": external_id,
+ "name": "Provider - Economy",
+ "amount": "10",
+ "currency": "USD",
+ "maximum_delivery_days": "7",
+ }
+ ]
+ checkout = customer_checkout
+ checkout.shipping_method = None
+ set_external_shipping_id(checkout, external_id)
+ checkout.save()
+ mock_send_request.return_value = mock_json_response
+ query = """
+ query {
+ me {
+ checkout {
+ id
+ deliveryMethod {
+ __typename
+ }
+ shippingMethod {
+ __typename
+ }
+ shippingMethods {
+ id
+ }
+ }
+ }
+ }
+ """
+
+ # when
+ get_graphql_content(user_api_client.post_graphql(query))
+
+ # then
+ assert mock_send_request.call_count == 1
diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.py
index 951e0b696d..f9b913d56e 100644
--- a/saleor/graphql/checkout/types.py
+++ b/saleor/graphql/checkout/types.py
@@ -2,16 +2,12 @@ import graphene
from promise import Promise
from ...checkout import calculations, models
-from ...checkout.utils import (
- get_external_shipping_id,
- get_valid_collection_points_for_checkout,
-)
+from ...checkout.utils import get_valid_collection_points_for_checkout
from ...core.exceptions import PermissionDenied
from ...core.permissions import AccountPermissions
from ...core.taxes import zero_taxed_money
from ...core.tracing import traced_resolver
from ...shipping.interface import ShippingMethodData
-from ...shipping.utils import convert_to_shipping_method_data
from ...warehouse import models as warehouse_models
from ...warehouse.reservations import is_reservation_enabled
from ..account.dataloaders import AddressByIdLoader
@@ -33,16 +29,9 @@ from ..product.dataloaders import (
ProductTypeByVariantIdLoader,
ProductVariantByIdLoader,
)
-from ..shipping.dataloaders import (
- ShippingMethodByIdLoader,
- ShippingMethodChannelListingByChannelSlugLoader,
-)
from ..shipping.types import ShippingMethod
from ..utils import get_user_or_app_from_context
-from ..warehouse.dataloaders import (
- StocksReservationsByCheckoutTokenLoader,
- WarehouseByIdLoader,
-)
+from ..warehouse.dataloaders import StocksReservationsByCheckoutTokenLoader
from ..warehouse.types import Warehouse
from .dataloaders import (
CheckoutByTokenLoader,
@@ -315,46 +304,18 @@ class Checkout(ModelObjectType):
@classmethod
def resolve_shipping_method(cls, root: models.Checkout, info):
- external_app_shipping_id = get_external_shipping_id(root)
-
- if external_app_shipping_id:
- shipping_method = info.context.plugins.get_shipping_method(
- checkout=root,
- channel_slug=root.channel.slug,
- shipping_method_id=external_app_shipping_id,
- )
- if shipping_method:
- return shipping_method
-
- if not root.shipping_method_id:
- return None
-
- def with_shipping_method_and_channel(data):
- shipping_method, channel = data
-
- def process_listing(listings):
- for listing in listings:
- if listing.shipping_method_id == shipping_method.id:
- shipping_method_channel_listing = listing
- break
+ def with_checkout_info(checkout_info):
+ delivery_method = checkout_info.delivery_method_info.delivery_method
+ if not delivery_method or not isinstance(
+ delivery_method, ShippingMethodData
+ ):
+ return
+ return delivery_method
- return convert_to_shipping_method_data(
- shipping_method, shipping_method_channel_listing # type: ignore
- )
-
- return (
- ShippingMethodChannelListingByChannelSlugLoader(info.context)
- .load(channel.slug)
- .then(process_listing)
- )
-
- shipping_method = ShippingMethodByIdLoader(info.context).load(
- root.shipping_method_id
- )
- channel = ChannelByIdLoader(info.context).load(root.channel_id)
-
- return Promise.all([shipping_method, channel]).then(
- with_shipping_method_and_channel
+ return (
+ CheckoutInfoByCheckoutTokenLoader(info.context)
+ .load(root.token)
+ .then(with_checkout_info)
)
@classmethod
@@ -364,12 +325,13 @@ class Checkout(ModelObjectType):
@staticmethod
def resolve_delivery_method(root: models.Checkout, info):
- external_app_shipping_id = get_external_shipping_id(root)
- if root.shipping_method_id or external_app_shipping_id:
- return Checkout.resolve_shipping_method(root, info)
- if root.collection_point_id:
- return WarehouseByIdLoader(info.context).load(root.collection_point_id)
- return None
+ return (
+ CheckoutInfoByCheckoutTokenLoader(info.context)
+ .load(root.token)
+ .then(
+ lambda checkout_info: checkout_info.delivery_method_info.delivery_method
+ )
+ )
@staticmethod
def resolve_quantity(root: models.Checkout, info):