Creating a GraphQL .NET server with Hot Chocolate
- One Endpoint
- One Request
- Type System
- Predictable
- Realtime
- HotChocolate is designed to work seamlessly with
IQueryable
and supports deferred execution out of the box.- When you return an IQueryable from a resolver, HotChocolate will take care of executing the IQueryable.
- ensure that the query is only executed once and only when needed. (abstracted away from the developer)
- Create target service + inject it by resolver when needed to support multiple parallel operations
- Pooled dbContext for faster data fetching, retrieve one DbContext instance from the pool for each invocation of a resolver. Once the resolver has finished executing, the instance will be returned to the pool.
- Out of the box, Hot Chocolate has the ability to automatically generate API documentation from your existing XML documentation comments. Example:
/// <summary>
/// The vehicle model object.
/// </summary>
public class Vehicle
{
/// <summary>
/// The unique identifier.
/// </summary>
public required int Id { get; init; }
/// <summary>
/// The country ISO code ALPHA-2.
/// </summary>
public string? CountryIsoCodeAlpha2 { get; init; }
}
- you will need to enable documentation file generation for your
.csproj
through:
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
- Otherwise you can provide documentation through schema definition documentation.
- run
dotnet ef database update
to run db migrations. - play with the API through banana cake pop:
http://localhost:5033/graphql/
. - run the graph voyager through
http://localhost:5033/ui/voyager
.