Skip to content

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

Use dataloader in checkout resolver (#17739)

Maciej Korycinskibd11af3ea0a4
diff --git a/saleor/graphql/checkout/resolvers.py b/saleor/graphql/checkout/resolvers.py
index f96cb6c44a..c6daa56fc8 100644
--- a/saleor/graphql/checkout/resolvers.py
+++ b/saleor/graphql/checkout/resolvers.py
@@ -1,3 +1,5 @@
+from uuid import UUID
+
from ...checkout import models
from ...core.exceptions import PermissionDenied
from ...permission.enums import (
@@ -5,11 +7,13 @@ from ...permission.enums import (
CheckoutPermissions,
PaymentPermissions,
)
+from ..channel.dataloaders import ChannelByIdLoader
from ..core.context import SyncWebhookControlContext, get_database_connection_name
from ..core.tracing import traced_resolver
from ..core.utils import from_global_id_or_error
from ..core.validators import validate_one_of_args_is_in_query
from ..utils import get_user_or_app_from_context
+from .dataloaders.models import CheckoutByTokenLoader
def resolve_checkout_lines(info):
@@ -33,30 +37,47 @@ def resolve_checkout(info, token, id):
validate_one_of_args_is_in_query("id", id, "token", token)
if id:
- _, token = from_global_id_or_error(id, only_type="Checkout")
- checkout = (
- models.Checkout.objects.using(get_database_connection_name(info.context))
- .filter(token=token)
- .first()
- )
- if checkout is None:
- return None
- # always return checkout for active channel
- if checkout.channel.is_active:
- return SyncWebhookControlContext(node=checkout, allow_sync_webhooks=True)
-
- # resolve checkout for staff or app
- if requester := get_user_or_app_from_context(info.context):
- has_manage_checkout = requester.has_perm(CheckoutPermissions.MANAGE_CHECKOUTS)
- has_impersonate_user = requester.has_perm(AccountPermissions.IMPERSONATE_USER)
- has_handle_payments = requester.has_perm(PaymentPermissions.HANDLE_PAYMENTS)
- if has_manage_checkout or has_impersonate_user or has_handle_payments:
- return SyncWebhookControlContext(node=checkout, allow_sync_webhooks=True)
-
- raise PermissionDenied(
- permissions=[
- CheckoutPermissions.MANAGE_CHECKOUTS,
- AccountPermissions.IMPERSONATE_USER,
- PaymentPermissions.HANDLE_PAYMENTS,
- ]
- )
+ _, token = from_global_id_or_error(id, only_type="Checkout", raise_error=True)
+ token = UUID(token)
+
+ def with_checkout(checkout):
+ if checkout is None:
+ return None
+
+ def _with_channel(channel):
+ # always return checkout for active channel
+ if channel.is_active:
+ return SyncWebhookControlContext(
+ node=checkout, allow_sync_webhooks=True
+ )
+ # resolve checkout for staff or app
+ if requester := get_user_or_app_from_context(info.context):
+ has_manage_checkout = requester.has_perm(
+ CheckoutPermissions.MANAGE_CHECKOUTS
+ )
+ has_impersonate_user = requester.has_perm(
+ AccountPermissions.IMPERSONATE_USER
+ )
+ has_handle_payments = requester.has_perm(
+ PaymentPermissions.HANDLE_PAYMENTS
+ )
+ if has_manage_checkout or has_impersonate_user or has_handle_payments:
+ return SyncWebhookControlContext(
+ node=checkout, allow_sync_webhooks=True
+ )
+
+ raise PermissionDenied(
+ permissions=[
+ CheckoutPermissions.MANAGE_CHECKOUTS,
+ AccountPermissions.IMPERSONATE_USER,
+ PaymentPermissions.HANDLE_PAYMENTS,
+ ]
+ )
+
+ return (
+ ChannelByIdLoader(info.context)
+ .load(checkout.channel_id)
+ .then(_with_channel)
+ )
+
+ return CheckoutByTokenLoader(info.context).load(token).then(with_checkout)
diff --git a/saleor/graphql/checkout/tests/benchmark/test_checkout.py b/saleor/graphql/checkout/tests/benchmark/test_checkout.py
new file mode 100644
index 0000000000..677b3ebeb7
--- /dev/null
+++ b/saleor/graphql/checkout/tests/benchmark/test_checkout.py
@@ -0,0 +1,74 @@
+import pytest
+
+from ....core.utils import to_global_id_or_none
+from ....tests.utils import get_graphql_content
+
+CHECKOUT_DETAILS_QUERY = """
+query checkout($id: ID){
+ checkout(
+ id:$id
+ ) {
+ shippingMethods{
+ active
+ message
+ }
+ lines{
+ id
+ variant{
+ product{
+ name
+ }
+ }
+ totalPrice{
+ gross{
+ amount
+ }
+ net{
+ amount
+ }
+ }
+ }
+ totalPrice{
+ gross{
+ amount
+ }
+ }
+ subtotalPrice{
+ gross{
+ amount
+ }
+ }
+ shippingPrice{
+ net{
+ amount
+ }
+ gross{
+ amount
+ }
+ }
+
+ }
+}
+"""
+
+
[email protected]_queries(autouse=False)
+def test_user_checkout_details(
+ user_api_client,
+ checkouts_for_benchmarks,
+ count_queries,
+):
+ # given
+ checkout = checkouts_for_benchmarks[0]
+ checkout_id = to_global_id_or_none(checkout)
+
+ # when
+ content = get_graphql_content(
+ user_api_client.post_graphql(
+ CHECKOUT_DETAILS_QUERY, variables={"id": checkout_id}
+ )
+ )
+
+ # then
+ assert content["data"]["checkout"] is not None