minu7 opened a new issue #18005:
URL: https://github.com/apache/airflow/issues/18005


   ### Apache Airflow Provider(s)
   
   neo4j
   
   ### Versions of Apache Airflow Providers
   
   apache-airflow-providers-neo4j version 2.0.0
   
   ### Apache Airflow version
   
   2.1.0
   
   ### Operating System
   
   macOS Big Sur Version 11.4
   
   ### Deployment
   
   Docker-Compose
   
   ### Deployment details
   
   docker-compose version 1.29.2, build 5becea4c
   Docker version 20.10.7, build f0df350
   
   ### What happened
   
   ```python
   neo4j = Neo4jHook(conn_id=self.neo4j_conn_id)
   sql = "MATCH (n) RETURN COUNT(n)"
   result = neo4j.run(sql)
   logging.info(result)
   ```
   If i run the code snippet above i always get an empty response.
   
   ### What you expected to happen
   
   i would like to have the query response.
   
   ### How to reproduce
   
   ```python
   from airflow.models.baseoperator import BaseOperator
   from airflow.providers.neo4j.hooks.neo4j import Neo4jHook
   import logging
   
   class testNeo4jHookOperator(BaseOperator):
   
       def __init__(
               self,
               neo4j_conn_id: str,
               **kwargs) -> None:
           super().__init__(**kwargs)
           self.neo4j_conn_id = neo4j_conn_id
   
       def execute(self, context):
           neo4j = Neo4jHook(conn_id=self.neo4j_conn_id)
           sql = "MATCH (n) RETURN COUNT(n)"
           result = neo4j.run(sql)
           logging.info(result)
           return result
   ```
   I created this custom operator to test the bug.
   
   ### Anything else
   
   The bug is related to the way the hook is implemented and how the Neo4j 
driver works: 
   ```python
   def run(self, query) -> Result:
           """
           Function to create a neo4j session
           and execute the query in the session.
           :param query: Neo4j query
           :return: Result
           """
           driver = self.get_conn()
           if not self.connection.schema:
               with driver.session() as session:
                   result = session.run(query)
           else:
               with driver.session(database=self.connection.schema) as session:
                   result = session.run(query)
           return result
   ```
   In my opinion, the above hook run method should be changed to:
   ```python
       def run(self, query) -> Result:
           """
           Function to create a neo4j session
           and execute the query in the session.
   
   
           :param query: Neo4j query
           :return: Result
           """
           driver = self.get_conn()
           logging.info('neo4j nostro')
           if not self.connection.schema:
               with driver.session() as session:
                   result = session.run(query)
                   return result.data()
           else:
               with driver.session(database=self.connection.schema) as session:
                   result = session.run(query)
                   return result.data()
   ```
   This is because when trying to get result.data (or result in general) 
externally the session is always empty.
   I tried this solution and it seems to work. 
   
   ### Are you willing to submit PR?
   
   - [X] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [X] I agree to follow this project's [Code of 
Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to