0
votes

I am trying to use Neo4jClient to run the Cypher syntax:

    UNWIND {apples} AS newApple
    CREATE (a:Apple {newApple})

with a C# list of object List<Apple> a where the object could be:

class Apple : Fruit
{
    [JsonProperty(PropertyName = "Variety")]
    public String Variety { get; set; }
}

I do not want to spread out object variable specs in different places around the code.

But running

            graphClient.Cypher
                .Unwind(a, "newApple")
                .Create("(a: Apple {newApple})")
                .ExecuteWithoutResults()

throws:

Neo4jClient.NeoException: 'ParameterNotFoundException: Expected a parameter named newApple'

Changing the Create row to

            .Create("(a: Apple {Id: newApple.Id})")

seems to work though, so the expected parameter newApple is found. The problem here is that if I change the properties of the class, I have to change the direct dependency in the cypher query string.

Questions

  1. Why is this? I would expect the identifier specified in Unwind would be found in both cases.
  2. Any workaround so that I can keep the generalized code? I aim to be able to send in any POCO object to automatically match with neo node type params.
2

2 Answers

1
votes

Because the newApple is no longer a parameter, but a variable, whereas the syntax used by you can only be applied with external parameters.

For variables can be used this way:

WITH [{id: 1, name: 'appe1'}, {id: 2, name: 'apple2'}] as apples
UNWIND apples as newApple
CREATE (a:Apple) SET a = newApple
RETURN a
0
votes

Building on Gabor's answer, for C# neo4jclient that can be achieved like this (unfolding in comments and edits over here):

graphClient.Cypher
    .Unwind(apples, "newApple")
    .Create("(a: Apple)")
    .Set("a = newApple")
    .ExecuteWithoutResults();

SET allows setting the whole object with JsonAttribute properties, without specifying explicit parameters in cypher.