Skip to content

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

Remove merging queries

Maciej Korycinski45fa3d87f8f1
diff --git a/saleor/graphql/core/fields.py b/saleor/graphql/core/fields.py
index b1cfc1b216..4d9ca80fa8 100644
--- a/saleor/graphql/core/fields.py
+++ b/saleor/graphql/core/fields.py
@@ -8,6 +8,7 @@ from graphene.relay import PageInfo
from graphene_django.converter import convert_django_field
from graphene_django.fields import DjangoConnectionField
from graphql_relay.connection.arrayconnection import connection_from_list_slice
+from promise import Promise
from .types.common import Weight
from .types.money import Money, TaxedMoney
@@ -92,24 +93,45 @@ class FilterInputConnectionField(DjangoConnectionField):
if 'edges' not in values:
enforce_first_or_last = False
+ first = args.get('first')
+ last = args.get('last')
+
+ if enforce_first_or_last:
+ assert first or last, (
+ 'You must provide a `first` or `last` value to properly '
+ 'paginate the `{}` connection.'
+ ).format(info.field_name)
+
+ if max_limit:
+ if first:
+ assert first <= max_limit, (
+ 'Requesting {} records on the `{}` connection exceeds the '
+ '`first` limit of {} records.'
+ ).format(first, info.field_name, max_limit)
+ args['first'] = min(first, max_limit)
+
+ if last:
+ assert last <= max_limit, (
+ 'Requesting {} records on the `{}` connection exceeds the '
+ '`last` limit of {} records.'
+ ).format(last, info.field_name, max_limit)
+ args['last'] = min(last, max_limit)
+
+ iterable = resolver(root, info, **args)
+
+ on_resolve = partial(cls.resolve_connection, connection,
+ default_manager, args)
+
filter_input = args.get(filters_name)
- qs = default_manager.get_queryset()
if filter_input and filterset_class:
- qs = filterset_class(
+ iterable = filterset_class(
data=dict(filter_input),
- queryset=default_manager.get_queryset(),
+ queryset=iterable,
request=info.context).qs
- return super().connection_resolver(
- resolver,
- connection,
- qs,
- max_limit,
- enforce_first_or_last,
- root,
- info,
- **args
- )
+ if Promise.is_thenable(iterable):
+ return Promise.resolve(iterable).then(on_resolve)
+ return on_resolve(iterable)
def get_resolver(self, parent_resolver):
return partial(
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index e142b4c37f..8ae52186c4 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -116,7 +116,7 @@ def filter_stock_availability(qs, _, value):
def filter_search(qs, _, value):
if value:
search = picker.pick_backend()
- qs &= search(value)
+ qs &= search(value).distinct()
return qs
diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.py
index 7883e5439a..4dcc056956 100644
--- a/saleor/graphql/product/resolvers.py
+++ b/saleor/graphql/product/resolvers.py
@@ -87,11 +87,6 @@ def resolve_products(
user = info.context.user
qs = models.Product.objects.visible_to_user(user)
- # Graphene merges resolve_queryset and filter_queryset. This process drops
- # all annotations from the filter_queryset. We have to add them to
- # resolve_queryset also.
- qs = qs.annotate(total_quantity=Sum('variants__quantity'))
-
if query:
search = picker.pick_backend()
qs &= search(query)