Search code examples
c#neo4jcypherfluentneo4jclient

Return relationship attribute in Cypher fluent query


I have the following Neo4jClient fluent API query that is used to return localized text for Xbox games stored in my Neo4j database:

var localized = client.Cypher
       .OptionalMatch("(xb:`XboxProfile`)-[p:PLAYS]->(g:`XboxGame`)-[:LOCALE]->(l:`XboxGameLocalized`)")
       .Where((Xbox.Profile xb) => xb.GamerTag == xboxProfile.GamerTag)
       .AndWhere((Xbox.Localization.Game l) => l.Locale == locale)
       .Return((l, g, p) => new {
           Localized = l.As<Xbox.Localization.Game>(),
           Game = g.As<Xbox.Game>(),
           LastPlayed = p.As<DateTimeOffset>()
       })
       .Results.ToList();

On the PLAYS relationship, I have a LastPlayed attribute that is a DateTimeOffset that I would like to return in the query result set. Currently, p.As<DateTimeOffset> isn't working as expected. It doesn't parse the date and the date is returning as:
0001-01-01T00:00:00Z

What is the correct way to return relationship attributes/properties?


Solution

  • The issue is that p in your query is an entire object (or bag), but you're not qualifying the property you want:

    LastPlayed = p.As<DateTimeOffset>()
    

    Because it can't deserialize an entire object into a DateTimeOffset, but it's not a nullable type, you're getting the default value.

    You just need to describe the type of p, for the sake of IntelliSense:

    public class PlaysPayload
    {
        public DateTimeOffset LastPlayed { get; set; }
    }
    

    And then use this in the query, to address the property you want:

       .Return((l, g, p) => new {
           Localized = l.As<Xbox.Localization.Game>(),
           Game = g.As<Xbox.Game>(),
           LastPlayed = p.As<PlaysPayload>().LastPlayed  // <-- this line is different
       })
    

    You do not need to use Relationship<T> or RelationshipInstance<T>.

    Disclaimer: I'm typing this at 0630 in the morning, while in an airport, while they're calling my plane. I'm the author of Neo4jClient so it should be correct, but I haven't verified this specific solution.