Skip to content

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

Make AppExtensions resolvers working with pre and post data migration (#18561)

Lukasz Ostrowski1b125087d789
diff --git a/saleor/app/installation_utils.py b/saleor/app/installation_utils.py
index 7ef8c83521..dec8a7afc7 100644
--- a/saleor/app/installation_utils.py
+++ b/saleor/app/installation_utils.py
@@ -269,6 +269,7 @@ def install_app(app_installation: AppInstallation, activate: bool = False):
url=extension_data.get("url"),
mount=extension_data.get("mount"),
target=extension_data.get("target", AppExtensionTarget.POPUP),
+ # This field should be removed in 3.24, ENG-1110
http_target_method=http_target_method,
settings=extension_data.get("options"),
)
diff --git a/saleor/graphql/app/tests/queries/test_app_extension.py b/saleor/graphql/app/tests/queries/test_app_extension.py
index a2e62f141a..48b40215ec 100644
--- a/saleor/graphql/app/tests/queries/test_app_extension.py
+++ b/saleor/graphql/app/tests/queries/test_app_extension.py
@@ -482,7 +482,7 @@ def test_app_extension_with_app_query_by_customer_without_permissions(
(AppExtensionTarget.NEW_TAB, "GET"),
],
)
-def test_app_extension_type_settings(
+def test_app_extension_type_settings_from_http_target_method(
target,
method,
app,
@@ -518,3 +518,43 @@ def test_app_extension_type_settings(
if target == AppExtensionTarget.WIDGET:
assert extension_data["settings"]["widgetTarget"]["method"] == method
+
+
+def test_app_extension_type_settings_from_native_settings(
+ app,
+ staff_api_client,
+):
+ # given
+ target = "NEW_TAB"
+ method = "GET"
+
+ app_extension = AppExtension.objects.create(
+ app=app,
+ label="Create product with App",
+ url="https://www.example.com/app-product",
+ mount=AppExtensionMount.ORDER_DETAILS_WIDGETS,
+ http_target_method=method,
+ settings={"newTabTarget": {"method": method}},
+ target=target,
+ )
+ id = graphene.Node.to_global_id("AppExtension", app_extension.id)
+ variables = {"id": id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_APP_EXTENSION,
+ variables,
+ )
+
+ # then
+ content = get_graphql_content(response)
+ extension_data = content["data"]["appExtension"]
+
+ assert extension_data["mountName"] == "ORDER_DETAILS_WIDGETS"
+ assert extension_data["targetName"] == app_extension.target.upper()
+
+ if target == AppExtensionTarget.NEW_TAB:
+ assert extension_data["settings"]["newTabTarget"]["method"] == method
+
+ if target == AppExtensionTarget.WIDGET:
+ assert extension_data["settings"]["widgetTarget"]["method"] == method
diff --git a/saleor/graphql/app/tests/queries/test_app_extensions.py b/saleor/graphql/app/tests/queries/test_app_extensions.py
index 4aedff6979..e37ec0a726 100644
--- a/saleor/graphql/app/tests/queries/test_app_extensions.py
+++ b/saleor/graphql/app/tests/queries/test_app_extensions.py
@@ -339,3 +339,77 @@ def test_app_extensions_with_name_filter(
extensions_data = content["data"]["appExtensions"]["edges"]
assert len(extensions_data) == expected_count
+
+
+# Behavior specific to 3.22 which handles backwards compatibility
+ ("mount_value", "target_value", "filter", "expected_count"),
+ [
+ # Test with lowercase values in database, uppercase input
+ (
+ "order_details_widgets",
+ "widget",
+ {"mountName": ["ORDER_DETAILS_WIDGETS"]},
+ 1,
+ ),
+ ("order_details_widgets", "widget", {"targetName": "WIDGET"}, 1),
+ (
+ "order_details_widgets",
+ "widget",
+ {"mountName": ["ORDER_DETAILS_WIDGETS"], "targetName": "WIDGET"},
+ 1,
+ ),
+ # Test with lowercase values in database, lowercase input
+ (
+ "order_details_widgets",
+ "widget",
+ {"mountName": ["order_details_widgets"]},
+ 1,
+ ),
+ ("order_details_widgets", "widget", {"targetName": "widget"}, 1),
+ (
+ "order_details_widgets",
+ "widget",
+ {"mountName": ["order_details_widgets"], "targetName": "widget"},
+ 1,
+ ),
+ # Test mixed case input
+ (
+ "order_details_widgets",
+ "widget",
+ {"mountName": ["Order_Details_Widgets"]},
+ 1,
+ ),
+ ("order_details_widgets", "widget", {"targetName": "WiDgEt"}, 1),
+ ],
+)
+def test_app_extensions_case_insensitive_filter(
+ mount_value,
+ target_value,
+ filter,
+ expected_count,
+ staff_api_client,
+ app,
+):
+ # given
+ AppExtension.objects.create(
+ app=app,
+ label="Test Extension",
+ url="https://www.example.com/app-extension",
+ mount=mount_value,
+ target=target_value,
+ http_target_method="POST",
+ )
+ variables = {"filter": filter}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_APP_EXTENSIONS,
+ variables,
+ )
+
+ # then
+ content = get_graphql_content(response)
+ extensions_data = content["data"]["appExtensions"]["edges"]
+
+ assert len(extensions_data) == expected_count
diff --git a/saleor/graphql/app/types.py b/saleor/graphql/app/types.py
index 92e1bfe1f6..ad9c239c57 100644
--- a/saleor/graphql/app/types.py
+++ b/saleor/graphql/app/types.py
@@ -117,13 +117,13 @@ class AppManifestExtension(BaseObjectType):
)
mount_name = graphene.String(
- description="Name of the extension mount point in the dashboard. Replaces `mount`"
+ description="Name of the extension mount point in the dashboard. Replaces `mount`. Value returned in UPPERCASE."
+ ADDED_IN_322,
required=True,
)
target_name = graphene.String(
- description="Name of the extension target in the dashboard. Replaces `target`"
+ description="Name of the extension target in the dashboard. Replaces `target`. Value returned in UPPERCASE."
+ ADDED_IN_322,
required=True,
)
@@ -148,12 +148,10 @@ class AppManifestExtension(BaseObjectType):
@staticmethod
def resolve_target_name(root, _info: ResolveInfo):
- # Temporary upper(), but TODO return it directly from DB once we migrate DB
return (root.get("target") or "POPUP").upper()
@staticmethod
def resolve_mount_name(root, _info: ResolveInfo):
- # Temporary upper(), but TODO return it directly from DB once we migrate DB
return root["mount"].upper()
@staticmethod
@@ -197,12 +195,10 @@ class AppExtension(AppManifestExtension, ModelObjectType[models.AppExtension]):
@staticmethod
def resolve_mount_name(root: models.AppExtension, _info: ResolveInfo):
- # Temporary upper(), but TODO return it directly from DB once we migrate DB
return root.mount.upper()
@staticmethod
def resolve_target_name(root: models.AppExtension, _info: ResolveInfo):
- # Temporary upper(), but TODO return it directly from DB once we migrate DB
return root.target.upper()
@staticmethod
@@ -249,14 +245,20 @@ class AppExtension(AppManifestExtension, ModelObjectType[models.AppExtension]):
"""Return app extension settings as plain JSON with same structure as options."""
http_method = root.http_target_method
- if root.target == AppExtensionTarget.WIDGET:
+ # New data model contains settings, migration will fill the old ones
+ if root.settings:
+ return root.settings
+
+ # Fallback if settings not propagated in DB yet
+ # Make it case-insensitive due to migration logic - enum will become uppercased in DB
+ if root.target.upper() == AppExtensionTarget.WIDGET.upper():
return {
"widgetTarget": {
"method": http_method,
}
}
- if root.target == AppExtensionTarget.NEW_TAB:
+ if root.target.upper() == AppExtensionTarget.NEW_TAB.upper():
return {
"newTabTarget": {
"method": http_method,
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 15f131969e..5c868741e1 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3026,14 +3026,14 @@ type AppExtension implements Node @doc(category: "Apps") {
url: String!
"""
- Name of the extension mount point in the dashboard. Replaces `mount`
+ Name of the extension mount point in the dashboard. Replaces `mount`. Value returned in UPPERCASE.
Added in Saleor 3.22.
"""
mountName: String!
"""
- Name of the extension target in the dashboard. Replaces `target`
+ Name of the extension target in the dashboard. Replaces `target`. Value returned in UPPERCASE.
Added in Saleor 3.22.
"""
@@ -29843,14 +29843,14 @@ type AppManifestExtension @doc(category: "Apps") {
url: String!
"""
- Name of the extension mount point in the dashboard. Replaces `mount`
+ Name of the extension mount point in the dashboard. Replaces `mount`. Value returned in UPPERCASE.
Added in Saleor 3.22.
"""
mountName: String!
"""
- Name of the extension target in the dashboard. Replaces `target`
+ Name of the extension target in the dashboard. Replaces `target`. Value returned in UPPERCASE.
Added in Saleor 3.22.
"""