Extracting return values from a query without actually running it

Is there a clever way to extract the final resulting variables returned by a neo4j query in a performant manner?

Just as an example:

MATCH p=(n)-[r]->(m) RETURN n,r,m

I want to extract n,r,m

A more complicated query would be something like:

MATCH p=(n)-[r]->(m) RETURN n,r,m AS computers LIMIT 5

In this case, I would want n,r,computers

My initial attempt at this involved using the EXPLAIN keyword and trying to pull the variables out of there, but explain doesn't give you the final resulting parameters, only the steps along the way. String processing would be extremely difficult with the many different forms of Cypher queries possible. The database knows what rows are being returned at the end of execution, so is there a way to ask the database for this information WITHOUT actually executing the query?

Hello and welcome to the Community!

The purpose of the EXPLAIN keyword is to provide you with the query plan. It does not perform the query. The only way that you can get the values from the graph is to actually execute the query. In your query:

MATCH p=(n)-[r]->(m) RETURN n,r,m

You re retrieving all nodes in the graph that have relationships to all other nodes in the graph. For a large graph this query could exhaust memory.

Are you using Neo4j Browser? If so, if you look at the result returned in the table view to see the values returned for each node. The variable p, to be set to all of the paths in the graph is not necessary unless you want to do something with it.

With this query, you could return specific properties of the nodes also, for example:

MATCH (n)-[r]->(m) RETURN n.name, m.title, r.roles

As you gain more experience with Neo4j, you will find that using node labels will be more performant:

MATCH (n:Person)-[r]->(m:Movie) RETURN n.name, m.title, r.roles


Hi Elaine,

The queries presented were just examples. My end goal here is to feed the return variables into the apoc.agg.graph function. The use case I'm working with allows users to specify their own Cypher queries, so I'm trying to find a generic solution that would allow me to extract the return variables and feed them to the apoc function I mentioned earlier.

I don't need the results of the query, I just need the resultant variables names, so that I can modify the query in flight and substitute RETURN n,r,m for RETURN apoc.agg.graph([n,r,m])