"""Graphql Connector.""" import uuid from typing import Any, Dict, Optional from lambdacommon.common_config import logger from lambdacommon.graphql import graphql from config import APPLICATION_NAME, GRAPHQL_ROUTER_URL, OA_USER graphql_router = graphql.GraphQLConnector( GRAPHQL_ROUTER_URL, APPLICATION_NAME ) def execute_graphql_query( query: str, variables: Optional[Dict[str, Any]] = None, user_id: Optional[str] = None, additional_headers: Optional[Dict[str, str]] = None ) -> Any: """Execute a GraphQL query with optional variables and headers. Args: query (str): The GraphQL query string. variables (Optional[Dict[str, Any]]): Query variables. user_id (Optional[str]): Orchard user ID to use in headers. additional_headers (Optional[Dict[str, str]]): Any additional headers. Returns: Any: The data part of the GraphQL response. """ correlation_id = str(uuid.uuid4()) headers = { 'Correlation-Id': correlation_id, 'Orchard-User-Id': user_id or OA_USER, } if additional_headers: headers.update(additional_headers) try: graphql_router.set_headers(headers) response = graphql_router.execute(query, variables or {}) return response['data'] except graphql.GraphQLError as gql_err: logger.error(f'GraphQL error occurred (Correlation-ID: {correlation_id}): {gql_err}') raise except Exception: logger.exception(f'Unexpected error during GraphQL execution (Correlation-ID: {correlation_id})') raise