Skip to content

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

Allow Playground view with DEBUG=False; provide example query

Marcin Gębalaa7a7d268197a
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.py
index 193bbcf282..610d65de58 100644
--- a/saleor/graphql/views.py
+++ b/saleor/graphql/views.py
@@ -31,6 +31,27 @@ API_PATH = SimpleLazyObject(lambda: reverse("api"))
unhandled_errors_logger = logging.getLogger("saleor.graphql.errors.unhandled")
handled_errors_logger = logging.getLogger("saleor.graphql.errors.handled")
+EXAMPLE_QUERY = """# Welcome to Saleor GraphQL API!
+#
+# Type queries into this side of the screen, and you will see
+# intelligent typeaheads aware of the current GraphQL type schema
+# and live syntax and validation errors highlighted within the text.
+#
+# Here is an example query to fetch a list of products:
+#
+{
+ products(first: 5) {
+ edges {
+ node {
+ id
+ name
+ description
+ }
+ }
+ }
+}
+"""
+
def tracing_wrapper(execute, sql, params, many, context):
conn: DatabaseWrapper = context["connection"]
@@ -81,9 +102,12 @@ class GraphQLView(View):
# Handle options method the GraphQlView restricts it.
if request.method == "GET":
if settings.PLAYGROUND_ENABLED:
- return render_to_response("graphql/playground.html")
+ ctx = {
+ "query": EXAMPLE_QUERY,
+ "api_url": request.build_absolute_uri(str(API_PATH)),
+ }
+ return render_to_response("graphql/playground.html", ctx)
return HttpResponseNotAllowed(["OPTIONS", "POST"])
-
if request.method == "OPTIONS":
response = self.options(request, *args, **kwargs)
elif request.method == "POST":
diff --git a/templates/graphql/playground.html b/templates/graphql/playground.html
index 062343a992..0bc794c2a1 100644
--- a/templates/graphql/playground.html
+++ b/templates/graphql/playground.html
@@ -46,7 +46,11 @@
GraphQLPlayground.init(document.getElementById('root'), {
settings: {
'schema.polling.enable': false
- }
+ },
+ tabs: [{
+ endpoint: '{{ api_url }}',
+ query: `{{ query|safe }}`
+ }]
})
})</script>
</body>
diff --git a/tests/api/test_view.py b/tests/api/test_view.py
index f66001dccf..d1be93d1cb 100644
--- a/tests/api/test_view.py
+++ b/tests/api/test_view.py
@@ -6,7 +6,11 @@ import pytest
from django.test import override_settings
from saleor.graphql.product.types import Product
-from saleor.graphql.views import handled_errors_logger, unhandled_errors_logger
+from saleor.graphql.views import (
+ EXAMPLE_QUERY,
+ handled_errors_logger,
+ unhandled_errors_logger,
+)
from .conftest import API_PATH
from .utils import _get_graphql_content_from_response, get_graphql_content
@@ -227,3 +231,9 @@ def test_unexpected_exceptions_are_logged_in_their_own_logger(
assert graphql_log_handler.messages == [
"saleor.graphql.errors.unhandled[ERROR].NotImplementedError"
]
+
+
+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