diff --git a/CHANGELOG.md b/CHANGELOG.md index 203e81a..d9a917a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ Note: version releases in the 0.x.y range may include both bug fixes and new features, not strictly limited to patches. +## 0.2.14 +- feat: add API to fetch query info and Graph method to check query installation + ## 0.2.13 - feat: rename parameter `secret` to `secret_alias` in `create_token` method for clarity - feat: add strict type checks for column_name formatting and raise errors on invalid inputs diff --git a/pyproject.toml b/pyproject.toml index 1b8392e..29fbc91 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "tigergraphx" -version = "0.2.13" +version = "0.2.14" description = "TigerGraphX is a high-level Python library offering a unified, Python-native interface for graph databases, advanced analytics, and GraphRAG workflows. Combining the simplicity of NetworkX with the advanced capabilities of TigerGraph, including tgCloud, it empowers Python developers to harness the power of graphs without the need to learn query languages like Cypher or GSQL." authors = ["Xuanlei Lin "] license = "MIT" diff --git a/tests/integration/core/graph_test.py b/tests/integration/core/graph_test.py index f8e765e..8abe351 100644 --- a/tests/integration/core/graph_test.py +++ b/tests/integration/core/graph_test.py @@ -356,8 +356,9 @@ def test_create_install_run_and_drop_query(self): Test creating, running, and dropping a GSQL query using the integration setup. """ # Define a GSQL query to install + query_name = "getUserPurchases" gsql_query = f""" - CREATE QUERY getUserPurchases(VERTEX user) FOR GRAPH {self.G.name} {{ + CREATE QUERY {query_name}(VERTEX user) FOR GRAPH {self.G.name} {{ Start = {{user}}; Purchases = SELECT tgt FROM Start:s - (purchased:e) -> :tgt; PRINT Purchases; @@ -369,8 +370,9 @@ def test_create_install_run_and_drop_query(self): assert success is True, "Expected query to be successfully created" # Install the query - query_name = "getUserPurchases" + assert not self.G.is_query_installed(query_name) success = self.G.install_query(query_name) + assert self.G.is_query_installed(query_name) assert success is True, "Expected query to be successfully created" # Run the query diff --git a/tigergraphx/config/endpoint_definitions.yaml b/tigergraphx/config/endpoint_definitions.yaml index 9eb9442..7baeb20 100644 --- a/tigergraphx/config/endpoint_definitions.yaml +++ b/tigergraphx/config/endpoint_definitions.yaml @@ -121,6 +121,9 @@ endpoints: path: "/restpp/query/{graph_name}/{query_name}" method: "POST" + get_query_info: + path: "/gsql/v1/queries/info?graph={graph_name}" + # ------------------------------ Upsert ------------------------------ upsert_graph_data: path: "/restpp/graph/{graph_name}" diff --git a/tigergraphx/core/graph.py b/tigergraphx/core/graph.py index 7b06236..5239827 100644 --- a/tigergraphx/core/graph.py +++ b/tigergraphx/core/graph.py @@ -174,7 +174,9 @@ def drop_graph(self) -> None: return self._schema_manager.drop_graph() # ------------------------------ Data Loading Operations ------------------------------ - def load_data(self, loading_job_config: LoadingJobConfig | Dict | str | Path) -> str: + def load_data( + self, loading_job_config: LoadingJobConfig | Dict | str | Path + ) -> str: """ Load data into the graph using the provided loading job configuration. @@ -531,6 +533,18 @@ def run_query(self, query_name: str, params: Dict = {}) -> Optional[List]: """ return self._query_manager.run_query(query_name, params) + def is_query_installed(self, query_name: str) -> bool: + """ + Check if a query is installed on the graph. + + Args: + query_name: Name of the query. + + Returns: + True if the query is installed, False otherwise. + """ + return self._query_manager.is_query_installed(query_name) + def get_nodes( self, node_type: Optional[str] = None, diff --git a/tigergraphx/core/managers/query_manager.py b/tigergraphx/core/managers/query_manager.py index 4e94ea5..866cd2b 100644 --- a/tigergraphx/core/managers/query_manager.py +++ b/tigergraphx/core/managers/query_manager.py @@ -69,6 +69,20 @@ def run_query(self, query_name: str, params: Dict = {}) -> Optional[List]: logger.error(f"Error running query {query_name}: {e}") return None + def is_query_installed(self, query_name: str) -> bool: + try: + query_info = self._tigergraph_api.get_query_info(self._graph_name) + for query in query_info: + if ( + query.get("name") == query_name + and query.get("installed") + ): + return True + return False + except Exception as e: + logger.error(f"Error checking if query {query_name} is installed: {e}") + return False + def get_nodes( self, node_type: Optional[str] = None, diff --git a/tigergraphx/core/tigergraph_api/api/query_api.py b/tigergraphx/core/tigergraph_api/api/query_api.py index 2d427da..cf61604 100644 --- a/tigergraphx/core/tigergraph_api/api/query_api.py +++ b/tigergraphx/core/tigergraph_api/api/query_api.py @@ -85,6 +85,15 @@ def run_installed_query_post( raise TypeError(f"Expected list, but got {type(result).__name__}: {result}") return result + def get_query_info(self, graph_name: str) -> List: + result = self._request( + endpoint_name="get_query_info", + graph_name=graph_name, + ) + if not isinstance(result, list): + raise TypeError(f"Expected list, but got {type(result).__name__}: {result}") + return result + def _parse_query_parameters(self, params: Dict[str, Any]) -> Dict[str, Any]: """ Parses query parameters into a dictionary suitable for HTTP requests. diff --git a/tigergraphx/core/tigergraph_api/tigergraph_api.py b/tigergraphx/core/tigergraph_api/tigergraph_api.py index f0a2fbd..e8a04c4 100644 --- a/tigergraphx/core/tigergraph_api/tigergraph_api.py +++ b/tigergraphx/core/tigergraph_api/tigergraph_api.py @@ -511,6 +511,20 @@ def run_installed_query_post( """ return self._query_api.run_installed_query_post(graph_name, query_name, params) + def get_query_info(self, graph_name: str) -> List: + """ + Retrieve information about all queries for a given graph. + + This includes query code, endpoints, parameters, and status. + + Args: + graph_name: The name of the graph. + + Returns: + API response as a list. + """ + return self._query_api.get_query_info(graph_name) + # ------------------------------ Upsert ------------------------------ def upsert_graph_data(self, graph_name: str, payload: Dict[str, Any]) -> List: """