Is there a way to set a parameter equal to query results?

Hey all,

Wondering if you can create params equal to query results.
For example, takethe creation of stringList:

:param  stringList => ["string1", "string2", "string3"]

Is there a way to create stringList equal to the result from:

return collect(ex.someStringProperty) as stringList

I think the narrow specific answer to your question is "no" -- but there's a way you can get what you want.

Instead of doing this:

:param  stringList => ["string1", "string2", "string3"]
RETURN $stringList;

Just do this:

WITH collect(ex.someStringProperty) as stringList
(your cypher here)

In other words, just run it as a pre-query, bind it to "stringList" as a variable in a cypher query, and then it functions the same as if it were a parameter, but you compute it each time.

If you don't want to compute it each time (for example because it's a big list of strings) you could compute it once, put it into a temp node, and then start with that node's value.

WITH collect(ex.someStringProperty) as stringList
CREATE (:Cache { stringList: stringList });

And then later:

MATCH (c:Cache)
WITH c.stringList as stringList
(your cypher here)
1 Like

Thanks David!

The main reason I was asking is mainly for when I'm debugging and need to use a parameter that requires some complex pre-query. Which then requires copy/pasting the pre-query for each different query I'm running relying on that param.

I had not thought about just saving the result to a temp node as you suggested which solves that problem for the most part!

Ideally I'd love to see being able to assign params to the results of a query as a feature in the future, as it saves extra steps needed to either assign the param or extra code to match on (:cache) for each query.
Perhaps I'll put that in as a feature request.

1 Like

if your pre-query is not so complex you could use e.g. pattern comprehensions or similar to turn it into an expression.

the => variant of param is actually executing a Cypher call RETURN expr
so even stuff like this works. :param foo => apoc.coll.toSet([1,1,1])

if you want to wrap a complex cypher query you can try:

:param foo => apoc.cypher.runFirstColumnMany('MATCH (n) RETURN id(n)',{})

or even with parameters:

:param foo => apoc.cypher.runFirstColumnMany('MATCH (n) WHERE id(n) < {min} RETURN id(n)',{min:12})

1 Like