Neo4jClient new Cypher Start notation & F# Extra Top Level Operators

Some weeks ago a new version of Neo4jClient was released. There were introduced some changes in the Cypher Start notation. The new Start notation for sure has become nicer for C#, but it is still a bit ugly for F#. Recommended way of using new Start notation are anonymous classes that do not supported in F#.

The one reasonable option is to use dictionary-like interface, which in C# looks in the following way:

graphClient
    .Cypher
    .Start(new Dictionary<string, object>
    {
        { "foo", nodeRef },
        { "bar", otherNodeRef }
    });

We need to create IDictionary<string,obj> object somehow. Luckily, F# has a set of Extra Top Level Operators one of which is dict that does exactly what we need. The last thing what we need to do is to box dictionary values to convert them to obj.

dict : seq<'Key * 'Value> -> IDictionary<'Key,'Value> (requires equality)
box : 'T -> obj

Some examples:

let getById (queryObject:'T when 'T :> NeoEntity) =
    client.Cypher
        .Start(dict ["n", box(sprintf "node(%d)" (queryObject.Id))])
        .Return<Node<'T>>("n")
        .Results

let simpleConnection connectionType (target:Node<_>) (source:Node<_>) =
    client.Cypher
        .Start(dict [("n", box(source.Reference)); ("m", box(target.Reference))])
        .CreateUnique(sprintf "n-[:%s]->m" connectionType)
        .ExecuteWithoutResults()

Update: Start notation supports wide range of possible object references:

graphClient
  .Cypher
  .Start(dict [
    ("n1", box "custom");
    ("n2", box nodeRef);
    ("n3", box Node.ByIndexLookup("indexName", "property", "value"));
    ("n4", box Node.ByIndexQuery("indexName", "query"));
    ("r1", box relRef);
    ("moreRels", box [|relRef; relRef2|]);
    ("r2", box Relationship.ByIndexLookup("indexName", "property", "value"));
    ("r3", box Relationship.ByIndexQuery("indexName", "query"));
    ("all", box All.Nodes)
  ])

Discover more from Sergey Tihon's Blog

Subscribe to get the latest posts sent to your email.

4 thoughts on “Neo4jClient new Cypher Start notation & F# Extra Top Level Operators

  1. what is the advantage to have a database over plain csv file loaded into quickgraph ?
    I can think of a few, but what is your take on this ?

Leave a comment