Bottlecap.Net.GraphQL.Generation.Cli 1.0.2

dotnet tool install --global Bottlecap.Net.GraphQL.Generation.Cli --version 1.0.2
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local Bottlecap.Net.GraphQL.Generation.Cli --version 1.0.2
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=Bottlecap.Net.GraphQL.Generation.Cli&version=1.0.2
                    
nuke :add-package Bottlecap.Net.GraphQL.Generation.Cli --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 net6.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last Updated
1.0.2 829 6/16/2022
1.0.1 517 6/14/2022
1.0.0 524 5/2/2022
0.10.0 630 3/20/2022
0.9.1-alpha 935 1/8/2020
0.9.0-alpha 644 11/25/2019
0.8.1-alpha 630 2/20/2019
0.8.0-alpha 563 2/16/2019
0.7.0-alpha 1,734 2/15/2019
0.6.0-alpha 1,036 12/23/2018
0.5.1-alpha 695 12/20/2018
0.5.0-alpha 616 12/19/2018
0.4.1-alpha 655 12/17/2018
0.4.0-alpha 650 12/16/2018
0.3.6-alpha 628 12/12/2018
0.3.5-alpha 642 12/12/2018
0.3.4-alpha 658 12/12/2018