Skip to content

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

Render example query only in demo mode

Marcin Gębalaae9db8cec448
diff --git a/saleor/core/management/commands/populatedb.py b/saleor/core/management/commands/populatedb.py
index 6dedffaa63..722f631ecd 100644
--- a/saleor/core/management/commands/populatedb.py
+++ b/saleor/core/management/commands/populatedb.py
@@ -1,12 +1,12 @@
from io import StringIO
from django.apps import apps
-from django.conf import settings
from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.db import connection
from ....account.utils import create_superuser
+from ....core.utils import is_demo_mode
from ...utils.random_data import (
add_address_to_admin,
configure_braintree,
@@ -123,7 +123,7 @@ class Command(BaseCommand):
for msg in create_permission_groups():
self.stdout.write(msg)
- if getattr(settings, "DEMO", False):
+ if is_demo_mode():
is_configured = configure_braintree()
if is_configured:
self.stdout.write("Configured Braintree")
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.py
index 7123a3490d..4bfa76d555 100644
--- a/saleor/core/utils/__init__.py
+++ b/saleor/core/utils/__init__.py
@@ -156,3 +156,8 @@ def generate_unique_slug(
unique_slug = f"{slug}-{extension}"
return unique_slug
+
+
+def is_demo_mode():
+ """Return True if Saleor is running in the "demo" mode."""
+ return getattr(settings, "DEMO", False)
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.py
index 610d65de58..262430b208 100644
--- a/saleor/graphql/views.py
+++ b/saleor/graphql/views.py
@@ -24,7 +24,7 @@ from graphql.error import (
from graphql.execution import ExecutionResult
from graphql_jwt.exceptions import PermissionDenied
-from ..core.utils import is_valid_ipv4, is_valid_ipv6
+from ..core.utils import is_demo_mode, is_valid_ipv4, is_valid_ipv6
API_PATH = SimpleLazyObject(lambda: reverse("api"))
@@ -102,11 +102,7 @@ class GraphQLView(View):
# Handle options method the GraphQlView restricts it.
if request.method == "GET":
if settings.PLAYGROUND_ENABLED:
- ctx = {
- "query": EXAMPLE_QUERY,
- "api_url": request.build_absolute_uri(str(API_PATH)),
- }
- return render_to_response("graphql/playground.html", ctx)
+ return self.render_playground(request)
return HttpResponseNotAllowed(["OPTIONS", "POST"])
if request.method == "OPTIONS":
response = self.options(request, *args, **kwargs)
@@ -122,6 +118,17 @@ class GraphQLView(View):
] = "Origin, Content-Type, Accept, Authorization"
return response
+ def render_playground(self, request):
+ ctx = {}
+ if is_demo_mode():
+ ctx.update(
+ {
+ "query": EXAMPLE_QUERY,
+ "api_url": request.build_absolute_uri(str(API_PATH)),
+ }
+ )
+ return render_to_response("graphql/playground.html", ctx)
+
def _handle_query(self, request: HttpRequest) -> JsonResponse:
try:
data = self.parse_body(request)
diff --git a/templates/graphql/playground.html b/templates/graphql/playground.html
index 0bc794c2a1..679354c65b 100644
--- a/templates/graphql/playground.html
+++ b/templates/graphql/playground.html
@@ -43,14 +43,20 @@
</div>
</div>
<script>window.addEventListener('load', function (event) {
- GraphQLPlayground.init(document.getElementById('root'), {
- settings: {
- 'schema.polling.enable': false
- },
- tabs: [{
- endpoint: '{{ api_url }}',
+ const settings = {
+ "schema.polling.enable": false,
+ };
+
+ let tabs;
+ {% if api_url and query %}
+ tabs = [{
+ endpoint: "{{ api_url }}",
query: `{{ query|safe }}`
}]
+ {% endif %}
+
+ GraphQLPlayground.init(document.getElementById('root'), {
+ settings, tabs
})
})</script>
</body>