Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use custom connection to return non-null edges
Marcin Gębala89821f07e641
diff --git a/saleor/graphql/core/connection.py b/saleor/graphql/core/connection.pynew file mode 100644index 0000000000..0a16816cef--- /dev/null+++ b/saleor/graphql/core/connection.py@@ -0,0 +1,42 @@+import graphene+from graphene import Field, ObjectType, String, NonNull, List+from graphene.relay.connection import ConnectionOptions, Connection+++class NonNullConnection(Connection):++ class Meta:+ abstract = True++ @classmethod+ def __init_subclass_with_meta__(cls, node=None, name=None, **options):+ super().__init_subclass_with_meta__(node=node, name=name, **options)++ # Override the original EdgeBase type to make to `node`` field required.+ class EdgeBase(object):+ node = Field(+ cls._meta.node, description='The item at the end of the edge',+ required=True)+ cursor = String(+ required=True, description='A cursor for use in pagination')++ # Create the edge type using the new EdgeBase.+ edge_name = cls.Edge._meta.name+ edge_bases = (EdgeBase, ObjectType,)+ edge = type(edge_name, edge_bases, {})+ cls.Edge = edge++ # Override the `edges` field to make it non-null list of non-null edges.+ cls._meta.fields['edges'] = Field(NonNull(List(NonNull(cls.Edge))))+++class CountableConnection(NonNullConnection):+ class Meta:+ abstract = True++ total_count = graphene.Int(+ description='A total count of items in the collection')++ @staticmethod+ def resolve_total_count(root, info, *args, **kwargs):+ return root.lengthdiff --git a/saleor/graphql/core/types.py b/saleor/graphql/core/types.pyindex d068dc84ec..d8765fbb59 100644--- a/saleor/graphql/core/types.py+++ b/saleor/graphql/core/types.py@@ -35,17 +35,7 @@ class CountryDisplay(graphene.ObjectType):code = graphene.String(description='Country code.')country = graphene.String(description='Country.')--class CountableConnection(graphene.relay.Connection):- class Meta:- abstract = True-- total_count = graphene.Int(- description='A total count of items in the collection')-- @staticmethod- def resolve_total_count(root, info, *args, **kwargs):- return root.length+from .connection import CountableConnectionclass CountableDjangoObjectType(DjangoObjectType):