Skip to content

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

Fix type of return value for field. Add more meaningfull name to it

Mira Szewczykd2c6b84e6f26
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index f6dfc50641..acc6a5aa04 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -270,7 +270,7 @@ class ProductVariant(CountableDjangoObjectType, MetadataObjectType):
stock = gql_optimizer.field(
graphene.Field(
- Stock,
+ graphene.List(Stock),
description="Stocks for the product variant.",
country=graphene.String(required=False),
)
@@ -293,13 +293,10 @@ class ProductVariant(CountableDjangoObjectType, MetadataObjectType):
@staticmethod
def resolve_stock(root: models.ProductVariant, info, country=None):
- if country is not None:
- return stock_models.Stock.objects.get_variant_stock_for_country(
- country, root
- )
- return stock_models.Stock.objects.annotate_available_quantity().filter(
- product_variant=root
- )
+ stock = root.stock.annotate_available_quantity()
+ if country is None:
+ return stock.all()
+ return stock.for_country(country)
@staticmethod
@permission_required(ProductPermissions.MANAGE_PRODUCTS)
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index f62271eff0..b657996ad8 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3370,7 +3370,7 @@ type ProductVariant implements Node {
images: [ProductImage]
translation(languageCode: LanguageCodeEnum!): ProductVariantTranslation
digitalContent: DigitalContent
- stock(country: String): Stock
+ stock(country: String): [Stock]
}
type ProductVariantBulkCreate {
diff --git a/saleor/warehouse/migrations/0001_initial.py b/saleor/warehouse/migrations/0001_initial.py
index 2fbbc83c0a..ce01e2486b 100644
--- a/saleor/warehouse/migrations/0001_initial.py
+++ b/saleor/warehouse/migrations/0001_initial.py
@@ -140,6 +140,7 @@ class Migration(migrations.Migration):
"product_variant",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
+ related_name="stock",
to="product.ProductVariant",
),
),
diff --git a/saleor/warehouse/models.py b/saleor/warehouse/models.py
index db5703bbf0..8eefedeb56 100644
--- a/saleor/warehouse/models.py
+++ b/saleor/warehouse/models.py
@@ -79,7 +79,7 @@ class StockQuerySet(models.QuerySet):
class Stock(models.Model):
warehouse = models.ForeignKey(Warehouse, null=False, on_delete=models.PROTECT)
product_variant = models.ForeignKey(
- ProductVariant, null=False, on_delete=models.CASCADE
+ ProductVariant, null=False, on_delete=models.CASCADE, related_name="stock"
)
quantity = models.PositiveIntegerField(default=0)
quantity_allocated = models.PositiveIntegerField(default=0)
diff --git a/tests/api/test_variant.py b/tests/api/test_variant.py
index 80da3174e8..92be9b7a6f 100644
--- a/tests/api/test_variant.py
+++ b/tests/api/test_variant.py
@@ -16,6 +16,9 @@ def test_fetch_variant(staff_api_client, product, permission_manage_products):
query ProductVariantDetails($id: ID!) {
productVariant(id: $id) {
id
+ stock {
+ id
+ }
attributes {
attribute {
id
@@ -60,6 +63,7 @@ def test_fetch_variant(staff_api_client, product, permission_manage_products):
content = get_graphql_content(response)
data = content["data"]["productVariant"]
assert data["name"] == variant.name
+ assert len(data["stock"]) == variant.stock.count()
def test_create_variant(