Skip to content

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

add `checkouts` connection to `User` type (#10862)

Piotr Zabieglikd0b8586ab0c8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8758981288..2f0ed382eb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Deprecated `slug` in filter for `menus`. Use `slugs` instead
- Add ability to filter payments by list of ids. #10821 by @kadewu
- Add ability to filter customers by ids. #10694 by @kadewu
+- Add `User.checkouts` field. #10862 by @zedzior
- Add optional field `audience` to mutation `tokenCreate`. If provided, the created tokens will have key `aud` with value: `custom:{audience-input-value}` - #10845 by @korycins
- Use `AttributeValue.name` instead of `AttributeValue.slug` to determine uniqueness of a value instance for dropdown and multiselect attributes. - #10881 by @jakubkuc
- Add ability to pass metadata directly in create/update mutations for product app models - #10689 by @SzymJ
diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.py
index fa7107960c..4ca277e402 100644
--- a/saleor/graphql/account/tests/test_account.py
+++ b/saleor/graphql/account/tests/test_account.py
@@ -205,6 +205,13 @@ FULL_USER_QUERY = """
}
}
}
+ checkouts(first: 10) {
+ edges {
+ node {
+ id
+ }
+ }
+ }
}
}
"""
@@ -304,8 +311,10 @@ def test_query_customer_user(
assert data["giftCards"]["edges"][0]["node"]["id"] == graphene.Node.to_global_id(
"GiftCard", gift_card_used.pk
)
-
assert data["checkoutIds"] == [to_global_id_or_none(checkout)]
+ assert data["checkouts"]["edges"][0]["node"]["id"] == graphene.Node.to_global_id(
+ "Checkout", checkout.pk
+ )
def test_query_customer_user_with_orders(
@@ -1045,6 +1054,14 @@ ME_QUERY = """
code
name
}
+ checkouts(first: 10) {
+ edges {
+ node {
+ id
+ }
+ }
+ totalCount
+ }
}
}
"""
@@ -1112,6 +1129,9 @@ def test_me_query_checkout(user_api_client, checkout):
content = get_graphql_content(response)
data = content["data"]["me"]
assert data["checkout"]["token"] == str(checkout.token)
+ assert data["checkouts"]["edges"][0]["node"]["id"] == graphene.Node.to_global_id(
+ "Checkout", checkout.pk
+ )
def test_me_query_checkout_with_inactive_channel(user_api_client, checkout):
@@ -1126,6 +1146,42 @@ def test_me_query_checkout_with_inactive_channel(user_api_client, checkout):
content = get_graphql_content(response)
data = content["data"]["me"]
assert not data["checkout"]
+ assert not data["checkouts"]["edges"]
+
+
+def test_me_query_checkouts_with_channel(user_api_client, checkout, checkout_JPY):
+ query = """
+ query Me($channel: String) {
+ me {
+ checkouts(first: 10, channel: $channel) {
+ edges {
+ node {
+ id
+ channel {
+ slug
+ }
+ }
+ }
+ totalCount
+ }
+ }
+ }
+ """
+
+ user = user_api_client.user
+ checkout.user = checkout_JPY.user = user
+ checkout.save()
+ checkout_JPY.save()
+
+ response = user_api_client.post_graphql(query, {"channel": checkout.channel.slug})
+
+ content = get_graphql_content(response)
+ data = content["data"]["me"]["checkouts"]
+ assert data["edges"][0]["node"]["id"] == graphene.Node.to_global_id(
+ "Checkout", checkout.pk
+ )
+ assert data["totalCount"] == 1
+ assert data["edges"][0]["node"]["channel"]["slug"] == checkout.channel.slug
QUERY_ME_CHECKOUT_TOKENS = """
diff --git a/saleor/graphql/account/types.py b/saleor/graphql/account/types.py
index cc54d897e2..2944269f8b 100644
--- a/saleor/graphql/account/types.py
+++ b/saleor/graphql/account/types.py
@@ -22,9 +22,9 @@ from ..account.utils import check_is_owner_or_has_one_of_perms
from ..app.dataloaders import AppByIdLoader
from ..app.types import App
from ..checkout.dataloaders import CheckoutByUserAndChannelLoader, CheckoutByUserLoader
-from ..checkout.types import Checkout
+from ..checkout.types import Checkout, CheckoutCountableConnection
from ..core.connection import CountableConnection, create_connection_slice
-from ..core.descriptions import DEPRECATED_IN_3X_FIELD
+from ..core.descriptions import ADDED_IN_38, DEPRECATED_IN_3X_FIELD
from ..core.enums import LanguageCodeEnum
from ..core.federation import federated_entity, resolve_federation_references
from ..core.fields import ConnectionField, PermissionsField
@@ -271,6 +271,13 @@ class User(ModelObjectType):
description="Slug of a channel for which the data should be returned."
),
)
+ checkouts = ConnectionField(
+ CheckoutCountableConnection,
+ description="Returns checkouts assigned to this user." + ADDED_IN_38,
+ channel=graphene.String(
+ description="Slug of a channel for which the data should be returned."
+ ),
+ )
gift_cards = ConnectionField(
"saleor.graphql.giftcard.types.GiftCardCountableConnection",
description="List of the user gift cards.",
@@ -381,6 +388,21 @@ class User(ModelObjectType):
.then(return_checkout_ids)
)
+ @staticmethod
+ def resolve_checkouts(root: models.User, info, **kwargs):
+ def _resolve_checkouts(checkouts):
+ return create_connection_slice(
+ checkouts, info, kwargs, CheckoutCountableConnection
+ )
+
+ if channel := kwargs.get("channel"):
+ return (
+ CheckoutByUserAndChannelLoader(info.context)
+ .load((root.id, channel))
+ .then(_resolve_checkouts)
+ )
+ return CheckoutByUserLoader(info.context).load(root.id).then(_resolve_checkouts)
+
@staticmethod
def resolve_gift_cards(root: models.User, info, **kwargs):
from ..giftcard.types import GiftCardCountableConnection
diff --git a/saleor/graphql/checkout/dataloaders.py b/saleor/graphql/checkout/dataloaders.py
index c0db8a926d..7489bd4cbd 100644
--- a/saleor/graphql/checkout/dataloaders.py
+++ b/saleor/graphql/checkout/dataloaders.py
@@ -170,7 +170,7 @@ class CheckoutByUserLoader(DataLoader):
checkout_by_user_map = defaultdict(list)
for checkout in checkouts:
checkout_by_user_map[checkout.user_id].append(checkout)
- return [checkout_by_user_map.get(user_id) for user_id in keys]
+ return [checkout_by_user_map[user_id] for user_id in keys]
class CheckoutByUserAndChannelLoader(DataLoader):
@@ -192,7 +192,7 @@ class CheckoutByUserAndChannelLoader(DataLoader):
for checkout in checkouts:
key = (checkout.user_id, checkout.channel_slug)
checkout_by_user_and_channel_map[key].append(checkout)
- return [checkout_by_user_and_channel_map.get(key) for key in keys]
+ return [checkout_by_user_and_channel_map[key] for key in keys]
class CheckoutInfoByCheckoutTokenLoader(DataLoader):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 86abb2363a..c4a50b41e3 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -7697,6 +7697,28 @@ type User implements Node & ObjectWithMetadata {
channel: String
): [ID!]
+ """
+ Returns checkouts assigned to this user.
+
+ Added in Saleor 3.8.
+ """
+ checkouts(
+ """Slug of a channel for which the data should be returned."""
+ channel: String
+
+ """Return the elements in the list that come before the specified cursor."""
+ before: String
+
+ """Return the elements in the list that come after the specified cursor."""
+ after: String
+
+ """Return the first n elements from the list."""
+ first: Int
+
+ """Return the last n elements from the list."""
+ last: Int
+ ): CheckoutCountableConnection
+
"""List of the user gift cards."""
giftCards(
"""Return the elements in the list that come before the specified cursor."""
@@ -9489,6 +9511,23 @@ type TransactionEvent implements Node {
name: String
}
+type CheckoutCountableConnection {
+ """Pagination data for this connection."""
+ pageInfo: PageInfo!
+ edges: [CheckoutCountableEdge!]!
+
+ """A total count of items in the collection."""
+ totalCount: Int
+}
+
+type CheckoutCountableEdge {
+ """The item at the end of the edge."""
+ node: Checkout!
+
+ """A cursor for use in pagination."""
+ cursor: String!
+}
+
type GiftCardCountableConnection {
"""Pagination data for this connection."""
pageInfo: PageInfo!
@@ -10571,23 +10610,6 @@ enum ExportFileSortField {
LAST_MODIFIED_AT
}
-type CheckoutCountableConnection {
- """Pagination data for this connection."""
- pageInfo: PageInfo!
- edges: [CheckoutCountableEdge!]!
-
- """A total count of items in the collection."""
- totalCount: Int
-}
-
-type CheckoutCountableEdge {
- """The item at the end of the edge."""
- node: Checkout!
-
- """A cursor for use in pagination."""
- cursor: String!
-}
-
input CheckoutSortingInput {
"""Specifies the direction in which to sort products."""
direction: OrderDirection!