Skip to content

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

Fix sdl queyr for federation

fowczrek60f845657242
diff --git a/saleor/__init__.py b/saleor/__init__.py
index 1f44d281dd..5f8db4a86e 100644
--- a/saleor/__init__.py
+++ b/saleor/__init__.py
@@ -1,4 +1,19 @@
+from graphql.utils import schema_printer
+
from .celeryconf import app as celery_app
__all__ = ["celery_app"]
__version__ = "dev"
+
+
+def patched_print_object(type):
+ interfaces = type.interfaces
+ implemented_interfaces = (
+ " implements {}".format(" & ".join(i.name for i in interfaces))
+ if interfaces
+ else ""
+ )
+
+ return ("type {}{} {{\n" "{}\n" "}}").format(
+ type.name, implemented_interfaces, schema_printer._print_fields(type)
+ )
diff --git a/saleor/graphql/management/commands/get_graphql_schema.py b/saleor/graphql/management/commands/get_graphql_schema.py
index 6e5812f4ae..3d779e8b5d 100644
--- a/saleor/graphql/management/commands/get_graphql_schema.py
+++ b/saleor/graphql/management/commands/get_graphql_schema.py
@@ -1,5 +1,4 @@
from django.core.management.base import BaseCommand
-from graphql import print_schema
from ...api import schema
@@ -8,14 +7,4 @@ class Command(BaseCommand):
help = "Writes SDL for GraphQL API schema to stdout"
def handle(self, *args, **options):
- """Support multiple interface notation in schema for Apollo tooling.
-
- In `graphql-core` V2 separator for interaces is `,`.
- Apollo tooling to generate TypeScript types using `&` as interfaces separator.
- https://github.com/graphql-python/graphql-core/pull/258
- """
- printed_schema = print_schema(schema)
- for line in printed_schema.splitlines():
- if "implements" in line:
- line = line.replace(",", " &")
- self.stdout.write(f"{line}\n")
+ self.stdout.write(str(schema))
diff --git a/saleor/graphql/tests/test_utils.py b/saleor/graphql/tests/test_utils.py
index 854d198909..009c5cf590 100644
--- a/saleor/graphql/tests/test_utils.py
+++ b/saleor/graphql/tests/test_utils.py
@@ -1,8 +1,12 @@
+import re
+
import graphene
from django.conf import settings
+from graphql.utils import schema_printer
from ..account.types import AddressInput
from ..utils import get_user_country_context
+from .utils import get_graphql_content
def test_get_user_country_context_from_address_instance(address, address_other_country):
@@ -43,3 +47,25 @@ def test_get_user_country_from_address_input():
assert not result.errors
assert result.data == {"field": "US"}
+
+
+def test_multiple_interface_separator_in_schema(api_client):
+ query = """
+ query __ApolloGetServiceDefinition__ {
+ _service {
+ sdl
+ }
+ }
+ """
+ response = api_client.post_graphql(query)
+
+ content = get_graphql_content(response)
+ sdl = content["data"]["_service"]["sdl"]
+ comma_separated_interfaces = re.findall("implements (\\w+,) (\\w+)", sdl)
+ ampersand_separated_interfaces = re.findall("implements (\\w+) & (\\w+)", sdl)
+ assert not comma_separated_interfaces
+ assert ampersand_separated_interfaces
+
+
+def test_graphql_core_contains_patched_function():
+ assert hasattr(schema_printer, "_print_object")
diff --git a/saleor/settings.py b/saleor/settings.py
index a852775314..5ded926808 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -13,11 +13,14 @@ import sentry_sdk
import sentry_sdk.utils
from django.core.exceptions import ImproperlyConfigured
from django.core.management.utils import get_random_secret_key
+from graphql.utils import schema_printer
from pytimeparse import parse
from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import ignore_logger
+from . import patched_print_object
+
def get_list(text):
return [item.strip() for item in text.split(",")]
@@ -598,3 +601,13 @@ JWT_TTL_REFRESH = timedelta(seconds=parse(os.environ.get("JWT_TTL_REFRESH", "30
JWT_TTL_REQUEST_EMAIL_CHANGE = timedelta(
seconds=parse(os.environ.get("JWT_TTL_REQUEST_EMAIL_CHANGE", "1 hour")),
)
+
+# Support multiple interface notation in schema for Apollo tooling.
+
+# In `graphql-core` V2 separator for interaces is `,`.
+# Apollo tooling to generate TypeScript types using `&` as interfaces separator.
+# https://github.com/graphql-python/graphql-core-legacy/pull/258
+# https://github.com/graphql-python/graphql-core-legacy/issues/176
+
+assert hasattr(schema_printer, "_print_object")
+schema_printer._print_object = patched_print_object