cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Dynamically filling in label in a query gives an error

adamsits
Node

In the query below I am passing in two strings, $node and $targetlabel. The first one works, the second creates an error.

		query := `MATCH ({name:$node})-[e]-(n:$targetlabel) RETURN DISTINCT n.name as name, n.detail as detail, TYPE(e) as link`
		records, err := tx.Run(query, map[string]interface{}{
			"node":        node,
			"targetlabel": targetlabel,
		})

I get the error:

2022/01/17 21:39:11 error querying node: Neo4jError: Neo.ClientError.Statement.SyntaxError (Invalid input '$': expected an identifier (line 1, column 29 (offset: 28))
"MATCH ({name:$node})-[e]-(n:$targetlabel) RETURN DISTINCT n.name as name, n.detail as detail, TYPE(e) as link"
                             ^)

The query works just fine when I replace $targetlabel with the actual label. Am I doing something wrong?

  • Go
2 REPLIES 2

koji
Ninja
Ninja

Hi @adamsits

Parameters cannot be used for the labels.

glilienfield
Ninja
Ninja

What I do in this scenario is format the cypher string with the label condition before passing to the query. In Java, I do the following:

String cypher = String.format("match(n) where n:%s return n", label.cypherLabel);

the label I want gets replaced in the where clause. You could add it in the node as well:

String cypher = String.format("match(n:%s) return n", label.cypherLabel);