What is the proper usage of `apoc.cypher.run` with params?

I'm new to neo4j and want to execute a cypher statement using apoc.cypher.run(cypher, params) substituting placeholders in the statement by dynamic content using the params argument.

Unfortunately the docs at https://neo4j.com/labs/apoc/4.1/overview/apoc.cypher/apoc.cypher.run/#usage-apoc.cypher.run do not show an example on how to properly use params.

I tried the following:

CALL apoc.cypher.run("MATCH (n) WHERE n:$label RETURN n", {label: "MyLabel"})
YIELD value
RETURN value.n

but I receive the following error:

Failed to invoke procedure apoc.cypher.run: Caused by: org.neo4j.exceptions.SyntaxException: Invalid input '$': expected whitespace or a label name (line 1, column 46 (offset: 45)) " WITH $label as label MATCH (n) WHERE n:$label RETURN n"

Can somebody tell me how to do it correctly?


Solution 1:

Here is three examples that maybe will help:

"Query string concatenation" based on result from previous statement (simulated with WITH "Person" as label)

WITH "Person" as label
CALL apoc.cypher.run("MATCH (n) WHERE n:"+ label + " RETURN n",{})
YIELD value
RETURN value

"Query param" that goes into "query string concatenation"

:param label=> "Person" //Set query parameter for testing in neo4j browser:
CALL apoc.cypher.run("MATCH (n) WHERE n:"+ $label + " RETURN n",{})
YIELD value
RETURN value

"Parameterized" but then it has to be used as a param

CALL apoc.cypher.run("MATCH (n) WHERE $l in labels(n) RETURN n",{ l:"Person"})
YIELD value
RETURN value

In your example the part where n:$label is not allowed in cypher.