Skip to content

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

Add channel-slug to Page type (#17927)

Maciej Korycinskie848f046c912
diff --git a/saleor/graphql/core/context.py b/saleor/graphql/core/context.py
index 5c223b1a62..a44afd71fb 100644
--- a/saleor/graphql/core/context.py
+++ b/saleor/graphql/core/context.py
@@ -1,9 +1,9 @@
import datetime
from dataclasses import dataclass
-from typing import TYPE_CHECKING, Any, TypeVar
+from typing import TYPE_CHECKING, Any, Generic, TypeVar
from django.conf import settings
-from django.db.models import QuerySet
+from django.db.models import Model, QuerySet
from django.http import HttpRequest
from django.utils.functional import empty
@@ -89,7 +89,10 @@ class ChannelContext(BaseContext[N]):
channel_slug: str | None
+M = TypeVar("M", bound=Model)
+
+
@dataclass
-class ChannelQsContext:
- qs: QuerySet
+class ChannelQsContext(Generic[M]):
+ qs: QuerySet[M]
channel_slug: str | None
diff --git a/saleor/graphql/page/filters.py b/saleor/graphql/page/filters.py
index b7f1d9ae2a..2316af92fa 100644
--- a/saleor/graphql/page/filters.py
+++ b/saleor/graphql/page/filters.py
@@ -7,6 +7,7 @@ from graphql import GraphQLError
from ...attribute import AttributeInputType
from ...attribute.models import AssignedPageAttributeValue, Attribute, AttributeValue
from ...page import models
+from ..core.context import ChannelQsContext
from ..core.doc_category import DOC_CATEGORY_PAGES
from ..core.filters import (
FilterInputObjectType,
@@ -41,14 +42,15 @@ from ..utils.filters import (
from .types import Page, PageType
-def search_pages(qs, value):
+def search_pages(channel_qs: ChannelQsContext, value):
if not value:
- return qs
- return qs.filter(
+ return channel_qs
+ channel_qs.qs = channel_qs.qs.filter(
Q(title__trigram_similar=value)
| Q(slug__trigram_similar=value)
| Q(content__icontains=value)
)
+ return channel_qs
def filter_page_page_types(qs, _, value):
diff --git a/saleor/graphql/page/resolvers.py b/saleor/graphql/page/resolvers.py
index b763306ea7..fd1fda5d1b 100644
--- a/saleor/graphql/page/resolvers.py
+++ b/saleor/graphql/page/resolvers.py
@@ -1,5 +1,6 @@
+from ...channel.models import Channel
from ...page import models
-from ..core.context import get_database_connection_name
+from ..core.context import ChannelQsContext, get_database_connection_name
from ..core.utils import from_global_id_or_error
from ..core.validators import validate_one_of_args_is_in_query
from ..utils import get_user_or_app_from_context
@@ -39,11 +40,18 @@ def resolve_page(info, global_page_id=None, slug=None, slug_language_code=None):
return page
-def resolve_pages(info):
+def resolve_pages(
+ info, channel_slug: str | None = None, channel: Channel | None = None
+) -> ChannelQsContext[models.Page]:
requestor = get_user_or_app_from_context(info.context)
- return models.Page.objects.using(
- get_database_connection_name(info.context)
- ).visible_to_user(requestor)
+ if channel is None and channel_slug is not None:
+ # If channel is provided but not found, return None
+ page_qs = models.Page.objects.none()
+ else:
+ page_qs = models.Page.objects.using(
+ get_database_connection_name(info.context)
+ ).visible_to_user(requestor)
+ return ChannelQsContext(qs=page_qs, channel_slug=channel_slug)
def resolve_page_type(info, id):
diff --git a/saleor/graphql/page/schema.py b/saleor/graphql/page/schema.py
index 58714f44f2..63a53dd5ff 100644
--- a/saleor/graphql/page/schema.py
+++ b/saleor/graphql/page/schema.py
@@ -1,8 +1,9 @@
import graphene
+from ..channel.dataloaders import ChannelBySlugLoader
from ..core import ResolveInfo
from ..core.connection import create_connection_slice, filter_connection_queryset
-from ..core.context import ChannelContext, ChannelQsContext
+from ..core.context import ChannelContext
from ..core.descriptions import ADDED_IN_321, ADDED_IN_322, DEPRECATED_IN_3X_INPUT
from ..core.doc_category import DOC_CATEGORY_PAGES
from ..core.enums import LanguageCodeEnum
@@ -43,6 +44,10 @@ class PageQueries(graphene.ObjectType):
description="Language code of the page slug, omit to use primary slug."
+ ADDED_IN_321,
),
+ channel=graphene.String(
+ description="Slug of a channel for which the data should be returned."
+ + ADDED_IN_322
+ ),
description="Look up a page by ID or slug.",
doc_category=DOC_CATEGORY_PAGES,
)
@@ -61,6 +66,10 @@ class PageQueries(graphene.ObjectType):
where=PageWhereInput(
description="Where filtering options for pages." + ADDED_IN_322
),
+ channel=graphene.String(
+ description="Slug of a channel for which the data should be returned."
+ + ADDED_IN_322
+ ),
description="List of the shop's pages.",
doc_category=DOC_CATEGORY_PAGES,
)
@@ -82,24 +91,43 @@ class PageQueries(graphene.ObjectType):
@staticmethod
def resolve_page(
- _root, info: ResolveInfo, *, id=None, slug=None, slug_language_code=None
+ _root,
+ info: ResolveInfo,
+ *,
+ id=None,
+ slug=None,
+ slug_language_code=None,
+ channel=None,
):
- page = resolve_page(info, id, slug, slug_language_code)
- if not page:
- return None
- return ChannelContext(page, channel_slug=None)
+ def _resolve_page(channel_instance):
+ if channel is not None and channel_instance is None:
+ # If channel is provided but not found, return None
+ return None
+ page = resolve_page(info, id, slug, slug_language_code)
+ if page is None:
+ return None
+ return ChannelContext(page, channel_slug=channel)
+
+ if channel:
+ return ChannelBySlugLoader(info.context).load(channel).then(_resolve_page)
+ return _resolve_page(channel_instance=None)
@staticmethod
- def resolve_pages(_root, info: ResolveInfo, **kwargs):
- qs = resolve_pages(info)
- search = kwargs.get("search") or kwargs.get("filter", {}).get("search")
- if search:
- qs = search_pages(qs, search)
- qs = ChannelQsContext(qs, channel_slug=None)
- qs = filter_connection_queryset(
- qs, kwargs, allow_replica=info.context.allow_replica
- )
- return create_connection_slice(qs, info, kwargs, PageCountableConnection)
+ def resolve_pages(_root, info: ResolveInfo, *, channel=None, **kwargs):
+ def _resolve_pages(channel_instance):
+ qs = resolve_pages(info, channel_slug=channel, channel=channel_instance)
+ search = kwargs.get("search") or kwargs.get("filter", {}).get("search")
+ if search:
+ qs = search_pages(qs, search)
+
+ qs = filter_connection_queryset(
+ qs, kwargs, allow_replica=info.context.allow_replica
+ )
+ return create_connection_slice(qs, info, kwargs, PageCountableConnection)
+
+ if channel:
+ return ChannelBySlugLoader(info.context).load(channel).then(_resolve_pages)
+ return _resolve_pages(channel_instance=None)
@staticmethod
def resolve_page_type(_root, info: ResolveInfo, *, id):
diff --git a/saleor/graphql/page/tests/queries/test_page.py b/saleor/graphql/page/tests/queries/test_page.py
index 11d2dc4200..6e2727a8ef 100644
--- a/saleor/graphql/page/tests/queries/test_page.py
+++ b/saleor/graphql/page/tests/queries/test_page.py
@@ -18,6 +18,7 @@ PAGE_QUERY = """
id
title
slug
+ created
pageType {
id
}
@@ -787,3 +788,232 @@ def test_page_attribute_with_referenced_product_object(
assert value["referencedObject"]["id"] == graphene.Node.to_global_id(
"Product", product.id
)
+
+
+QUERY_PAGE_WITH_ATTRIBUTE_AND_CHANNEL = """
+query Page($id: ID!, $channel: String) {
+ page(id: $id, channel: $channel) {
+ attributes {
+ attribute {
+ id
+ slug
+ }
+ values {
+ reference
+ referencedObject {
+ __typename
+ ... on Page {
+ id
+ }
+ ... on ProductVariant {
+ id
+ pricing {
+ onSale
+ }
+ }
+ ... on Product {
+ id
+ created
+ pricing {
+ onSale
+ }
+ }
+ }
+ }
+ }
+ }
+}
+"""
+
+
+def test_page_attribute_with_referenced_page_object_and_channel_slug(
+ staff_api_client,
+ page_type_page_reference_attribute,
+ permission_manage_pages,
+ page,
+ channel_USD,
+):
+ # given
+ staff_api_client.user.user_permissions.add(permission_manage_pages)
+
+ # Create a second page to reference
+ referenced_page = Page.objects.create(
+ title="Referenced Page",
+ slug="referenced-page",
+ page_type=page.page_type,
+ is_published=True,
+ )
+
+ page_type = page.page_type
+ page_type.page_attributes.all().delete()
+ page_type.page_attributes.add(page_type_page_reference_attribute)
+
+ attribute_value = AttributeValue.objects.create(
+ attribute=page_type_page_reference_attribute,
+ name=f"Page {referenced_page.pk}",
+ slug=f"page-{referenced_page.pk}",
+ reference_page=referenced_page,
+ )
+
+ associate_attribute_values_to_instance(
+ page, {page_type_page_reference_attribute.pk: [attribute_value]}
+ )
+
+ query = QUERY_PAGE_WITH_ATTRIBUTE_AND_CHANNEL
+
+ # when
+ variables = {
+ "id": graphene.Node.to_global_id("Page", page.pk),
+ "channel": channel_USD.slug,
+ }
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ page_data = content["data"]["page"]
+ page_attributes = page_data["attributes"]
+ assert len(page_attributes) == 1
+
+ attribute_with_reference = page_attributes[0]
+ assert attribute_with_reference["attribute"]["slug"] == "page-reference"
+
+ assert len(attribute_with_reference["values"]) == 1
+ value = attribute_with_reference["values"][0]
+ assert value["reference"] == graphene.Node.to_global_id("Page", referenced_page.id)
+ assert value["referencedObject"]["__typename"] == "Page"
+ assert value["referencedObject"]["id"] == graphene.Node.to_global_id(
+ "Page", referenced_page.id
+ )
+
+
+def test_page_attribute_with_referenced_product_variant_object_and_channel_slug(
+ staff_api_client,
+ page_type_variant_reference_attribute,
+ permission_manage_pages,
+ page,
+ product,
+ channel_USD,
+):
+ # given
+ staff_api_client.user.user_permissions.add(permission_manage_pages)
+
+ product_variant = product.variants.first()
+ page_type = page.page_type
+ page_type.page_attributes.all().delete()
+ page_type.page_attributes.add(page_type_variant_reference_attribute)
+
+ attribute_value = AttributeValue.objects.create(
+ attribute=page_type_variant_reference_attribute,
+ name=f"Variant {product_variant.pk}",
+ slug=f"variant-{product_variant.pk}",
+ reference_variant=product_variant,
+ )
+
+ associate_attribute_values_to_instance(
+ page,
+ {page_type_variant_reference_attribute.pk: [attribute_value]},
+ )
+
+ query = QUERY_PAGE_WITH_ATTRIBUTE_AND_CHANNEL
+
+ # when
+ variables = {
+ "id": graphene.Node.to_global_id("Page", page.pk),
+ "channel": channel_USD.slug,
+ }
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ page_data = content["data"]["page"]
+ page_attributes = page_data["attributes"]
+ assert len(page_attributes) == 1
+
+ attribute_with_reference = page_attributes[0]
+ assert attribute_with_reference["attribute"]["slug"] == "variant-reference"
+
+ assert len(attribute_with_reference["values"]) == 1
+ value = attribute_with_reference["values"][0]
+ assert value["reference"] == graphene.Node.to_global_id(
+ "ProductVariant", product_variant.id
+ )
+ assert value["referencedObject"]["__typename"] == "ProductVariant"
+ # having pricing object means that we passed channel_slug to the variant
+ assert value["referencedObject"]["pricing"]
+
+
+def test_page_attribute_with_referenced_product_object_and_channel_slug(
+ staff_api_client,
+ page_type_product_reference_attribute,
+ permission_manage_pages,
+ page,
+ product,
+ channel_USD,
+):
+ # given
+ staff_api_client.user.user_permissions.add(permission_manage_pages)
+
+ page_type = page.page_type
+ page_type.page_attributes.all().delete()
+ page_type.page_attributes.add(page_type_product_reference_attribute)
+
+ attribute_value = AttributeValue.objects.create(
+ attribute=page_type_product_reference_attribute,
+ name=f"Product {product.pk}",
+ slug=f"product-{product.pk}",
+ reference_product=product,
+ )
+
+ associate_attribute_values_to_instance(
+ page,
+ {page_type_product_reference_attribute.pk: [attribute_value]},
+ )
+
+ query = QUERY_PAGE_WITH_ATTRIBUTE_AND_CHANNEL
+
+ # when
+ variables = {
+ "id": graphene.Node.to_global_id("Page", page.pk),
+ "channel": channel_USD.slug,
+ }
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ page_data = content["data"]["page"]
+ page_attributes = page_data["attributes"]
+ assert len(page_attributes) == 1
+
+ attribute_with_reference = page_attributes[0]
+ assert attribute_with_reference["attribute"]["slug"] == "product-reference"
+
+ assert len(attribute_with_reference["values"]) == 1
+ value = attribute_with_reference["values"][0]
+ assert value["reference"] == graphene.Node.to_global_id("Product", product.id)
+ assert value["referencedObject"]["__typename"] == "Product"
+ # having pricing object means that we passed channel_slug to the product
+ assert value["referencedObject"]["pricing"]
+
+
+def test_page_channel_not_found(staff_api_client, page, permission_manage_pages):
+ # given
+ staff_api_client.user.user_permissions.add(permission_manage_pages)
+
+ variables = {
+ "id": graphene.Node.to_global_id("Page", page.pk),
+ "channel": "not-existing-channel",
+ }
+ query = """
+ query Page($id: ID!, $channel: String) {
+ page(id: $id, channel: $channel) {
+ id
+ }
+ }
+ """
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ assert content["data"]["page"] is None
diff --git a/saleor/graphql/page/tests/queries/test_pages.py b/saleor/graphql/page/tests/queries/test_pages.py
index 868873fdb2..e983e9be15 100644
--- a/saleor/graphql/page/tests/queries/test_pages.py
+++ b/saleor/graphql/page/tests/queries/test_pages.py
@@ -3,6 +3,8 @@ import pytest
from django.utils import timezone
from freezegun import freeze_time
+from .....attribute.models.base import AttributeValue
+from .....attribute.utils import associate_attribute_values_to_instance
from .....page.models import Page
from .....tests.utils import dummy_editorjs
from ....tests.utils import get_graphql_content
@@ -369,3 +371,188 @@ def test_query_pages_by_customer(api_client, page_list, page):
content = get_graphql_content(response)
data = content["data"]["pages"]["edges"]
assert len(data) == page_count - 1
+
+
+PAGES_QUERY_WITH_ATTRIBUTE_AND_CHANNEL = """
+ query ($channel: String) {
+ pages(first: 10, channel: $channel) {
+ edges {
+ node {
+ id
+ title
+ slug
+ pageType {
+ id
+ }
+ content
+ contentJson
+ attributes {
+ attribute {
+ slug
+ }
+ values {
+ id
+ slug
+ reference
+ referencedObject {
+ __typename
+ ... on Page {
+ id
+ }
+ ... on ProductVariant {
+ id
+ pricing {
+ onSale
+ }
+ }
+ ... on Product {
+ id
+ created
+ pricing {
+ onSale
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+"""
+
+
+def test_pages_attribute_with_referenced_product_variant_object_and_channel_slug(
+ staff_api_client,
+ page_type_variant_reference_attribute,
+ permission_manage_pages,
+ page,
+ product,
+ channel_USD,
+):
+ # given
+ staff_api_client.user.user_permissions.add(permission_manage_pages)
+
+ product_variant = product.variants.first()
+ page_type = page.page_type
+ page_type.page_attributes.all().delete()
+ page_type.page_attributes.add(page_type_variant_reference_attribute)
+
+ attribute_value = AttributeValue.objects.create(
+ attribute=page_type_variant_reference_attribute,
+ name=f"Variant {product_variant.pk}",
+ slug=f"variant-{product_variant.pk}",
+ reference_variant=product_variant,
+ )
+
+ associate_attribute_values_to_instance(
+ page,
+ {page_type_variant_reference_attribute.pk: [attribute_value]},
+ )
+
+ query = PAGES_QUERY_WITH_ATTRIBUTE_AND_CHANNEL
+
+ # when
+ variables = {
+ "channel": channel_USD.slug,
+ }
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ pages_data = content["data"]["pages"]["edges"]
+ assert len(pages_data) == 1
+ page_data = pages_data[0]["node"]
+ page_attributes = page_data["attributes"]
+ assert len(page_attributes) == 1
+
+ attribute_with_reference = page_attributes[0]
+ assert attribute_with_reference["attribute"]["slug"] == "variant-reference"
+
+ assert len(attribute_with_reference["values"]) == 1
+ value = attribute_with_reference["values"][0]
+ assert value["reference"] == graphene.Node.to_global_id(
+ "ProductVariant", product_variant.id
+ )
+ assert value["referencedObject"]["__typename"] == "ProductVariant"
+ # having pricing object means that we passed channel_slug to the variant
+ assert value["referencedObject"]["pricing"]
+
+
+def test_pages_attribute_with_referenced_product_object_and_channel_slug(
+ staff_api_client,
+ page_type_product_reference_attribute,
+ permission_manage_pages,
+ page,
+ product,
+ channel_USD,
+):
+ # given
+ staff_api_client.user.user_permissions.add(permission_manage_pages)
+
+ page_type = page.page_type
+ page_type.page_attributes.all().delete()
+ page_type.page_attributes.add(page_type_product_reference_attribute)
+
+ attribute_value = AttributeValue.objects.create(
+ attribute=page_type_product_reference_attribute,
+ name=f"Product {product.pk}",
+ slug=f"product-{product.pk}",
+ reference_product=product,
+ )
+
+ associate_attribute_values_to_instance(
+ page,
+ {page_type_product_reference_attribute.pk: [attribute_value]},
+ )
+
+ query = PAGES_QUERY_WITH_ATTRIBUTE_AND_CHANNEL
+
+ # when
+ variables = {
+ "channel": channel_USD.slug,
+ }
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ pages_data = content["data"]["pages"]["edges"]
+ assert len(pages_data) == 1
+ page_data = pages_data[0]["node"]
+ page_attributes = page_data["attributes"]
+ assert len(page_attributes) == 1
+
+ attribute_with_reference = page_attributes[0]
+ assert attribute_with_reference["attribute"]["slug"] == "product-reference"
+
+ assert len(attribute_with_reference["values"]) == 1
+ value = attribute_with_reference["values"][0]
+ assert value["reference"] == graphene.Node.to_global_id("Product", product.id)
+ assert value["referencedObject"]["__typename"] == "Product"
+ # having pricing object means that we passed channel_slug to the product
+ assert value["referencedObject"]["pricing"]
+
+
+def test_pages_attribute_with_incorrect_channel_slug(
+ staff_api_client,
+ page_type_variant_reference_attribute,
+ permission_manage_pages,
+ page,
+ product,
+ channel_USD,
+):
+ # given
+ staff_api_client.user.user_permissions.add(permission_manage_pages)
+
+ query = PAGES_QUERY_WITH_ATTRIBUTE_AND_CHANNEL
+
+ # when
+ variables = {
+ "channel": "non-existing-channel-slug",
+ }
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ content = get_graphql_content(response)
+ pages_data = content["data"]["pages"]["edges"]
+ assert len(pages_data) == 0
diff --git a/saleor/graphql/product/tests/test_attributes.py b/saleor/graphql/product/tests/test_attributes.py
index a9ca8c517f..e941ccbe3b 100644
--- a/saleor/graphql/product/tests/test_attributes.py
+++ b/saleor/graphql/product/tests/test_attributes.py
@@ -46,10 +46,16 @@ QUERY_PRODUCT_AND_VARIANTS_ATTRIBUTES = """
... on ProductVariant {
id
created
+ pricing {
+ onSale
+ }
}
... on Product {
id
created
+ pricing {
+ onSale
+ }
}
}
}
@@ -72,10 +78,16 @@ QUERY_PRODUCT_AND_VARIANTS_ATTRIBUTES = """
... on ProductVariant {
id
created
+ pricing {
+ onSale
+ }
}
... on Product {
id
created
+ pricing {
+ onSale
+ }
}
}
}
@@ -1904,6 +1916,8 @@ def test_product_attribute_with_referenced_product_variant_object(
assert value["referencedObject"]["id"] == graphene.Node.to_global_id(
"ProductVariant", product_variant.id
)
+ # having pricing object means that we passed channel_slug to the variant
+ assert value["referencedObject"]["pricing"]
def test_product_attribute_with_referenced_product_object(
@@ -1954,6 +1968,8 @@ def test_product_attribute_with_referenced_product_object(
assert value["referencedObject"]["id"] == graphene.Node.to_global_id(
"Product", product.id
)
+ # having pricing object means that we passed channel_slug to the product
+ assert value["referencedObject"]["pricing"]
def test_product_variant_attribute_with_referenced_page_object(
@@ -2061,6 +2077,8 @@ def test_product_variant_attribute_with_referenced_product_variant_object(
assert value["referencedObject"]["id"] == graphene.Node.to_global_id(
"ProductVariant", product_variant.id
)
+ # having pricing object means that we passed channel_slug to the variant
+ assert value["referencedObject"]["pricing"]
def test_product_variant_attribute_with_referenced_product_object(
@@ -2113,3 +2131,5 @@ def test_product_variant_attribute_with_referenced_product_object(
assert value["referencedObject"]["id"] == graphene.Node.to_global_id(
"Product", product.id
)
+ # having pricing object means that we passed channel_slug to the product
+ assert value["referencedObject"]["pricing"]
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index a2e3e79c80..c96126098f 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -684,6 +684,13 @@ type Query {
Added in Saleor 3.21.
"""
slugLanguageCode: LanguageCodeEnum
+
+ """
+ Slug of a channel for which the data should be returned.
+
+ Added in Saleor 3.22.
+ """
+ channel: String
): Page @doc(category: "Pages")
"""List of the shop's pages."""
@@ -708,6 +715,13 @@ type Query {
"""
where: PageWhereInput
+ """
+ Slug of a channel for which the data should be returned.
+
+ Added in Saleor 3.22.
+ """
+ channel: String
+
"""Return the elements in the list that come before the specified cursor."""
before: String
diff --git a/saleor/graphql/translations/schema.py b/saleor/graphql/translations/schema.py
index d5c511fc86..b832b45e3a 100644
--- a/saleor/graphql/translations/schema.py
+++ b/saleor/graphql/translations/schema.py
@@ -120,7 +120,7 @@ class TranslationQueries(graphene.ObjectType):
elif kind == TranslatableKinds.CATEGORY:
qs = resolve_categories(info)
elif kind == TranslatableKinds.PAGE:
- qs = resolve_pages(info)
+ qs = resolve_pages(info).qs
elif kind == TranslatableKinds.SHIPPING_METHOD:
qs = resolve_shipping_methods(info)
elif kind == TranslatableKinds.VOUCHER: