Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add introspection caching (#6871)
Filip Owczarek4d4be8d444e2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e020308ff7..8a40643b75 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,7 @@ All notable, unreleased changes to this project will be documented in this file.## [Unreleased]- Add query contains only schema validation - #6827 by @fowczarek+- Add introspection caching - #6871 by @fowczarek### Breaking- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysockidiff --git a/saleor/graphql/core/tests/test_view.py b/saleor/graphql/core/tests/test_view.pyindex 239b48942f..ca2c943481 100644--- a/saleor/graphql/core/tests/test_view.py+++ b/saleor/graphql/core/tests/test_view.py@@ -3,7 +3,9 @@ from unittest import mockimport grapheneimport pytestfrom django.test import override_settings+from graphql.execution.base import ExecutionResult+from .... import __version__ as saleor_versionfrom ....demo.views import EXAMPLE_QUERYfrom ...tests.fixtures import (ACCESS_CONTROL_ALLOW_CREDENTIALS,@@ -13,6 +15,7 @@ from ...tests.fixtures import (API_PATH,)from ...tests.utils import get_graphql_content, get_graphql_content_from_response+from ...views import generate_cache_keydef test_batch_queries(category, product, api_client, channel_USD):@@ -333,3 +336,64 @@ def test_query_contains_not_only_schema_raise_error(assert graphql_log_handler.messages == ["saleor.graphql.errors.handled[INFO].GraphQLError"]+++INTROSPECTION_QUERY = """+query IntrospectionQuery {+ __schema {+ queryType {+ name+ }+ }+}+"""++INTROSPECTION_RESULT = {"__schema": {"queryType": {"name": "Query"}}}++[email protected]("saleor.graphql.views.cache.set")[email protected]("saleor.graphql.views.cache.get")+@override_settings(DEBUG=False)+def test_introspection_query_is_cached(cache_get_mock, cache_set_mock, api_client):+ cache_get_mock.return_value = None+ cache_key = generate_cache_key(INTROSPECTION_QUERY)+ response = api_client.post_graphql(INTROSPECTION_QUERY)+ content = get_graphql_content(response)+ assert content["data"] == INTROSPECTION_RESULT+ cache_get_mock.assert_called_once_with(cache_key)+ cache_set_mock.assert_called_once_with(+ cache_key, ExecutionResult(data=INTROSPECTION_RESULT)+ )++[email protected]("saleor.graphql.views.cache.set")[email protected]("saleor.graphql.views.cache.get")+@override_settings(DEBUG=False)+def test_introspection_query_is_cached_only_once(+ cache_get_mock, cache_set_mock, api_client+):+ cache_get_mock.return_value = ExecutionResult(data=INTROSPECTION_RESULT)+ cache_key = generate_cache_key(INTROSPECTION_QUERY)+ response = api_client.post_graphql(INTROSPECTION_QUERY)+ content = get_graphql_content(response)+ assert content["data"] == INTROSPECTION_RESULT+ cache_get_mock.assert_called_once_with(cache_key)+ cache_set_mock.assert_not_called()++[email protected]("saleor.graphql.views.cache.set")[email protected]("saleor.graphql.views.cache.get")+@override_settings(DEBUG=True)+def test_introspection_query_is_not_cached_in_debug_mode(+ cache_get_mock, cache_set_mock, api_client+):+ response = api_client.post_graphql(INTROSPECTION_QUERY)+ content = get_graphql_content(response)+ assert content["data"] == INTROSPECTION_RESULT+ cache_get_mock.assert_not_called()+ cache_set_mock.assert_not_called()+++def test_generate_cache_key_use_saleor_version():+ cache_key = generate_cache_key(INTROSPECTION_QUERY)+ assert saleor_version in cache_keydiff --git a/saleor/graphql/views.py b/saleor/graphql/views.pyindex c339d914c1..0f5ae5ee01 100644--- a/saleor/graphql/views.py+++ b/saleor/graphql/views.py@@ -1,4 +1,5 @@import fnmatch+import hashlibimport jsonimport loggingimport traceback@@ -7,6 +8,7 @@ from typing import Any, Dict, List, Optional, Tuple, Unionimport opentracingimport opentracing.tagsfrom django.conf import settings+from django.core.cache import cachefrom django.db import connectionfrom django.db.backends.postgresql.base import DatabaseWrapperfrom django.http import HttpRequest, HttpResponseNotAllowed, JsonResponse@@ -22,6 +24,7 @@ from graphql.error import format_error as format_graphql_errorfrom graphql.execution import ExecutionResultfrom jwt.exceptions import PyJWTError+from .. import __version__ as saleor_versionfrom ..core.exceptions import PermissionDenied, ReadOnlyExceptionfrom ..core.utils import is_valid_ipv4, is_valid_ipv6@@ -213,14 +216,18 @@ class GraphQLView(View):return None, ExecutionResult(errors=[e], invalid=True)def check_if_query_contains_only_schema(self, document: GraphQLDocument):+ query_with_schema = Falsefor 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":+ selection_count = len(selections)+ for selection in selections:+ selection_name = str(selection.name.value)+ if selection_name == "__schema":+ query_with_schema = True+ if selection_count > 1:msg = "`__schema` must be fetched in separete query"raise GraphQLError(msg)+ return query_with_schemadef execute_graphql_request(self, request: HttpRequest, data: dict):with opentracing.global_tracer().start_active_span("graphql_query") as scope:@@ -237,7 +244,9 @@ class GraphQLView(View):raw_query_string = document.document_stringspan.set_tag("graphql.query", raw_query_string)try:- self.check_if_query_contains_only_schema(document)+ query_contains_schema = self.check_if_query_contains_only_schema(+ document+ )except GraphQLError as e:return ExecutionResult(errors=[e], invalid=True)@@ -249,14 +258,26 @@ class GraphQLView(View):extra_options["executor"] = self.executortry:with connection.execute_wrapper(tracing_wrapper):- return document.execute( # type: ignore- root=self.get_root_value(),- variables=variables,- operation_name=operation_name,- context=request,- middleware=self.middleware,- **extra_options,+ response = None+ should_use_cache_for_scheme = query_contains_schema & (+ not settings.DEBUG)+ if should_use_cache_for_scheme:+ key = generate_cache_key(raw_query_string)+ response = cache.get(key)++ if not response:+ response = document.execute( # type: ignore+ root=self.get_root_value(),+ variables=variables,+ operation_name=operation_name,+ context=request,+ middleware=self.middleware,+ **extra_options,+ )+ if should_use_cache_for_scheme:+ cache.set(key, response)+ return responseexcept Exception as e:span.set_tag(opentracing.tags.ERROR, True)return ExecutionResult(errors=[e], invalid=True)@@ -365,3 +386,8 @@ def obj_set(obj, path, value, do_not_replace):except IndexError:passreturn obj_set(obj[current_path], path[1:], value, do_not_replace)+++def generate_cache_key(raw_query: str) -> str:+ hashed_query = hashlib.sha256(str(raw_query).encode("utf-8")).hexdigest()+ return f"{saleor_version}-{hashed_query}"diff --git a/saleor/settings.py b/saleor/settings.pyindex 931fe4c9f4..5e9706e890 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -571,6 +571,7 @@ REDIS_URL = os.environ.get("REDIS_URL")if REDIS_URL:CACHE_URL = os.environ.setdefault("CACHE_URL", REDIS_URL)CACHES = {"default": django_cache_url.config()}+CACHES["default"]["TIMEOUT"] = parse(os.environ.get("CACHE_TIMEOUT", "7 days"))# Default False because storefront and dashboard don't support expiration of tokenJWT_EXPIRE = get_bool_from_env("JWT_EXPIRE", False)