Skip to content

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

Add externalReference filter to products where input (#19904)

DamianCzajkowski52de41769a94
diff --git a/saleor/graphql/product/filters/product.py b/saleor/graphql/product/filters/product.py
index 20120d6215..73fc3d9cd7 100644
--- a/saleor/graphql/product/filters/product.py
+++ b/saleor/graphql/product/filters/product.py
@@ -11,6 +11,7 @@ from ....product.models import (
ProductVariantChannelListing,
)
from ...channel.filters import get_channel_slug_from_filter_data
+from ...core.descriptions import ADDED_IN_323
from ...core.doc_category import DOC_CATEGORY_PRODUCTS
from ...core.filters import (
BooleanWhereFilter,
@@ -229,6 +230,11 @@ class ProductWhere(MetadataWhereFilterBase):
method="filter_product_slug",
help_text="Filter by product slug.",
)
+ external_reference = OperationObjectTypeWhereFilter(
+ input_class=StringFilterInput,
+ method="filter_product_external_reference",
+ help_text="Filter by external reference." + ADDED_IN_323,
+ )
product_type = OperationObjectTypeWhereFilter(
input_class=GlobalIDFilterInput,
method="filter_product_type",
@@ -315,6 +321,10 @@ class ProductWhere(MetadataWhereFilterBase):
def filter_product_slug(qs, _, value):
return filter_where_by_value_field(qs, "slug", value)
+ @staticmethod
+ def filter_product_external_reference(qs, _, value):
+ return filter_where_by_value_field(qs, "external_reference", value)
+
@staticmethod
def filter_product_type(qs, _, value):
return filter_where_by_id_field(qs, "product_type", value, "ProductType")
diff --git a/saleor/graphql/product/tests/queries/test_collection_query.py b/saleor/graphql/product/tests/queries/test_collection_query.py
index ba138c3185..caf67e80c4 100644
--- a/saleor/graphql/product/tests/queries/test_collection_query.py
+++ b/saleor/graphql/product/tests/queries/test_collection_query.py
@@ -5,6 +5,7 @@ import graphene
import pytest
from django.core.files import File
+from .....product.models import Product
from .....product.tests.utils import create_image
from .....thumbnail.models import Thumbnail
from ....core.enums import LanguageCodeEnum, ThumbnailFormatEnum
@@ -510,6 +511,44 @@ def test_filter_where_collection_products(
)
+def test_filter_where_collection_products_by_external_reference(
+ user_api_client, product_list, published_collection, channel_USD
+):
+ # given
+ query = GET_FILTERED_PRODUCTS_COLLECTION_QUERY
+
+ for product in product_list:
+ published_collection.products.add(product)
+
+ first_external_reference = "external-reference-a"
+ second_external_reference = "external-reference-b"
+ product_list[0].external_reference = first_external_reference
+ product_list[1].external_reference = second_external_reference
+ Product.objects.bulk_update(product_list[:2], ["external_reference"])
+
+ variables = {
+ "id": graphene.Node.to_global_id("Collection", published_collection.pk),
+ "channel": channel_USD.slug,
+ "where": {
+ "externalReference": {
+ "oneOf": [first_external_reference, second_external_reference]
+ }
+ },
+ }
+
+ # when
+ response = user_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ products = content["data"]["collection"]["products"]["edges"]
+ assert len(products) == 2
+ assert {node["node"]["id"] for node in products} == {
+ graphene.Node.to_global_id("Product", product.pk)
+ for product in product_list[:2]
+ }
+
+
def test_search_collection_products(
user_api_client, product_list, published_collection, channel_USD, channel_PLN
):
diff --git a/saleor/graphql/product/tests/queries/test_products_query_with_where.py b/saleor/graphql/product/tests/queries/test_products_query_with_where.py
index 5eceb482a3..98cdb88391 100644
--- a/saleor/graphql/product/tests/queries/test_products_query_with_where.py
+++ b/saleor/graphql/product/tests/queries/test_products_query_with_where.py
@@ -134,6 +134,97 @@ def test_product_filter_by_slug(where, indexes, api_client, product_list, channe
assert returned_slugs == {product_list[index].slug for index in indexes}
+PRODUCT_A_EXTERNAL_REFERENCE = "external-reference-a"
+PRODUCT_B_EXTERNAL_REFERENCE = "external-reference-b"
+
+
+ ("_case", "where", "indexes"),
+ [
+ ("eq_match", {"eq": PRODUCT_A_EXTERNAL_REFERENCE}, [0]),
+ ("eq_no_match", {"eq": "non-existing"}, []),
+ ("eq_empty_string", {"eq": ""}, []),
+ ("eq_null_matches_products_without_reference", {"eq": None}, [2]),
+ (
+ "one_of_multiple_matches",
+ {"oneOf": [PRODUCT_A_EXTERNAL_REFERENCE, PRODUCT_B_EXTERNAL_REFERENCE]},
+ [0, 1],
+ ),
+ (
+ "one_of_mixed_existing_and_non_existing",
+ {"oneOf": [PRODUCT_B_EXTERNAL_REFERENCE, "non-existing"]},
+ [1],
+ ),
+ ("one_of_no_match", {"oneOf": ["non-existing-1", "non-existing-2"]}, []),
+ ("one_of_empty_list", {"oneOf": []}, []),
+ ("one_of_null", {"oneOf": None}, []),
+ ("empty_input", {}, []),
+ ("null_input", None, []),
+ ],
+)
+def test_product_filter_by_external_reference(
+ _case, where, indexes, api_client, product_list, channel_USD
+):
+ # given
+ product_list[0].external_reference = PRODUCT_A_EXTERNAL_REFERENCE
+ product_list[1].external_reference = PRODUCT_B_EXTERNAL_REFERENCE
+ Product.objects.bulk_update(product_list[:2], ["external_reference"])
+ assert product_list[2].external_reference is None
+
+ variables = {"channel": channel_USD.slug, "where": {"externalReference": where}}
+
+ # when
+ response = api_client.post_graphql(PRODUCTS_WHERE_QUERY, variables)
+
+ # then
+ data = get_graphql_content(response)
+ nodes = data["data"]["products"]["edges"]
+ assert len(nodes) == len(indexes)
+ returned_ids = {node["node"]["id"] for node in nodes}
+ assert returned_ids == {
+ graphene.Node.to_global_id("Product", product_list[index].pk)
+ for index in indexes
+ }
+
+
+def test_product_filter_by_external_reference_and_name(
+ api_client, product_list, channel_USD
+):
+ # given
+ product_list[0].external_reference = PRODUCT_A_EXTERNAL_REFERENCE
+ product_list[1].external_reference = PRODUCT_B_EXTERNAL_REFERENCE
+ Product.objects.bulk_update(product_list[:2], ["external_reference"])
+ expected_product = product_list[1]
+
+ variables = {
+ "channel": channel_USD.slug,
+ "where": {
+ "AND": [
+ {
+ "externalReference": {
+ "oneOf": [
+ PRODUCT_A_EXTERNAL_REFERENCE,
+ PRODUCT_B_EXTERNAL_REFERENCE,
+ ]
+ }
+ },
+ {"name": {"eq": expected_product.name}},
+ ]
+ },
+ }
+
+ # when
+ response = api_client.post_graphql(PRODUCTS_WHERE_QUERY, variables)
+
+ # then
+ data = get_graphql_content(response)
+ nodes = data["data"]["products"]["edges"]
+ assert len(nodes) == 1
+ assert nodes[0]["node"]["id"] == graphene.Node.to_global_id(
+ "Product", expected_product.pk
+ )
+
+
def test_product_filter_by_product_types(
api_client, product_list, channel_USD, product_type_list
):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 494d91fb13..808b74640e 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -4744,6 +4744,13 @@ input ProductWhereInput @doc(category: "Products") {
"""Filter by product slug."""
slug: StringFilterInput
+ """
+ Filter by external reference.
+
+ Added in Saleor 3.23.
+ """
+ externalReference: StringFilterInput
+
"""Filter by product type."""
productType: GlobalIDFilterInput