We are using Neo4j Community 3.2.2 with Neo4jClient 2.0.0.9 and try to create a node if it does not exist. This is covered in the cypher examples and questions like this here on SO, so we thought that should be pretty straight forward:
public class KlientNode
{
[JsonProperty(PropertyName = "id")]
public Guid Id { get; set; }
}
and:
var neuerKlient = new KlientNode { Id = ev.KlientId };
var kq = graphClient.Cypher
.Merge("(klient:Klient { id: {klientId} })")
.OnCreate()
.Set("klient = {neuerKlient}").WithParams(new
{
klientId = neuerKlient.Id,
neuerKlient
});
Console.WriteLine(kq.Query.DebugQueryText);
kq.ExecuteWithoutResults();
So we basically copied the example 1:1.
Unfortunately, this leads to the following output and exception:
MERGE (klient:Klient { id: "80248429-ea80-4a5d-8d4e-88dc1499ea8a" })
ON CREATE
SET klient = {
"id": "80248429-ea80-4a5d-8d4e-88dc1499ea8a"
}
Neo4jClient.NeoException: SyntaxError: Invalid input 'N': expected 'p/P' (line 5, column 2 (offset: 250))
"ON CREATE"
^
The cause seem to be the quotes around the "id"
in the SET klient = ...
query. If I paste the generated query to the neo4j web console, it shows a syntax error, and if I remove the quotation marks, the query runs just fine.
Anyone has an idea what might be causing the broken query when we just seem to copy the examples almost verbatim?
FWIW, I'm not sure why that happened, but we were able to solve it as follows:
var kq = graphClient.Cypher
.Merge($"(klient:{NodeName} {{ id: {{klientId}} }})")
.OnCreate()
.Set("klient = {neuerKlient}").WithParams(new
{
klientId = ev.KlientId,
neuerKlient = new KlientNode
{
Id = ev.KlientId,
},
});
Console.WriteLine(kq.Query.DebugQueryText);
kq.ExecuteWithoutResults();
The debug output is still the same (ignore the different random Guid):
MERGE (klient:Klient { id: "87798b47-ab1b-49b7-9c5e-018cd244465e" })
ON CREATE
SET klient = {
"id": "87798b47-ab1b-49b7-9c5e-018cd244465e"
}
and the query is still broken if I try to paste it into the neo4j web frontend:
However, the query now executes without an exception.
Since the only change is the shorthand definition of the neuerKlient
property in the anonymous object, I assume there is some behaviour internally that was causing the error (even though the debug query output was the same).