Search code examples
neo4jclient

Default node type for Node<T>


I am starting to investigate the use of Neo4j using the neo4client API. I have created a basic database, and can query it using the web client. I am now trying to build a sample C# interface. I am having some problems with index lookups. My database consists of nodes with two properties: conceptID and fullySpecifiedName. Auto-indexing is enabled, and both node properties are listed in the node_keys_indexable property of neo4j.properties.

I keep getting IntelliSense errors in my C# when using the Node class. It appears to be defined as Node<T>, but I don't know what to supply as the value of the type. Consider this example from this forum...

var result = _graphClient
.Cypher
.Start(new
{
    n = Node.ByIndexLookup("index_name", "key_name", "Key_value")
})
.Return((n) => new
{
    N = n.Node<Item>()
})
.Results
.Single();

var n = result.N;

Where does the "Item" in Node<Item> come from? I have deduced that the index name I should use is node_auto_index, but I can't figure out a default node type.


Solution

  • Item is the type of node you have stored in the DB, so if you have you're storing a class:

    public class MyType { public int conceptId { get; set; } public string fullySpecifiedName { get;set; } }
    

    You would be retrieving Node<MyType> back.

    Simple flow:

    //Store a 'MyType'
    _graphClient.Create(new MyType{conceptId = 1, fullySpecifiedName = "Name");
    
    //Query MyType by Index
    var query = 
        _graphClient.Cypher
            .Start(new { n = Node.ByIndexLookup("node_auto_index", "conceptId", 1)
            .Return<Node<MyType>>("n");
    
    Node<MyType> result = query.Results.Single();
    
    //Get the MyType instance
    MyType myType = result.Data;
    

    You can bypass the result.Data step by doing .Return<MyType>("n") instead of Node<MyType> as you'll just get an instance of MyType in that case.