Bottlecap.Net.GraphQL.Generation.Attributes
1.0.2
dotnet add package Bottlecap.Net.GraphQL.Generation.Attributes --version 1.0.2
NuGet\Install-Package Bottlecap.Net.GraphQL.Generation.Attributes -Version 1.0.2
<PackageReference Include="Bottlecap.Net.GraphQL.Generation.Attributes" Version="1.0.2" />
paket add Bottlecap.Net.GraphQL.Generation.Attributes --version 1.0.2
#r "nuget: Bottlecap.Net.GraphQL.Generation.Attributes, 1.0.2"
// Install Bottlecap.Net.GraphQL.Generation.Attributes as a Cake Addin
#addin nuget:?package=Bottlecap.Net.GraphQL.Generation.Attributes&version=1.0.2
// Install Bottlecap.Net.GraphQL.Generation.Attributes as a Cake Tool
#tool nuget:?package=Bottlecap.Net.GraphQL.Generation.Attributes&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
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
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
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 | Versions 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. |
-
.NETStandard 2.1
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Bottlecap.Net.GraphQL.Generation.Attributes:
Package | Downloads |
---|---|
Bottlecap.Net.GraphQL.Generation
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.2 | 1,327 | 6/16/2022 |
1.0.1 | 690 | 6/14/2022 |
1.0.0 | 691 | 5/2/2022 |
0.10.0 | 967 | 3/20/2022 |
0.9.1-alpha | 538 | 1/8/2020 |
0.9.0-alpha | 877 | 11/25/2019 |
0.8.1-alpha | 583 | 2/20/2019 |
0.8.0-alpha | 583 | 2/16/2019 |
0.7.0-alpha | 1,508 | 2/15/2019 |
0.6.0-alpha | 1,070 | 12/23/2018 |
0.5.1-alpha | 637 | 12/20/2018 |
0.5.0-alpha | 583 | 12/19/2018 |
0.4.1-alpha | 611 | 12/17/2018 |
0.4.0-alpha | 596 | 12/16/2018 |
0.3.6-alpha | 612 | 12/12/2018 |
0.3.5-alpha | 572 | 12/12/2018 |
0.3.4-alpha | 567 | 12/12/2018 |
0.3.3-alpha | 668 | 12/12/2018 |
0.3.2.1-alpha | 611 | 12/12/2018 |
0.3.2-alpha | 737 | 12/11/2018 |
0.3.0-alpha | 564 | 11/17/2018 |
0.2.0-alpha | 591 | 11/14/2018 |
0.1.0-alpha | 602 | 11/14/2018 |
## [1.0.2](https://github.com/BottlecapDave/Bottlecap.Net.GraphQL.Generation/compare/v1.0.1...v1.0.2) (2022-06-16)
### Bug Fixes
* **core:** Added verbose logs for template overrides ([398c759](https://github.com/BottlecapDave/Bottlecap.Net.GraphQL.Generation/commit/398c759f43b003fc1c2041ecceabd1428ee2225f))