Skip to content

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

Explore replacing graphene-relay with custom implementation

Rafał Pitoń131972eeda70
diff --git a/saleor/graphql/core/relay.py b/saleor/graphql/core/relay.py
new file mode 100644
index 0000000000..1db948769a
--- /dev/null
+++ b/saleor/graphql/core/relay.py
@@ -0,0 +1,42 @@
+"""Implementation of Relay specification."""
+import graphene
+
+
+class NewPageInfo(graphene.ObjectType):
+ has_next_page = graphene.Boolean(required=True)
+ has_previous_page = graphene.Boolean(required=True)
+ start_cursor = graphene.String()
+ end_cursor = graphene.String()
+
+
+class NewConnection(graphene.ObjectType):
+ page_info = graphene.Field(NewPageInfo, required=True)
+ total_count = graphene.Int()
+
+ class Meta:
+ abstract = True
+
+
+types_registry = {}
+
+
+def connection_field(graphql_type):
+ type_name = get_type_name(graphql_type)
+ if type_name not in types_registry:
+ connection_type = create_connection_type(type_name)
+ types_registry[type_name] = connection_type
+ return graphene.Field(types_registry[type_name])
+
+
+def get_type_name(graphql_type):
+ try:
+ return graphql_type.Meta.name or graphql_type.__name__
+ except AttributeError:
+ return graphql_type.__name__
+
+
+def create_connection_type(type_name: str):
+ connection_type_name = f"{type_name}ConnectionType"
+ return type(
+ connection_type_name, (NewConnection,), {}
+ )
diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.py
index 1476f1994e..8a0c8280da 100644
--- a/saleor/graphql/product/schema.py
+++ b/saleor/graphql/product/schema.py
@@ -12,6 +12,7 @@ from ..core.fields import (
FilterInputConnectionField,
PrefetchingConnectionField,
)
+from ..core.relay import connection_field
from ..core.utils import from_global_id_or_error
from ..core.validators import validate_one_of_args_is_in_query
from ..decorators import permission_required
@@ -125,6 +126,12 @@ from .types import (
class ProductQueries(graphene.ObjectType):
+ new_categories = connection_field(
+ Category
+ )
+ new_categories_alt = connection_field(
+ Category
+ )
digital_content = graphene.Field(
DigitalContent,
description="Look up digital content by ID.",
@@ -250,6 +257,13 @@ class ProductQueries(graphene.ObjectType):
def resolve_categories(self, info, level=None, **kwargs):
return resolve_categories(info, level=level, **kwargs)
+ def resolve_new_categories(self, info, level=None, **kwargs):
+ qs = resolve_categories(info, level=level, **kwargs)
+ return {
+ "edges": qs,
+ "total_count": qs.count()
+ }
+
@traced_resolver
def resolve_category(self, info, id=None, slug=None, **kwargs):
validate_one_of_args_is_in_query("id", id, "slug", slug)