Bottlecap.Net.GraphQL.Generation 1.0.2

dotnet add package Bottlecap.Net.GraphQL.Generation --version 1.0.2
NuGet\Install-Package Bottlecap.Net.GraphQL.Generation -Version 1.0.2
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="Bottlecap.Net.GraphQL.Generation" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Bottlecap.Net.GraphQL.Generation --version 1.0.2
#r "nuget: Bottlecap.Net.GraphQL.Generation, 1.0.2"
#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 Bottlecap.Net.GraphQL.Generation as a Cake Addin
#addin nuget:?package=Bottlecap.Net.GraphQL.Generation&version=1.0.2

// Install Bottlecap.Net.GraphQL.Generation as a Cake Tool
#tool nuget:?package=Bottlecap.Net.GraphQL.Generation&version=1.0.2

Bottlecap.Net.GraphQL.Generation

The purpose of this is to support the generation of GraphTypes for .Net GraphQL based on existing entities.

Why does this exist?

I strive for seperation of concerns when building my applications. However, I also don't like repeating code and performing boiler plate tasks.

Therefore, I created this toolset to generate extendable GraphType objects based on defined entity/data classes.

How does it work?

Bottlecap.Net.GraphQL.Generation.Attributes

Nuget

This provides attributes which are used to decorate classes that you wish to have graph types and dataloaders generated for. The aim was to support bespoke attributing at a minimum for most scenarios.

GraphType

Any class that has this attribute will have a GraphQL type generated for it. Specify IsInput if the graph type is used for input.

This will generate a GraphType field for each public property with descriptions defined using the System.ComponentModel.Description attribute.

If there are properties you don't want to expose, then there is a GraphTypeProperty attribute, which has an IsIgnored flag.

DataLoaders

This will generate an extension method for GraphQL.IDataLoaderContextAccessor for each public method in the class that returns a Task<IDictionary<,>>.

Bottlecap.Net.GraphQL.Generation

Nuget

This contains the main generation logic. If you're wanting to upgrade the generator, or generate types without using attributes, then this is the assembly you will want.

GraphType Example

Below is an example of a data record defined for graph type generation

[GraphType]
public class User
{
    [Description("The id of the user")]
    public long Id { get; set; }

    [Description("The username of the user")]
    public string Username { get; set; }

    [GraphTypeProperty(IsIgnored = true)]
    [Description("The password of the user")]
    public string Password { get; set; }
}

and here is the result...

public partial class UserGraphType : ObjectGraphType<GraphQLExample.Data.User>
{
    public UserGraphType()
    {
        Name = "User";
        Field(x => x.Id, nullable: false)
        .Description("The id of the user");
        Field(x => x.Username, nullable: false)
        .Description("The username of the user");
        SetupFields();
    }

    partial void SetupFields();
}

DataLoader Example

Below is an example of a method defined for data loader generation

[DataLoaders]
public interface IUserRepository
{
    Task<IDictionary<long, User>> GetUsersByIdsAsync(IEnumerable<long> ids);
}

and here is the result...

public static class IUserRepositoryExtensions
{
     public static Task<GraphQLExample.Data.User> GetUsersByIdsAsync(this IDataLoaderContextAccessor accessor, 
      GraphQLExample.Data.IUserRepository repository,
      Func<System.Int64> keySelector)
    {
        var loader = accessor.Context.GetOrAddBatchLoader<System.Int64, GraphQLExample.Data.User>("GraphQLExample.Data.IUserRepository.GetUsersByIdsAsync", repository.GetUsersByIdsAsync);
        return loader.LoadAsync(keySelector()).GetResultAsync();
    }
}

Bottlecap.Net.GraphQL.Generation.Cli

Nuget

This is the CLI that utilises Bottlecap.Net.GraphQL.Generation by taking a given dll, and looking for all classes that implement a Bottlecap.Net.GraphQL.Generation.Attributes attribute. It will then generate all GraphQL types in the specified namespace at the specified output location.

Installation

You can install the application using dotnet tool install.

e.g.

dotnet tool install Bottlecap.Net.GraphQL.Generation.Cli --version 0.3.2-alpha --tool-path "C:\Repos\Applications\ProjectX\tools"
Using
Argument Description
-i The dll to load and find all Bottlecap.Net.GraphQL.Generation.Attributes to generate GraphQL types from. This can have muliple values (e.g. -i path\to\first\dll path\to\second\dll)
-o The output path of the generated classes. This will generate a single class file.
-n The namespace the generated class will be in
-v Verbose mode for the Cli.
-t The root directory that houses template overrides. These should be named as the full type name of the corresponding C# template class. Integration tests have an example of using this facility.

An example of running the application would look like the following.

bottlecap-graphql-gen -i <<INPUT DLL> -o <<OUTPUT>> -n GraphQLExample.Schemas
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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.2 452 6/16/2022
1.0.1 413 6/14/2022
1.0.0 464 5/2/2022
0.10.0 611 3/20/2022
0.9.1-alpha 513 1/8/2020
0.9.0-alpha 390 11/25/2019
0.8.1-alpha 512 2/20/2019
0.8.0-alpha 516 2/16/2019
0.7.0-alpha 453 2/15/2019
0.6.0-alpha 516 12/23/2018
0.5.1-alpha 560 12/20/2018
0.5.0-alpha 475 12/19/2018
0.4.1-alpha 534 12/17/2018
0.4.0-alpha 546 12/16/2018
0.3.6-alpha 567 12/12/2018
0.3.5-alpha 519 12/12/2018
0.3.4-alpha 580 12/12/2018
0.3.3-alpha 593 12/12/2018
0.3.2.1-alpha 581 12/12/2018
0.3.2-alpha 653 12/11/2018
0.3.1-alpha 561 11/21/2018
0.3.0-alpha 517 11/17/2018
0.2.0-alpha 527 11/14/2018
0.1.0-alpha 546 11/14/2018