Neo4jClient.ReturnPoly 1.0.6

dotnet add package Neo4jClient.ReturnPoly --version 1.0.6                
NuGet\Install-Package Neo4jClient.ReturnPoly -Version 1.0.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Neo4jClient.ReturnPoly" Version="1.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Neo4jClient.ReturnPoly --version 1.0.6                
#r "nuget: Neo4jClient.ReturnPoly, 1.0.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Neo4jClient.ReturnPoly as a Cake Addin
#addin nuget:?package=Neo4jClient.ReturnPoly&version=1.0.6

// Install Neo4jClient.ReturnPoly as a Cake Tool
#tool nuget:?package=Neo4jClient.ReturnPoly&version=1.0.6                

Neo4jClient.ReturnPoly

A polymorphic return function for Neo4jClient

This library adds two extension methods, and some JsonConverters that allow you to return polymorphic entities from a Neo4j database.

Available on NuGet! NuGet version (Neo4jClient.ReturnPoly)

Install-Package Neo4jClient.ReturnPoly

Getting Values

There are three primary snippets you can use to retrieve the polymorphic values from the graph

// Your standard way of doing things:
await graph.Cypher
            .Match("(n)")
            .ReturnPolymorphic<BaseType>("n")
            .ResultsAsync;

// Your standard way of doing things, but distinctly:
await graph.Cypher
            .Match("(n)")
            .ReturnDistinctPolymorphic<BaseType>("n")
            .ResultsAsync;

// The anonymous object/built return object. This method is a little more clunky, but
// there's not much to be done since the core Neo4jClient expression parser handles
// all of this
await graph.Cypher
            .Match("(n)")
            .Return(() => new {
                MyVariable = Return.As<BaseType>(PolyUtils.Poly("n"))
            })
            .ResultsAsync;

Setting up the converters

Label Converter

/* Database

	(:TypeA { PropertyA: "PropA" })
	(:TypeA:TypeB { PropertyA: "PropA", PropertyB: "PropB" })
	(:TypeB { PropertyA: "PropA", PropertyB: "PropB" })

*/

public class TypeA
{
    public string PropertyA { get; set; }
}

public class TypeB : TypeA
{
    public string PropertyB { get; set; }
}

class Program
{
    static async Task Main(string[] args)
    {
        var client = new BoltGraphClient("bolt://localhost:7687");
        await client.ConnectAsync();

        client.JsonConverters.Add(new PolymorphicJsonLabelConverter<TypeA>());

        var results = await client.Cypher
            .Match("(n)")
            .ReturnPolymorphic<TypeA>("n")
            .ResultsAsync;

        foreach (var result in results)
            Print(result);
    }
    
    static void Print(TypeA type)
    {
        Console.WriteLine(type.GetType().Name);
        Console.WriteLine("  A: " + type.PropertyA);
        if (type is TypeB b)
            Console.WriteLine("  B: " + b.PropertyB);
    }
}
Available Functions

In order to use labels to determine the proper type, you must use one of these two return functions.

ICypherFluentQuery<T> ReturnPolymorphic<T>(this ICypherFluentQuery, string)
ICypherFluentQuery<T> ReturnDistinctPolymorphic<T>(this ICypherFluentQuery, string)

Or if using a built return object, the PolyUtils.Poly(string) method can be used to create the return text. It must be used in conjunction with Return.As<T> like so:

Return.As<T>(PolyUtils.Poly(string))

If you use it a log, add using static Neo4jClient.ReturnPoly.PolyUtils; to your imports, and then its just

Return.As<T>(Poly(string))

And doesn't that just read a little better? (working with what I've got here, as I can't hook into the core expression parsing (yet))

Lambda Converter

/* Database

	Labels dont matter here
	({ PropertyA: "PropA" })
	({ PropertyA: "PropA", PropertyB: "PropB" })
	({ PropertyA: "PropA", PropertyB: "PropB" })

*/

public class TypeA
{
    public string PropertyA { get; set; }
}

public class TypeB : TypeA
{
    public string PropertyB { get; set; }
}

class Program
{
    static async Task Main(string[] args)
    {
        var client = new BoltGraphClient("bolt://localhost:7687");
        await client.ConnectAsync();

        // Since this converter doesn't use labels, you can use the regular .Return<T> functions
        // from Neo4jClient
        client.JsonConverters.Add(new PolymorphicJsonLambdaConverter<TypeA>(jo => {
        	if (jo.ContainsKey("PropertyB"))
                return typeof(TypeB);
            return typeof(TypeA);
        }));

        var results = await client.Cypher
            .Match("(n)")
            .Return<TypeA>("n")
            .ResultsAsync;
        
        foreach (var result in results)
            Print(result);
    }
    
    static void Print(TypeA type)
    {
        Console.WriteLine(type.GetType().Name);
        Console.WriteLine("  A: " + type.PropertyA);
        if (type is TypeB b)
            Console.WriteLine("  B: " + b.PropertyB);
    }
}

There are no extension methods with this style. Just use Return like you normally would

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 432 7/30/2021
1.0.5 345 5/28/2021
1.0.4 458 5/22/2021
1.0.3 322 4/6/2021
1.0.2 417 11/10/2020
1.0.1 403 11/10/2020