https://github.com/pmikstacki/hotchocolatier
HotChocolate GraphQL Schema Generator for Entity Framework DbSets
https://github.com/pmikstacki/hotchocolatier
Last synced: about 1 year ago
JSON representation
HotChocolate GraphQL Schema Generator for Entity Framework DbSets
- Host: GitHub
- URL: https://github.com/pmikstacki/hotchocolatier
- Owner: pmikstacki
- License: mit
- Created: 2023-11-10T08:34:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-10T12:41:05.000Z (over 2 years ago)
- Last Synced: 2024-05-01T19:29:32.533Z (about 2 years ago)
- Language: C#
- Size: 28.3 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HotChocolatier
  [](https://www.nuget.org/packages/HotChocolatier.Adnotations/) [](https://www.nuget.org/packages/HotChocolatier.Tool/)
HotChocolate GraphQL Schema Generator for Entity Framework DbSets
It enables you to create hassle-free GraphQL APIs without having to write Schema manually (for queries and subscriptions - i still recommend using rest for writes)
## Installation
### Install adnotations package in the project that holds your DbContext
`dotnet add package HotChocolatier.Adnotations --version 1.0.0`
or
`NuGet\Install-Package HotChocolatier.Adnotations -Version 1.0.0`
### Install dotnet tool
`dotnet tool install --global HotChocolatier.Tool --version 1.0.0`
# How it Works
It generates GraphQL Schema for Query and Subscription. Example use:
### IMPORTANT NOTE
You need to add `true` to your DbContext Project
in the first propertygroup section
```XML
net7.0
enable
enable
true
```
Then you need to build the project and call HotChocolatier with following attributes:
```
hotchocolatier -a E:\Projects\GameDesignStudio\src\Backend\GameDesignStudio.Data\bin\Debug\net7.0\GameDesignStudio.Data.dll -n GameDesignStudio.GraphQL.Schema -v -o E:\Projects\GameDesignStudio\src\Backend\GameDesignStudio.GraphQL
```
If you feel lost, there's always -h that displays the manual, but here's a rundown of options:
* -a is a dll holding the DbContext class
* -n is a target namespace of generated schema
* -v is used to display verbose logs
* -o defines output directory (of course it will create its' own Schema folder with subsequent Query and Subscription folders to organize each partial classes.
It automatically names partials based on DbSet name. So example output will look like:
```CSharp
// ... inside DbContext
[GraphQlSubscription(Authorize = true),
GraphQlList(UseSorting = true, UseFiltering = true, UseProjection = true, Authorize = true),
GraphQlPagedList(UseSorting = true, UseFiltering = true, UseProjection = true, Authorize = true),
GraphQlGetById(Authorize = true)]
public DbSet Users { get; set; }
```
In order to get the attributes you need the HotChocolatier.Adnotations package.
It contains following attributes:
* GraphQLSubscription (creates subscriptions with Added and Updated Event - you need to trigger them manually from other services - see (https://chillicream.com/docs/hotchocolate/v13/defining-a-schema/subscriptions)
* GraphQLList (exposes raw IQueryable for projections, sorting etc - of course you can configure which one you want and which one you don't want)
* GraphQlPagedList (uses offset paging)
* GraphQLGetById (allows to get one item by integer id)
Output for Users DbSet will look like this (File Name: Query.Users.cs):
```CSharp
using Microsoft.EntityFrameworkCore;
using HotChocolate.Authorization;
namespace GameDesignStudio.GraphQL.Schema;
public partial class Query{
[Authorize, UseProjection, UseFiltering, UseSorting]
public IQueryable GetUsers([Service(ServiceKind.Synchronized)] GameDesignStudio.Data.Context.GameDesignStudioContext context)
=> context.Users;
[Authorize, UseOffsetPaging(DefaultPageSize = 50, IncludeTotalCount = true, MaxPageSize = 1000), UseProjection, UseFiltering, UseSorting]
public IQueryable GetUsersWithPaging([Service(ServiceKind.Synchronized)] GameDesignStudio.Data.Context.GameDesignStudioContext context)
=> context.Users;
public async ValueTask GetUser([Service(ServiceKind.Synchronized)] GameDesignStudio.Data.Context.GameDesignStudioContext context, int id)
=> await context.Users.FirstOrDefaultAsync(x => x.Id == id);
}
```