Skip to content

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

Mark old graphQL fields as deprecated

Mira Szewczykdf85c35a86aa
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index d0b5144f5f..1d39020fb7 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -21,7 +21,11 @@ from ....product.utils.availability import (
)
from ....product.utils.costs import get_margin_for_variant, get_product_costs_data
from ....stock import models as stock_models
-from ....stock.availability import is_product_in_stock
+from ....stock.availability import (
+ get_available_quantity_for_customer,
+ is_product_in_stock,
+ is_variant_in_stock,
+)
from ...core.connection import CountableDjangoObjectType
from ...core.enums import ReportingPeriod, TaxRateType
from ...core.fields import FilterInputConnectionField, PrefetchingConnectionField
@@ -189,6 +193,20 @@ class ProductPricingInfo(BasePricingInfo):
@key(fields="id")
class ProductVariant(CountableDjangoObjectType, MetadataObjectType):
+ # DEPRECATED
+ quantity = graphene.Int(
+ required=True,
+ description="DEPRECATED: Will be removed in favour of stock. "
+ "Quantity of a product in the store's possession, "
+ "including the allocated stock that is waiting for shipment.",
+ )
+ quantity_allocated = graphene.Int(required=False, description="DEPRECATED")
+ # DEPRECATED
+ stock_quantity = graphene.Int(
+ required=True,
+ description="DEPRECATED: Will be removed in favour to stock. "
+ "Quantity of a product available for sale.",
+ )
price_override = graphene.Field(
Money,
description=(
@@ -203,6 +221,11 @@ class ProductVariant(CountableDjangoObjectType, MetadataObjectType):
"only meant for displaying."
),
)
+ # DEPRECATED
+ is_available = graphene.Boolean(
+ description="DEPRECATED: Will be removed in favour of stock. "
+ "Whether the variant is in stock and visible or not."
+ )
attributes = gql_optimizer.field(
graphene.List(
@@ -255,7 +278,6 @@ class ProductVariant(CountableDjangoObjectType, MetadataObjectType):
"id",
"name",
"product",
- "quantity_allocated",
"sku",
"track_inventory",
"weight",
@@ -265,14 +287,26 @@ class ProductVariant(CountableDjangoObjectType, MetadataObjectType):
@staticmethod
@permission_required(StockPermissions.MANAGE_STOCKS)
- def resolve_stock(root: models.ProductVariant, info, country):
- return stock_models.Stock.objects.get_variant_stock_for_country(country, root)
+ 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
+ )
@staticmethod
@permission_required(ProductPermissions.MANAGE_PRODUCTS)
def resolve_digital_content(root: models.ProductVariant, *_args):
return getattr(root, "digital_content", None)
+ @staticmethod
+ def resolve_stock_quantity(root: models.ProductVariant, info):
+ country = info.context.country
+ stock = stock_models.Stock.objects.get_variant_stock_for_country(country, root)
+ return get_available_quantity_for_customer(stock)
+
@staticmethod
@gql_optimizer.resolver_hints(
prefetch_related=["attributes__values", "attributes__assignment__attribute"]
@@ -311,6 +345,11 @@ class ProductVariant(CountableDjangoObjectType, MetadataObjectType):
resolve_availability = resolve_pricing
+ @staticmethod
+ def resolve_is_available(root: models.ProductVariant, info):
+ country = info.context.country
+ return is_variant_in_stock(root, country)
+
@staticmethod
@permission_required(ProductPermissions.MANAGE_PRODUCTS)
def resolve_price_override(root: models.ProductVariant, *_args):
@@ -318,8 +357,10 @@ class ProductVariant(CountableDjangoObjectType, MetadataObjectType):
@staticmethod
@permission_required(ProductPermissions.MANAGE_PRODUCTS)
- def resolve_quantity(root: models.ProductVariant, *_args):
- return root.quantity
+ def resolve_quantity(root: models.ProductVariant, info):
+ country = info.context.country
+ stock = stock_models.Stock.objects.get_variant_stock_for_country(country, root)
+ return stock.quantity
@staticmethod
@permission_required(ProductPermissions.MANAGE_PRODUCTS)
@@ -328,6 +369,12 @@ class ProductVariant(CountableDjangoObjectType, MetadataObjectType):
# `resolve_report_product_sales` resolver.
return getattr(root, "quantity_ordered", None)
+ @permission_required(ProductPermissions.MANAGE_PRODUCTS)
+ def resolve_quantity_allocated(root: models.ProductVariant, info):
+ country = info.context.country
+ stock = stock_models.Stock.objects.get_variant_stock_for_country(country, root)
+ return stock.quantity_allocated
+
@staticmethod
@permission_required(
[OrderPermissions.MANAGE_ORDERS, ProductPermissions.MANAGE_PRODUCTS]
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 952823c117..1886ebeaa0 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3356,8 +3356,12 @@ type ProductVariant implements Node {
weight: Weight
privateMeta: [MetaStore]!
meta: [MetaStore]!
+ quantity: Int!
+ quantityAllocated: Int
+ stockQuantity: Int!
priceOverride: Money
pricing: VariantPricingInfo
+ isAvailable: Boolean
attributes: [SelectedAttribute!]!
costPrice: Money
margin: Int
diff --git a/saleor/product/utils/availability.py b/saleor/product/utils/availability.py
index 1644a69b01..ed74c7fe33 100644
--- a/saleor/product/utils/availability.py
+++ b/saleor/product/utils/availability.py
@@ -148,7 +148,12 @@ def get_product_availability(
)
is_on_sale = product.is_visible and discount is not None
- is_available = product.is_visible and is_product_in_stock(product, country)
+ if country is not None:
+ is_available = product.is_visible and is_product_in_stock(product, country)
+ else:
+ is_available = product.is_visible and is_product_in_stock(
+ product, settings.DEFAULT_COUNTRY
+ )
return ProductAvailability(
on_sale=is_on_sale,