Skip to content

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

Add query contains only schema validation (#6827)

Filip Owczareke4f16b6f82d2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7464fc4886..8418ffcff7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
All notable, unreleased changes to this project will be documented in this file. For the released changes, please visit the [Releases](https://github.com/mirumee/saleor/releases) page.
## [Unreleased]
+- Add query contains only schema validation - #6827 by @fowczarek
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --git a/saleor/graphql/core/tests/test_view.py b/saleor/graphql/core/tests/test_view.py
index 0f682d730a..239b48942f 100644
--- a/saleor/graphql/core/tests/test_view.py
+++ b/saleor/graphql/core/tests/test_view.py
@@ -309,3 +309,27 @@ def test_example_query(api_client, product):
response = api_client.post_graphql(EXAMPLE_QUERY)
content = get_graphql_content(response)
assert content["data"]["products"]["edges"][0]["node"]["name"] == product.name
+
+
+ "other_query",
+ ["me{email}", 'products(first:5,channel:"channel"){edges{node{name}}}'],
+)
+def test_query_contains_not_only_schema_raise_error(
+ other_query, api_client, graphql_log_handler
+):
+ query = """
+ query IntrospectionQuery {
+ %(other_query)s
+ __schema {
+ queryType {
+ name
+ }
+ }
+ }
+ """
+ response = api_client.post_graphql(query % {"other_query": other_query})
+ assert response.status_code == 400
+ assert graphql_log_handler.messages == [
+ "saleor.graphql.errors.handled[INFO].GraphQLError"
+ ]
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.py
index 8c404dca31..c339d914c1 100644
--- a/saleor/graphql/views.py
+++ b/saleor/graphql/views.py
@@ -212,6 +212,16 @@ class GraphQLView(View):
except (ValueError, GraphQLSyntaxError) as e:
return None, ExecutionResult(errors=[e], invalid=True)
+ def check_if_query_contains_only_schema(self, document: GraphQLDocument):
+ for definition in document.document_ast.definitions:
+ selections = definition.selection_set.selections
+ if len(selections) > 1:
+ for selection in selections:
+ selection_name = str(selection.name.value)
+ if selection_name == "__schema":
+ msg = "`__schema` must be fetched in separete query"
+ raise GraphQLError(msg)
+
def execute_graphql_request(self, request: HttpRequest, data: dict):
with opentracing.global_tracer().start_active_span("graphql_query") as scope:
span = scope.span
@@ -226,6 +236,10 @@ class GraphQLView(View):
if document is not None:
raw_query_string = document.document_string
span.set_tag("graphql.query", raw_query_string)
+ try:
+ self.check_if_query_contains_only_schema(document)
+ except GraphQLError as e:
+ return ExecutionResult(errors=[e], invalid=True)
extra_options: Dict[str, Optional[Any]] = {}