IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| gRPC APIs in .NET Core | Asynchronous-Messaging | |
GraphQL with .NET Core |
GraphQL is a query language for APIs and a server-side runtime for executing those queries. In contrast to traditional REST APIs, which rely on multiple, fixed endpoints, GraphQL typically uses a single endpoint. This allows clients to request exactly the data they need, addressing issues like over-fetching (getting too much data) and under-fetching (making multiple requests for related data).
In .NET Core, several libraries facilitate GraphQL implementation. One of the most popular is Hot Chocolate, a feature-rich and developer-friendly framework that integrates seamlessly with ASP.NET Core.
HotChocolate.AspNetCore, HotChocolate.Datadotnet new webapi -n GraphQLDemo
dotnet add package HotChocolate.AspNetCore
public class Book {
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
}
public class Query {
public Book GetBook() => new Book { Id = 1, Title = "GraphQL Guide", Author = "Shivshanker" };
}
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddGraphQLServer()
.AddQueryType<Query>();
var app = builder.Build();
app.MapGraphQL();
app.Run();
Navigate to https://localhost:{port}/graphql and use the Banana Cake Pop UI to test queries like:
{
book {
id
title
author
}
}
First, create a new ASP.NET Core Web API project.
dotnet add package HotChocolate.AspNetCore dotnet add package HotChocolate.Data.EntityFramework
// Models/Product.cs
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
// Data/ApplicationDbContext.cs
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{ }
public DbSet<Product> Products { get; set; }
}
// Services/ProductService.cs
public class ProductService
{
private readonly ApplicationDbContext _context;
public ProductService(ApplicationDbContext context) => _context = context;
public IQueryable<Product> GetProducts() => _context.Products;
}
// GraphQL/Query.cs
using HotChocolate.Data;
public class Query
{
[UseProjection]
[UseFiltering]
[UseSorting]
public IQueryable<Product> GetProducts([Service] ProductService service)
{
return service.GetProducts();
}
}
var builder = WebApplication.CreateBuilder(args);
// Add services for GraphQL
builder.Services.AddPooledDbContextFactory<ApplicationDbContext>(opt =>
opt.UseInMemoryDatabase("ProductsDb"));
builder.Services.AddScoped<ProductService>();
builder.Services
.AddGraphQLServer()
.AddQueryType<Query>()
.AddProjections()
.AddFiltering()
.AddSorting()
.AddInMemorySubscriptions(); // For real-time functionality
var app = builder.Build();
// Seed data
await using (var scope = app.Services.CreateAsyncScope())
{
var context = scope.ServiceProvider.GetRequiredService<IDbContextFactory<ApplicationDbContext>>().CreateDbContext();
context.Products.AddRange(
new Product { Id = 1, Name = "Laptop", Price = 1200 },
new Product { Id = 2, Name = "Keyboard", Price = 75 }
);
await context.SaveChangesAsync();
}
app.MapGraphQL();
app.Run();
Run the application and navigate to the /graphql endpoint. Hot Chocolate provides a browser-based IDE (Banana Cake Pop) where you can explore the schema and execute queries.
query {
products(where: { name: { contains: "key" } }, first: 10) {
name
price
}
}
| gRPC APIs in .NET Core | Asynchronous-Messaging | |