Skip to content

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

Introduce positive integer (#18118)

Iga Karbowiake83c3935bf85
diff --git a/saleor/graphql/core/scalars.py b/saleor/graphql/core/scalars.py
index 8e8b216654..df029207f3 100644
--- a/saleor/graphql/core/scalars.py
+++ b/saleor/graphql/core/scalars.py
@@ -209,3 +209,28 @@ class Hour(graphene.Int):
class Day(graphene.Int):
"""The `Day` scalar type represents number of days by integer value."""
+
+
+class PositiveInt(graphene.Int):
+ """Positive Integer scalar implementation.
+
+ Should be used in places where value must be positive (greater than 0).
+ """
+
+ @staticmethod
+ def parse_value(value) -> int | None:
+ parsed_value = graphene.Int.parse_value(value)
+
+ if (parsed_value is not None) and parsed_value > 0:
+ return parsed_value
+
+ return None
+
+ @staticmethod
+ def parse_literal(node) -> int | None:
+ parsed_value = graphene.Int.parse_literal(node)
+
+ if (parsed_value is not None) and parsed_value > 0:
+ return parsed_value
+
+ return None
diff --git a/saleor/graphql/core/tests/test_scalars_decimals.py b/saleor/graphql/core/tests/test_scalars_decimals.py
index 6c5f6cf2a6..005551f084 100644
--- a/saleor/graphql/core/tests/test_scalars_decimals.py
+++ b/saleor/graphql/core/tests/test_scalars_decimals.py
@@ -3,7 +3,7 @@ import decimal
import pytest
from graphql.language.ast import FloatValue, IntValue, ObjectValue, StringValue
-from ..scalars import Decimal, PositiveDecimal
+from ..scalars import Decimal, PositiveDecimal, PositiveInt
# Decimals
@@ -92,3 +92,52 @@ def test_positive_decimal_scalar_invalid_literal(node):
result = PositiveDecimal.parse_literal(node)
assert result is None
+
+
+# PositiveInt
+
+
+ "valid_node",
+ [
+ IntValue(value="1"),
+ IntValue(value="10"),
+ IntValue(value="999"),
+ ],
+)
+def test_positive_int_scalar_valid_literal(valid_node):
+ result = PositiveInt.parse_literal(valid_node)
+ assert result == int(valid_node.value)
+
+
+ "invalid_node",
+ [
+ IntValue(value="0"),
+ IntValue(value="-1"),
+ FloatValue(value="1.0"),
+ StringValue(value="10"),
+ ObjectValue(fields=[]),
+ ],
+)
+def test_positive_int_scalar_invalid_literal(invalid_node):
+ result = PositiveInt.parse_literal(invalid_node)
+ assert result is None
+
+
+ "valid_value",
+ ["1", "10", "999"],
+)
+def test_positive_int_scalar_valid_value(valid_value):
+ result = PositiveInt.parse_value(valid_value)
+ assert result == int(valid_value)
+
+
+ "invalid_value",
+ ["0", "-1", "NaN", "abc"],
+)
+def test_positive_int_scalar_invalid_value(invalid_value):
+ result = PositiveInt.parse_value(invalid_value)
+ assert result is None
diff --git a/saleor/settings.py b/saleor/settings.py
index 6b663ac65c..3f78fe0c14 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -926,8 +926,6 @@ TRANSACTION_BATCH_FOR_RELEASING_FUNDS = os.environ.get(
"TRANSACTION_BATCH_FOR_RELEASING_FUNDS", 60
)
-NESTED_QUERY_LIMIT = int(os.environ.get("NESTED_QUERY_LIMIT", 100))
-
# The maximum SearchVector expression count allowed per index SQL statement
# If the count is exceeded, the expression list will be truncated