Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
feat(checkout): make lines optional in checkoutCreate (#19476)
Lukasz Ostrowski5614db7b96af
diff --git a/AGENTS.md b/AGENTS.mdindex bdd944c779..d63acf54b2 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -52,6 +52,20 @@ database/cache or collide with another worktree's stack.- When testing graphQL cases with expected errors ALWAYS assert expected length of errors list- When setting up test data, extract values into variables and reuse them in assertions. Do not repeat literal values between setup and assertion — use the variable instead.- When comparing JSON payloads in tests, use `json.loads()` to compare dicts instead of comparing serialized strings with `json.dumps()`. String comparison breaks when key order changes.+- When a test expects exactly one row, use `Model.objects.get()` instead of `.first()` followed by an `is not None` assertion. `get()` both fetches the object and asserts that exactly one exists, so drop the separate not-None check.+- For presence/absence checks prefer `qs.exists()` over `qs.count() == 0` / `!= 0`. `exists()` is cheaper than a `COUNT`. Example: `assert checkout.lines.exists() is False`.+- Use `@pytest.mark.parametrize` for variations of the same scenario instead of copy/pasting near-identical test bodies. Label each case with a leading `_case` string parameter rather than the `ids=` argument — it keeps the description next to the data and is easier to read and edit:+ ```python+ @pytest.mark.parametrize(+ ("_case", "value"),+ [+ ("omitted", {}),+ ("empty_list", {"lines": []}),+ ],+ )+ def test_something(_case, value): ...+ ```+- When a mutation input is optional/nullable, test the explicit `null` value in addition to omitting the field — they are distinct inputs and can be handled differently.# Webhooks and Eventsdiff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4f7dcc5faf..648569245d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -21,6 +21,7 @@ All notable, unreleased changes to this project will be documented in this file.### GraphQL API- Added `stockAvailability` and `stocks` filters to the `productVariants` query `where` input, allowing variants to be filtered by their stock status and stock quantity for a given channel - #17689 by @ayesha-waris+- `lines` input on the `checkoutCreate` mutation is no longer required. When omitted, a checkout with no lines is created.### Webhooksdiff --git a/saleor/graphql/AGENTS.md b/saleor/graphql/AGENTS.mdindex 69ffef0ea5..2f517db2bf 100644--- a/saleor/graphql/AGENTS.md+++ b/saleor/graphql/AGENTS.md@@ -33,6 +33,8 @@ def resolve_parent(root: models.Category, info):**Write exhaustive GraphQL descriptions on fields, including limits and behavior**+**Do not rely on `default_value` for optional list inputs.** In the graphene version used here `default_value` is passed to the resolver as a literal, not a factory — `default_value=[]` shares one mutable list instance across all requests, and `default_value=list` passes the `list` class itself (not an empty list). Prefer `required=False` without a default and normalize the absent/`None` case in the resolver (e.g. `lines = data.pop("lines", None) or []`).+# Input validationFor complex input shapes prefer using Pydantic model. Ensure Pydantic errors are mapped to Django ValidationError using `pydantic_to_validation_error` from `saleor/graphql/error.py`:diff --git a/saleor/graphql/checkout/mutations/checkout_create.py b/saleor/graphql/checkout/mutations/checkout_create.pyindex 6a1889a2a5..cfbed76724 100644--- a/saleor/graphql/checkout/mutations/checkout_create.py+++ b/saleor/graphql/checkout/mutations/checkout_create.py@@ -136,9 +136,10 @@ class CheckoutCreateInput(BaseInputObjectType):CheckoutLineInput,description=("A list of checkout lines, each containing information about "- "an item in the checkout."+ "an item in the checkout. When omitted, a checkout with no lines "+ "is created."),- required=True,+ required=False,)email = graphene.String(description="The customer's email address.")save_shipping_address = graphene.Boolean(diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_create.py b/saleor/graphql/checkout/tests/mutations/test_checkout_create.pyindex 3247d8bd6e..ccbdd05ff3 100644--- a/saleor/graphql/checkout/tests/mutations/test_checkout_create.py+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_create.py@@ -626,6 +626,40 @@ def test_checkout_create(api_client, stock, graphql_address_data, channel_USD):assert new_checkout.shipping_address.validation_skipped is False+ ("_case", "lines_input"),+ [+ ("omitted", {}),+ ("empty_list", {"lines": []}),+ ("explicit_null", {"lines": None}),+ ],+)+def test_checkout_create_without_lines(_case, lines_input, api_client, channel_USD):+ """Create checkout when lines are omitted, empty, or null - no lines are created."""+ # given+ variables = {+ "checkoutInput": {+ "channel": channel_USD.slug,+ **lines_input,+ }+ }+ assert not Checkout.objects.exists()++ # when+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)++ # then+ content = get_graphql_content(response)["data"]["checkoutCreate"]+ assert content["errors"] == []++ new_checkout = Checkout.objects.get()+ assert new_checkout.lines.exists() is False+ checkout_data = content["checkout"]+ assert checkout_data["token"] == str(new_checkout.token)+ assert checkout_data["lines"] == []+ assert checkout_data["quantity"] == 0++def test_checkout_create_with_custom_price(app_api_client,stock,diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 1a59d05f0c..2e78b954dc 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -30577,9 +30577,9 @@ input CheckoutCreateInput @doc(category: "Checkout") {channel: String"""- A list of checkout lines, each containing information about an item in the checkout.+ A list of checkout lines, each containing information about an item in the checkout. When omitted, a checkout with no lines is created."""- lines: [CheckoutLineInput!]!+ lines: [CheckoutLineInput!]"""The customer's email address."""email: String