Skip to content

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

Resolve metadata

Jakub Draganek929e540a8599
diff --git a/saleor/graphql/account/types.py b/saleor/graphql/account/types.py
index 0b03477f80..12643cf6ba 100644
--- a/saleor/graphql/account/types.py
+++ b/saleor/graphql/account/types.py
@@ -267,6 +267,13 @@ class User(CountableDjangoObjectType):
return resolve_payment_sources(root)
+ @staticmethod
+ def resolve_private_meta(root: models.User, _info):
+ return [
+ {"label": label, "metadata": data}
+ for label, data in root.private_meta.items()
+ ]
+
class ChoiceValue(graphene.ObjectType):
raw = graphene.String()
diff --git a/saleor/graphql/account/types_meta.py b/saleor/graphql/account/types_meta.py
index 8c45cd3a0a..453d14d319 100644
--- a/saleor/graphql/account/types_meta.py
+++ b/saleor/graphql/account/types_meta.py
@@ -10,9 +10,19 @@ class MetaClientStore(graphene.ObjectType):
name = graphene.String(required=True, description="Metadata clients name.")
metadata = graphene.List(MetaItem, description="Metadata stored for a client.")
+ @staticmethod
+ def resolve_metadata(root, _info):
+ return [{"key": k, "value": v} for k, v in root["metadata"].items()]
+
class MetaStore(graphene.ObjectType):
label = graphene.String(required=True, description="Name of metadata client group.")
clients = graphene.List(
MetaClientStore, description="List of clients that stored metadata in a group."
)
+
+ @staticmethod
+ def resolve_clients(root: dict, _info):
+ return [
+ {"name": key, "metadata": value} for key, value in root["metadata"].items()
+ ]
diff --git a/tests/api/test_account_meta.py b/tests/api/test_account_meta.py
index f07e54d932..a41e9baad9 100644
--- a/tests/api/test_account_meta.py
+++ b/tests/api/test_account_meta.py
@@ -1,5 +1,21 @@
+import graphene
+import pytest
from tests.api.utils import get_graphql_content
+META_LABEL = "TEST_LABEL"
+META_CLIENT = "TEST_PLUGIN"
+
+KEY = "name"
+VALUE = "Bond"
+
+
+def customer_with_meta(customer_user):
+ customer_user.store_private_meta(META_LABEL, META_CLIENT, {KEY: VALUE})
+ customer_user.save()
+ return customer_user
+
+
GET_PRIVATE_META_QUERY = """
query UserMeta($id: ID!) {
user(id: $id) {
@@ -19,8 +35,16 @@ GET_PRIVATE_META_QUERY = """
"""
-def test_get_private_meta(staff_api_client, permission_manage_users, customer_user):
- variables = {"id": customer_user.id}
+def test_get_private_meta(
+ staff_api_client, permission_manage_users, customer_with_meta
+):
+ user_id = graphene.Node.to_global_id("User", customer_with_meta.id)
+ variables = {"id": user_id}
staff_api_client.user.user_permissions.add(permission_manage_users)
response = staff_api_client.post_graphql(GET_PRIVATE_META_QUERY, variables)
- content = get_graphql_content(response)
+ meta = get_graphql_content(response)["data"]["user"]["privateMeta"][0]
+
+ assert meta["label"] == META_LABEL
+ assert meta["clients"] == [
+ {"metadata": [{"key": KEY, "value": VALUE}], "name": META_CLIENT}
+ ]