Skip to content

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

Fixed playground not displaying docs if api is hidden behind reverse proxy (#16810)

Jakub Borusewicz7198c7cc19b6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3f95a6378b..bb737888cc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,3 +31,4 @@ All notable, unreleased changes to this project will be documented in this file.
- Fixed order-level discounts handling when using tax app for tax calculation - #16696 by @zedzior
- Fixed bug when manual line discount doesn't override line-level vouchers - #16738 by @zedzior
- Skipped obsolete payload save and cleanup for successful sync webhooks - #16632 by @cmiacz
+- Fixed playground not displaying docs if api is hidden behind reverse proxy - #16810 by @jqob
diff --git a/saleor/graphql/core/tests/test_view.py b/saleor/graphql/core/tests/test_view.py
index f615f8df46..43eede2e4f 100644
--- a/saleor/graphql/core/tests/test_view.py
+++ b/saleor/graphql/core/tests/test_view.py
@@ -1,8 +1,10 @@
import json
from unittest import mock
+from unittest.mock import patch
import graphene
import pytest
+from django.shortcuts import render
from django.test import override_settings
from graphql.execution.base import ExecutionResult
@@ -350,3 +352,37 @@ def test_graphql_view_clears_context(rf, staff_user, product, channel_USD):
assert json_data["data"]["product"]["category"]["name"] == product.category.name
assert response.status_code == 200
assert request.dataloaders == {}
+
+
+ ("public_url", "expected_url_base"),
+ [
+ (None, "http://testserver"),
+ ("http://some_custom_domain.com", "http://some_custom_domain.com"),
+ ],
+)
+@patch("saleor.graphql.views.render", wraps=render)
+def test_playground_is_rendered_with_proper_api_url_if_public_url_is_set(
+ mocked_render, rf, settings, public_url, expected_url_base
+):
+ # given
+ request = rf.get(
+ path="/",
+ )
+ request.app = None
+ request.user = None
+ settings.PUBLIC_URL = public_url
+
+ # when
+ view = GraphQLView.as_view(backend=backend, schema=schema)
+ view(request)
+
+ # then
+ mocked_render.assert_called_once_with(
+ request,
+ "graphql/playground.html",
+ {
+ "api_url": f"{expected_url_base}/graphql/",
+ "plugins_url": f"{expected_url_base}/plugins/",
+ },
+ )
diff --git a/saleor/graphql/views.py b/saleor/graphql/views.py
index e62d227314..c5bde7811d 100644
--- a/saleor/graphql/views.py
+++ b/saleor/graphql/views.py
@@ -3,6 +3,7 @@ import importlib
import json
from inspect import isclass
from typing import Any, Optional, Union
+from urllib.parse import urljoin
import opentracing
import opentracing.tags
@@ -121,12 +122,19 @@ class GraphQLView(View):
return HttpResponseNotAllowed(["OPTIONS", "POST"])
def render_playground(self, request):
+ if settings.PUBLIC_URL:
+ api_url = urljoin(settings.PUBLIC_URL, str(API_PATH))
+ plugins_url = urljoin(settings.PUBLIC_URL, "/plugins/")
+ else:
+ api_url = request.build_absolute_uri(str(API_PATH))
+ plugins_url = request.build_absolute_uri("/plugins/")
+
return render(
request,
"graphql/playground.html",
{
- "api_url": request.build_absolute_uri(str(API_PATH)),
- "plugins_url": request.build_absolute_uri("/plugins/"),
+ "api_url": api_url,
+ "plugins_url": plugins_url,
},
)