Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add ability to filter by slug. (#10578)
Krzysztof Waliczek9414f0c87abc
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0526edac2e..4ab6a123e1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,11 @@ All notable, unreleased changes to this project will be documented in this file.# 3.8.0 [Unreleased]+### GraphQL API+- Add ability to filter by slug. #10578 by @kadewu+ - Affected types: Attribute, Category, Collection, Menu, Page, Product, ProductType, Warehouse+ - Deprecated `slug` in filter for `menus`. Use `slugs` instead+### Other changes- Reference attribute linking to product variants - #10468 by @IKarbowiakdiff --git a/saleor/graphql/attribute/filters.py b/saleor/graphql/attribute/filters.pyindex a24cc1dd2d..6d80fd02ee 100644--- a/saleor/graphql/attribute/filters.py+++ b/saleor/graphql/attribute/filters.py@@ -1,4 +1,5 @@import django_filters+import graphenefrom django.db.models import Qfrom ...attribute.models import Attribute, AttributeValue@@ -11,7 +12,9 @@ from ..core.filters import (EnumFilter,GlobalIDFilter,GlobalIDMultipleChoiceFilter,+ ListObjectTypeFilter,MetadataFilterBase,+ filter_slug_list,)from ..core.types import ChannelFilterInputObjectType, FilterInputObjectTypefrom ..core.utils import from_global_id_or_error@@ -82,6 +85,7 @@ class AttributeFilter(MetadataFilterBase):in_collection = GlobalIDFilter(method="filter_in_collection")in_category = GlobalIDFilter(method="filter_in_category")+ slugs = ListObjectTypeFilter(input_class=graphene.String, method=filter_slug_list)class Meta:model = Attributediff --git a/saleor/graphql/core/filters.py b/saleor/graphql/core/filters.pyindex 1a1f948ae7..4e552fe8e9 100644--- a/saleor/graphql/core/filters.py+++ b/saleor/graphql/core/filters.py@@ -82,6 +82,10 @@ def filter_metadata(qs, _, value):return qs+def filter_slug_list(qs, _, values):+ return qs.filter(slug__in=values)++class BaseJobFilter(django_filters.FilterSet):created_at = ObjectTypeFilter(input_class=DateTimeRangeInput, method=filter_created_atdiff --git a/saleor/graphql/menu/filters.py b/saleor/graphql/menu/filters.pyindex 37e0a28f33..e4bbbebe04 100644--- a/saleor/graphql/menu/filters.py+++ b/saleor/graphql/menu/filters.py@@ -3,7 +3,7 @@ import graphenefrom django.db.models import Qfrom ...menu.models import Menu, MenuItem-from ..core.filters import ListObjectTypeFilter, MetadataFilterBase+from ..core.filters import ListObjectTypeFilter, MetadataFilterBase, filter_slug_listfrom ..core.types import FilterInputObjectType@@ -22,6 +22,7 @@ def filter_menu_item_search(qs, _, value):class MenuFilter(MetadataFilterBase):search = django_filters.CharFilter(method=filter_menu_search)slug = ListObjectTypeFilter(input_class=graphene.String, method=filter_menu_slug)+ slugs = ListObjectTypeFilter(input_class=graphene.String, method=filter_slug_list)class Meta:model = Menudiff --git a/saleor/graphql/menu/schema.py b/saleor/graphql/menu/schema.pyindex 7f591c3a7a..c6f3b4526e 100644--- a/saleor/graphql/menu/schema.py+++ b/saleor/graphql/menu/schema.py@@ -3,6 +3,7 @@ import graphenefrom ..channel import ChannelQsContextfrom ..channel.utils import get_default_channel_slug_or_graphql_errorfrom ..core.connection import create_connection_slice, filter_connection_queryset+from ..core.descriptions import DEPRECATED_IN_3X_FIELDfrom ..core.fields import FilterConnectionFieldfrom ..core.utils import from_global_id_or_errorfrom ..translations.mutations import MenuItemTranslate@@ -45,7 +46,12 @@ class MenuQueries(graphene.ObjectType):description="Slug of a channel for which the data should be returned."),sort_by=MenuSortingInput(description="Sort menus."),- filter=MenuFilterInput(description="Filtering options for menus."),+ filter=MenuFilterInput(+ description=(+ "Filtering options for menus. "+ f"\n\n`slug`: {DEPRECATED_IN_3X_FIELD} Use `slugs` instead."+ )+ ),description="List of the storefront's menus.",)menu_item = graphene.Field(diff --git a/saleor/graphql/menu/tests/test_menu.py b/saleor/graphql/menu/tests/test_menu.pyindex 80d3859e9a..a3ba0b6e25 100644--- a/saleor/graphql/menu/tests/test_menu.py+++ b/saleor/graphql/menu/tests/test_menu.py@@ -176,7 +176,13 @@ QUERY_MENU_WITH_FILTER = """@pytest.mark.parametrize(- "menu_filter, count", [({"search": "Menu1"}, 1), ({"search": "Menu"}, 2)]+ "menu_filter, count",+ [+ ({"search": "Menu1"}, 1),+ ({"search": "Menu"}, 2),+ ({"slugs": ["Menu1", "Menu2"]}, 2),+ ({"slugs": []}, 4),+ ],)def test_menus_query_with_filter(menu_filter, count, staff_api_client, permission_manage_menusdiff --git a/saleor/graphql/page/filters.py b/saleor/graphql/page/filters.pyindex d6fd66c942..1050a156b8 100644--- a/saleor/graphql/page/filters.py+++ b/saleor/graphql/page/filters.py@@ -1,8 +1,14 @@import django_filters+import graphenefrom django.db.models import Qfrom ...page import models-from ..core.filters import GlobalIDMultipleChoiceFilter, MetadataFilterBase+from ..core.filters import (+ GlobalIDMultipleChoiceFilter,+ ListObjectTypeFilter,+ MetadataFilterBase,+ filter_slug_list,+)from ..core.types import FilterInputObjectTypefrom ..utils import resolve_global_ids_to_primary_keysfrom ..utils.filters import filter_by_id@@ -36,6 +42,7 @@ class PageFilter(MetadataFilterBase):search = django_filters.CharFilter(method=filter_page_search)page_types = GlobalIDMultipleChoiceFilter(method=filter_page_page_types)ids = GlobalIDMultipleChoiceFilter(method=filter_by_id(Page))+ slugs = ListObjectTypeFilter(input_class=graphene.String, method=filter_slug_list)class Meta:model = models.Page@@ -49,6 +56,7 @@ class PageFilterInput(FilterInputObjectType):class PageTypeFilter(django_filters.FilterSet):search = django_filters.CharFilter(method=filter_page_type_search)+ slugs = ListObjectTypeFilter(input_class=graphene.String, method=filter_slug_list)class PageTypeFilterInput(FilterInputObjectType):diff --git a/saleor/graphql/page/tests/queries/test_page_types_sorting_and_filtering.py b/saleor/graphql/page/tests/queries/test_page_types_sorting_and_filtering.pyindex 1194ebcc12..ff1e9176dc 100644--- a/saleor/graphql/page/tests/queries/test_page_types_sorting_and_filtering.py+++ b/saleor/graphql/page/tests/queries/test_page_types_sorting_and_filtering.py@@ -40,6 +40,31 @@ def test_filter_page_types(}+ "filter_by, pages_count",+ [+ ({"slugs": ["page-type-2", "page-type-3"]}, 2),+ ({"slugs": []}, 3),+ ],+)+def test_filter_page_types_filtering(+ filter_by, pages_count, staff_api_client, page_type_list+):+ # given+ variables = {"filter": filter_by}++ # when+ response = staff_api_client.post_graphql(+ PAGE_TYPES_QUERY,+ variables,+ )++ # then+ content = get_graphql_content(response)+ pages_nodes = content["data"]["pageTypes"]["edges"]+ assert len(pages_nodes) == pages_count++@pytest.mark.parametrize("direction, order_direction",(("ASC", "name"), ("DESC", "-name")),diff --git a/saleor/graphql/page/tests/queries/test_pages.py b/saleor/graphql/page/tests/queries/test_pages.pyindex ebb93329e8..19f595f754 100644--- a/saleor/graphql/page/tests/queries/test_pages.py+++ b/saleor/graphql/page/tests/queries/test_pages.py@@ -76,6 +76,30 @@ def test_pages_query_with_filter_by_page_type(assert content["data"]["pages"]["totalCount"] == 2+ "filter_by, pages_count",+ [+ ({"slugs": ["test-url-1"]}, 1),+ ({"slugs": ["test-url-1", "test-url-2"]}, 2),+ ({"slugs": []}, 2),+ ],+)+def test_pages_with_filtering(filter_by, pages_count, staff_api_client, page_list):+ # given+ variables = {"filter": filter_by}++ # when+ response = staff_api_client.post_graphql(+ QUERY_PAGES_WITH_FILTER,+ variables,+ )++ # then+ content = get_graphql_content(response)+ pages_nodes = content["data"]["pages"]["edges"]+ assert len(pages_nodes) == pages_count++def test_pages_query_with_filter_by_ids(staff_api_client, permission_manage_pages, page_list, page_list_unpublished):diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.pyindex 5b5f67d441..ab80e99c98 100644--- a/saleor/graphql/product/filters.py+++ b/saleor/graphql/product/filters.py@@ -42,6 +42,7 @@ from ..core.filters import (ListObjectTypeFilter,MetadataFilterBase,ObjectTypeFilter,+ filter_slug_list,)from ..core.types import (ChannelFilterInputObjectType,@@ -628,6 +629,7 @@ class ProductFilter(MetadataFilterBase):has_preordered_variants = django_filters.BooleanFilter(method=filter_has_preordered_variants)+ slugs = ListObjectTypeFilter(input_class=graphene.String, method=filter_slug_list)class Meta:model = Product@@ -694,6 +696,7 @@ class CollectionFilter(MetadataFilterBase):)search = django_filters.CharFilter(method="collection_filter_search")ids = GlobalIDMultipleChoiceFilter(field_name="id")+ slugs = ListObjectTypeFilter(input_class=graphene.String, method=filter_slug_list)class Meta:model = Collection@@ -717,6 +720,7 @@ class CollectionFilter(MetadataFilterBase):class CategoryFilter(MetadataFilterBase):search = django_filters.CharFilter(method="category_filter_search")ids = GlobalIDMultipleChoiceFilter(field_name="id")+ slugs = ListObjectTypeFilter(input_class=graphene.String, method=filter_slug_list)class Meta:model = Category@@ -745,6 +749,7 @@ class ProductTypeFilter(MetadataFilterBase):product_type = EnumFilter(input_class=ProductTypeEnum, method=filter_product_type)kind = EnumFilter(input_class=ProductTypeKindEnum, method=filter_product_type_kind)ids = GlobalIDMultipleChoiceFilter(field_name="id")+ slugs = ListObjectTypeFilter(input_class=graphene.String, method=filter_slug_list)class Meta:model = ProductTypediff --git a/saleor/graphql/product/tests/test_category_filtering.py b/saleor/graphql/product/tests/test_category_filtering.pynew file mode 100644index 0000000000..414edcff3f--- /dev/null+++ b/saleor/graphql/product/tests/test_category_filtering.py@@ -0,0 +1,68 @@+import pytest++from ....product.models import Category+from ...tests.utils import get_graphql_content+++def categories_for_filtering():+ return Category.objects.bulk_create(+ [+ Category(+ name="Category1", slug="category1", lft=0, rght=0, tree_id=0, level=0+ ),+ Category(+ name="Category2", slug="category2", lft=1, rght=2, tree_id=1, level=0+ ),+ Category(+ name="Category3", slug="category3", lft=1, rght=2, tree_id=2, level=0+ ),+ ]+ )+++QUERY_CATEGORIES_WITH_FILTERING = """+ query (+ $filter: CategoryFilterInput+ ){+ categories (+ first: 10, filter: $filter+ ) {+ edges {+ node {+ name+ slug+ }+ }+ }+ }+"""+++ "filter_by, categories_count",+ [+ ({"slugs": ["category1"]}, 1),+ ({"slugs": ["category2", "category3"]}, 2),+ ({"slugs": []}, 3),+ ],+)+def test_categories_with_filtering(+ filter_by,+ categories_count,+ staff_api_client,+ categories_for_filtering,+):+ # given+ variables = {"filter": filter_by}++ # when+ response = staff_api_client.post_graphql(+ QUERY_CATEGORIES_WITH_FILTERING,+ variables,+ )++ # then+ content = get_graphql_content(response)+ categories_nodes = content["data"]["categories"]["edges"]+ assert len(categories_nodes) == categories_countdiff --git a/saleor/graphql/product/tests/test_collection_filtering_and_sorting_with_channels.py b/saleor/graphql/product/tests/test_collection_filtering_and_sorting_with_channels.pyindex be39dc8d8a..3d6a69bb95 100644--- a/saleor/graphql/product/tests/test_collection_filtering_and_sorting_with_channels.py+++ b/saleor/graphql/product/tests/test_collection_filtering_and_sorting_with_channels.py@@ -294,7 +294,13 @@ def test_collections_with_filtering_without_channel(@pytest.mark.parametrize("filter_by, collections_count",- [({"published": "PUBLISHED"}, 1), ({"published": "HIDDEN"}, 3)],+ [+ ({"published": "PUBLISHED"}, 1),+ ({"published": "HIDDEN"}, 3),+ ({"slugs": ["collection1"]}, 1),+ ({"slugs": ["collection2", "collection3"]}, 2),+ ({"slugs": []}, 4),+ ],)def test_collections_with_filtering_with_channel_USD(filter_by,@@ -352,7 +358,11 @@ def test_collections_with_filtering_with_channel_PLN(@pytest.mark.parametrize("filter_by",- [{"published": "PUBLISHED"}, {"published": "HIDDEN"}],+ [+ {"published": "PUBLISHED"},+ {"published": "HIDDEN"},+ {"slugs": ["collection1"]},+ ],)def test_collections_with_filtering_and_not_existing_channel(filter_by,diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex c7e0bbbe28..1651a3d149 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -11570,6 +11570,8 @@ def test_categories_query_with_sort(({"productType": "SHIPPABLE"}, 2), # is_shipping_required({"kind": "NORMAL"}, 2),({"kind": "GIFT_CARD"}, 1),+ ({"slugs": ["digital-type", "tools"]}, 2),+ ({"slugs": []}, 3),],)def test_product_type_query_with_filter(diff --git a/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py b/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.pyindex f92eb73c80..bcda88b6b7 100644--- a/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py+++ b/saleor/graphql/product/tests/test_product_filtering_and_sorting_with_channels.py@@ -518,6 +518,9 @@ def test_products_with_filtering_without_channel(({"price": {"gte": 11}}, 1),({"minimalPrice": {"lte": 4}}, 1),({"minimalPrice": {"gte": 5}}, 3),+ ({"slugs": ["prod1"]}, 1),+ ({"slugs": ["prod_prod1", "prod_prod2"]}, 2),+ ({"slugs": []}, 4),],)def test_products_with_filtering_with_channel_USD(@@ -590,6 +593,7 @@ def test_products_with_filtering_with_channel_PLN({"price": {"gte": 11}},{"minimalPrice": {"lte": 4}},{"minimalPrice": {"gte": 5}},+ {"slugs": ["prod1"]},],)def test_products_with_filtering_and_not_existing_channel(diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex a641826440..7b31925e21 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -641,7 +641,11 @@ type Query {"""Sort menus."""sortBy: MenuSortingInput- """Filtering options for menus."""+ """+ Filtering options for menus.++ `slug`: This field will be removed in Saleor 4.0. Use `slugs` instead.+ """filter: MenuFilterInput"""Return the elements in the list that come before the specified cursor."""@@ -5077,6 +5081,7 @@ input AttributeFilterInput {type: AttributeTypeEnuminCollection: IDinCategory: ID+ slugs: [String!]"""Specifies the channel by which the data should be filtered.@@ -6013,6 +6018,7 @@ input ProductFilterInput {giftCard: Booleanids: [ID!]hasPreorderedVariants: Boolean+ slugs: [String!]"""Specifies the channel by which the data should be filtered.@@ -6260,6 +6266,7 @@ input WarehouseFilterInput {ids: [ID!]isPrivate: Booleanchannels: [ID!]+ slugs: [String!]}input WarehouseSortingInput {@@ -9682,6 +9689,7 @@ input CategoryFilterInput {search: Stringmetadata: [MetadataFilter!]ids: [ID!]+ slugs: [String!]}input CategorySortingInput {@@ -9715,6 +9723,7 @@ input CollectionFilterInput {search: Stringmetadata: [MetadataFilter!]ids: [ID!]+ slugs: [String!]"""Specifies the channel by which the data should be filtered.@@ -9780,6 +9789,7 @@ input ProductTypeFilterInput {metadata: [MetadataFilter!]kind: ProductTypeKindEnumids: [ID!]+ slugs: [String!]}enum ProductTypeConfigurable {@@ -9911,6 +9921,7 @@ input PageFilterInput {metadata: [MetadataFilter!]pageTypes: [ID!]ids: [ID!]+ slugs: [String!]}type PageTypeCountableConnection {@@ -9948,6 +9959,7 @@ enum PageTypeSortField {input PageTypeFilterInput {search: String+ slugs: [String!]}type OrderEventCountableConnection {@@ -10085,6 +10097,7 @@ input MenuFilterInput {search: Stringslug: [String!]metadata: [MetadataFilter!]+ slugs: [String!]}type MenuItemCountableConnection {diff --git a/saleor/graphql/warehouse/filters.py b/saleor/graphql/warehouse/filters.pyindex 23909d0485..d933b79b2d 100644--- a/saleor/graphql/warehouse/filters.py+++ b/saleor/graphql/warehouse/filters.py@@ -1,4 +1,5 @@import django_filters+import graphenefrom django.db.models import Exists, OuterRef, Qfrom ...account.models import Address@@ -6,7 +7,12 @@ from ...product.models import Product, ProductVariantfrom ...warehouse import WarehouseClickAndCollectOptionfrom ...warehouse.models import Stock, Warehousefrom ..channel.types import Channel-from ..core.filters import EnumFilter, GlobalIDMultipleChoiceFilter+from ..core.filters import (+ EnumFilter,+ GlobalIDMultipleChoiceFilter,+ ListObjectTypeFilter,+ filter_slug_list,+)from ..core.types import FilterInputObjectTypefrom ..utils import resolve_global_ids_to_primary_keysfrom ..warehouse.enums import WarehouseClickAndCollectOptionEnum@@ -86,6 +92,7 @@ class WarehouseFilter(django_filters.FilterSet):method=filter_click_and_collect_option,)channels = GlobalIDMultipleChoiceFilter(method=filter_channels)+ slugs = ListObjectTypeFilter(input_class=graphene.String, method=filter_slug_list)class Meta:model = Warehousediff --git a/saleor/graphql/warehouse/tests/test_warehouse.py b/saleor/graphql/warehouse/tests/test_warehouse.pyindex 61da0b9ac8..9c68446939 100644--- a/saleor/graphql/warehouse/tests/test_warehouse.py+++ b/saleor/graphql/warehouse/tests/test_warehouse.py@@ -624,6 +624,32 @@ def test_query_warehouses_with_filters_by_channels_no_warehouse_returned(assert not warehouses_data+ "filter_by, pages_count",+ [+ ({"slugs": ["warehouse1", "warehouse2"]}, 2),+ ({"slugs": []}, 2),+ ],+)+def test_query_warehouses_with_filtering(+ filter_by, pages_count, staff_api_client, permission_manage_products, warehouses+):+ # given+ variables = {"filter": filter_by}++ # when+ response = staff_api_client.post_graphql(+ QUERY_WAREHOUSES_WITH_FILTERS,+ variables,+ permissions=[permission_manage_products],+ )++ # then+ content = get_graphql_content(response)+ pages_nodes = content["data"]["warehouses"]["edges"]+ assert len(pages_nodes) == pages_count++def test_mutation_create_warehouse(staff_api_client, permission_manage_products, shipping_zone):