GraphQL in C# (.NET): Hot Chocolate and GraphQL.NET libraries for creating APIs

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

GraphQL in C# (.NET): Overview of Libraries and Tools



GraphQL is a query language for APIs that allows clients to request only the data they actually need. In the C# (.NET) ecosystem, there are several powerful libraries for implementing GraphQL servers and clients. This article covers the two most popular ones: Hot Chocolate and GraphQL.NET. Both libraries are actively developed, open source, and have excellent documentation.



1. Hot Chocolate — A Modern Framework from ChilliCream



Hot Chocolate is a full-featured GraphQL framework for .NET that supports both server-side and client-side components. It integrates with ASP.NET Core and Entity Framework Core, offering convenient attributes and automatic schema generation.



Installation and Basic Server



To get started, install the package via NuGet:



dotnet add package HotChocolate.AspNetCore


Create a simple GraphQL server:



// Define the data typepublic class Book{    public string Id { get; set; }    public string Title { get; set; }    public string Author { get; set; }}

// Create the Query classpublic class Query{ public Book GetBook() => new Book { Id = "1", Title = "War and Peace", Author = "Leo Tolstoy" };}

// Configure in Program.csvar builder = WebApplication.CreateBuilder(args);builder.Services .AddGraphQLServer() .AddQueryType<Query>();

var app = builder.Build();app.MapGraphQL();app.Run();


After starting, the server will be available at /graphql. You can send a query using any GraphQL client:



{  book {    title    author  }}


Hot Chocolate supports mutations, subscriptions, filtering, sorting, pagination, and integration with Entity Framework via UseProjection, UseFiltering, and UseSorting.



Example with Mutation



public class Mutation{    public Book AddBook(string title, string author) =>        new Book { Id = Guid.NewGuid().ToString(), Title = title, Author = author };}

// Registrationbuilder.Services .AddGraphQLServer() .AddQueryType<Query>() .AddMutationType<Mutation>();


2. GraphQL.NET — A Lightweight and Battle-Tested Library



GraphQL.NET (also known as graphql-dotnet) is a port of the original JavaScript GraphQL implementation to C#. It offers manual schema construction through types and fields, giving you full control over the API structure.



Installation and Example



dotnet add package GraphQLdotnet add package GraphQL.SystemTextJsondotnet add package GraphQL.Server.Transports.AspNetCore


Let's create a simple server:



// Define the typepublic class BookType : ObjectGraphType<Book>{    public BookType()    {        Field(x => x.Id);        Field(x => x.Title);        Field(x => x.Author);    }}

// Create the schemapublic class AppSchema : Schema{ public AppSchema(IServiceProvider provider) : base(provider) { Query = provider.GetRequiredService<BookQuery>(); }}

// Querypublic class BookQuery : ObjectGraphType{ public BookQuery() { Field<BookType>("book") .Resolve(context => new Book { Id = "1", Title = "Crime and Punishment", Author = "F.M. Dostoevsky" }); }}

// Configure Program.csvar builder = WebApplication.CreateBuilder(args);builder.Services.AddSingleton<BookQuery>();builder.Services.AddSingleton<AppSchema>();builder.Services.AddGraphQL(b => b.AddSystemTextJson());

var app = builder.Build();\

Recommendations