From 94577e40607ca51df74c46b7c34c2d4f5175c188 Mon Sep 17 00:00:00 2001
From: Mohammad Torkashvand <mohammad.torkashvand@geant.org>
Date: Fri, 13 Dec 2024 18:29:13 +0100
Subject: [PATCH] add custom customer type and resolver to the graphql

---
 gso/__init__.py                       |  3 ++-
 gso/graphql_api/resolvers/__init__.py |  1 +
 gso/graphql_api/resolvers/customer.py | 30 +++++++++++++++++++++++++++
 3 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 gso/graphql_api/resolvers/__init__.py
 create mode 100644 gso/graphql_api/resolvers/customer.py

diff --git a/gso/__init__.py b/gso/__init__.py
index ccbb2d0d..56c9c7d4 100644
--- a/gso/__init__.py
+++ b/gso/__init__.py
@@ -17,6 +17,7 @@ import gso.workflows  # noqa: F401
 from gso.api import router as api_router
 from gso.auth.oidc import oidc_instance
 from gso.auth.opa import graphql_opa_instance, opa_instance
+from gso.graphql_api.resolvers.customer import custom_subscription_interface
 from gso.graphql_api.types import GSO_SCALAR_OVERRIDES
 from gso.settings import load_oss_params
 
@@ -35,7 +36,7 @@ def init_gso_app() -> OrchestratorCore:
     app.register_authentication(oidc_instance)
     app.register_authorization(opa_instance)
     app.register_graphql_authorization(graphql_opa_instance)
-    app.register_graphql()
+    app.register_graphql(subscription_interface=custom_subscription_interface)
     app.include_router(api_router, prefix="/api")
 
     if app_settings.EXECUTOR == ExecutorType.WORKER:
diff --git a/gso/graphql_api/resolvers/__init__.py b/gso/graphql_api/resolvers/__init__.py
new file mode 100644
index 00000000..decfd822
--- /dev/null
+++ b/gso/graphql_api/resolvers/__init__.py
@@ -0,0 +1 @@
+"""resolvers module."""
diff --git a/gso/graphql_api/resolvers/customer.py b/gso/graphql_api/resolvers/customer.py
new file mode 100644
index 00000000..b8187a69
--- /dev/null
+++ b/gso/graphql_api/resolvers/customer.py
@@ -0,0 +1,30 @@
+"""This module contains the resolver for the customer field in the subscription and process types."""
+
+import strawberry
+from orchestrator.graphql.schemas.customer import CustomerType
+from orchestrator.graphql.schemas.process import ProcessType
+from orchestrator.graphql.schemas.subscription import SubscriptionInterface
+from orchestrator.graphql.utils.override_class import override_class
+
+from gso.services.partners import get_partner_by_id
+
+
+async def resolve_customer(root: CustomerType) -> CustomerType:
+    """Resolve the customer field for a subscription or process."""
+    partner = get_partner_by_id(root.customer_id)
+
+    return CustomerType(
+        customer_id=partner.partner_id,
+        fullname=partner.name,
+        shortcode=partner.email,
+    )
+
+
+customer_field = strawberry.field(
+    resolver=resolve_customer,
+    description="Returns customer of a subscription",
+)
+customer_field.name = "customer"  # type: ignore[attr-defined]
+
+override_class(ProcessType, [customer_field])  # type: ignore[list-item]
+custom_subscription_interface = override_class(SubscriptionInterface, [customer_field])  # type: ignore[list-item]
-- 
GitLab