Skip to content

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

Fix resolving order by non-existing external reference (#13466)

Marcin Gębala7691de1ea22e
diff --git a/saleor/graphql/core/utils/__init__.py b/saleor/graphql/core/utils/__init__.py
index c1219941c3..ea31889310 100644
--- a/saleor/graphql/core/utils/__init__.py
+++ b/saleor/graphql/core/utils/__init__.py
@@ -122,7 +122,7 @@ def raise_validation_error(field=None, message=None, code=None):
def ext_ref_to_global_id_or_error(model, external_reference):
- """Convert external reference to graphen global id."""
+ """Convert external reference to global id."""
internal_id = (
model.objects.filter(external_reference=external_reference)
.values_list("id", flat=True)
diff --git a/saleor/graphql/order/schema.py b/saleor/graphql/order/schema.py
index 20a98044ec..ae749abf29 100644
--- a/saleor/graphql/order/schema.py
+++ b/saleor/graphql/order/schema.py
@@ -1,6 +1,7 @@
from typing import List, Optional
import graphene
+from django.core.exceptions import ValidationError
from graphql import GraphQLError
from ...order import models
@@ -161,7 +162,10 @@ class OrderQueries(graphene.ObjectType):
"id", id, "external_reference", external_reference
)
if not id:
- id = ext_ref_to_global_id_or_error(models.Order, external_reference)
+ try:
+ id = ext_ref_to_global_id_or_error(models.Order, external_reference)
+ except ValidationError:
+ return None
_, id = from_global_id_or_error(id, Order)
return resolve_order(info, id)
diff --git a/saleor/graphql/order/tests/queries/test_order.py b/saleor/graphql/order/tests/queries/test_order.py
index dab43f53d6..7f91a499c2 100644
--- a/saleor/graphql/order/tests/queries/test_order.py
+++ b/saleor/graphql/order/tests/queries/test_order.py
@@ -1125,6 +1125,24 @@ def test_query_order_by_external_reference(user_api_client, order):
assert data["id"] == graphene.Node.to_global_id("Order", order.id)
[email protected]("external_reference", ['" "', "not-existing"])
+def test_query_order_by_not_existing_external_reference(
+ external_reference, user_api_client, order
+):
+ # given
+ query = QUERY_ORDER_BY_EXTERNAL_REFERENCE
+ order.external_reference = "test-ext-ref"
+ order.save(update_fields=["external_reference"])
+ variables = {"externalReference": external_reference}
+
+ # when
+ response = user_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+
+ # then
+ assert content["data"]["order"] is None
+
+
def test_query_order_by_external_reference_and_id(user_api_client, order):
# given
query = QUERY_ORDER_BY_EXTERNAL_REFERENCE