Skip to content

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

Fix query cost - fragment complexity not multiplied recursively (#12682)

Przemysław Łada7f2a3f9dcd7b
diff --git a/saleor/graphql/core/tests/test_query_cost_validation.py b/saleor/graphql/core/tests/test_query_cost_validation.py
index 462b8ccf5c..b53966b664 100644
--- a/saleor/graphql/core/tests/test_query_cost_validation.py
+++ b/saleor/graphql/core/tests/test_query_cost_validation.py
@@ -1,4 +1,5 @@
import graphene
+import pytest
from django.test import override_settings
@@ -131,3 +132,112 @@ def test_query_below_cost_limit_with_multiplied_complexity_passes_validation(
query_cost = json_response["extensions"]["cost"]["requestedQueryCost"]
assert query_cost == 5
assert len(json_response["data"]) == 1
+
+
+PRODUCTS_QUERY = """
+query productsQueryCost($channel: String, $first: Int) {
+ products(channel: $channel, first: $first) {
+ edges {
+ node {
+ id
+ category{
+ products(channel: $channel, first: $first) {
+ edges{
+ node{
+ id
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+"""
+
+PRODUCTS_QUERY_WITH_INLINE_FRAGMENT = """
+query productsQueryCost($channel: String, $first: Int) {
+ products(channel: $channel, first: $first) {
+ edges {
+ node {
+ id
+ category {
+ ... on Category {
+ products(channel: $channel, first: $first) {
+ edges {
+ node {
+ id
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+"""
+
+PRODUCTS_QUERY_WITH_FRAGMENT = """
+fragment category on Category {
+ products(channel: $channel, first: $first) {
+ edges {
+ node {
+ id
+ }
+ }
+ }
+}
+
+query productsQueryCost($channel: String, $first: Int) {
+ products(channel: $channel, first: $first) {
+ edges {
+ node {
+ id
+ category {
+ ...category
+ }
+ }
+ }
+ }
+}
+"""
+
+
+ "query",
+ [
+ PRODUCTS_QUERY,
+ PRODUCTS_QUERY_WITH_INLINE_FRAGMENT,
+ PRODUCTS_QUERY_WITH_FRAGMENT,
+ ],
+)
+@override_settings(GRAPHQL_QUERY_MAX_COMPLEXITY=100000)
+def test_query_with_fragments_have_same_multiplied_complexity_cost(
+ query, api_client, product, channel_USD
+):
+ # given
+ variables = {"channel": channel_USD.slug, "first": 10}
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ # when
+ response = api_client.post_graphql(query, variables)
+ json_response = response.json()
+ # then
+ assert "errors" not in json_response
+ expected_data = {
+ "products": {
+ "edges": [
+ {
+ "node": {
+ "id": product_id,
+ "category": {
+ "products": {"edges": [{"node": {"id": product_id}}]}
+ },
+ }
+ }
+ ]
+ }
+ }
+ assert json_response["data"] == expected_data
+ query_cost = json_response["extensions"]["cost"]["requestedQueryCost"]
+ assert query_cost == 120
diff --git a/saleor/graphql/core/validators/query_cost.py b/saleor/graphql/core/validators/query_cost.py
index d08c7c65f9..5b6f55610f 100644
--- a/saleor/graphql/core/validators/query_cost.py
+++ b/saleor/graphql/core/validators/query_cost.py
@@ -112,14 +112,18 @@ class CostValidator(ValidationRule):
fragment_type = self.context.get_schema().get_type(
fragment.type_condition.name.value
)
- node_cost = self.compute_node_cost(fragment, fragment_type)
+ node_cost = self.compute_node_cost(
+ fragment, fragment_type, self.operation_multipliers
+ )
if isinstance(child_node, InlineFragment):
inline_fragment_type = type_def
if child_node.type_condition and child_node.type_condition.name:
inline_fragment_type = self.context.get_schema().get_type(
child_node.type_condition.name.value
)
- node_cost = self.compute_node_cost(child_node, inline_fragment_type)
+ node_cost = self.compute_node_cost(
+ child_node, inline_fragment_type, self.operation_multipliers
+ )
total += node_cost
return total