Skip to main content


Showing posts with the label Hot Chocolate

Hot Chocolate GraphQL Pagination Using UseOffsetPaging Middleware[.NET6]

In this article, we are going to understand the GraphQL pagination using the 'UseOffsetPaging' middleware. UseOffsetPaging Middleware: Hot chocolate provides default pagination middleware that is 'UseOffsetPaging'. Simply applying 'UseOffsetPaging' on the resolver method will enable the pagination. This middleware implicitly reads the variables like 'skip' and 'take' from the request query and then generates the raw SQL query from the IQueriable. Create A .NET6 Web API Project: Let's create a .Net6 Web API sample application to accomplish our demo. We can use either Visual Studio 2022 or Visual Studio Code(using .NET CLI commands) to create any .NET6 application. For this demo, I'm using the 'Visual Studio Code'(using the .NET CLI command) editor. .NET CLI Command: dotnet new webapi -o Your_Project_Name Sample Table Data: Let's have a sample Todo table as shown. Setup EntityFramework Core Database Context: Let's

A Basic GraphQL CRUD Operation With .NET6 + Hot Chocolate(V 12) + SQL Database

In this article, we are going to implement GraphQL CRUD operation in .NET6, Hot Chocolate(V12), SQL Database. GraphQL: GraphQL is an open-source data query and manipulation and language for APIs. It is a query language for your API and a server-side runtime for executing queries by using a type system you define for your data. GraphQL can be integrated into any framework like .NET, Java, NestJS, etc and it isn't tied to any specific database or storage engine and is backed by your existing code and data. GraphQL 2 main operations are like: Query(fetching data) Mutation(data manipulation like save, update, delete) Hot Chocolate GraphQL: Hot Chocolate is an open-source GraphQL server that is compliant with the newest GraphQL latest specs. It is the wrapper library of the original .NET GraphQL library. Hot Chocolate takes the complexity away from building a fully-fledged GraphQL server. Create A .NET6 Web API Project: Let's create a .Net6 Web API sample application to accompli

Hot Chocolate GraphQL Pagination Using Cursor Technique[.NET 6]

In this article, we are going to understand the Cursor Pagination technique in Hot Chocolate GraphQL. GraphQL Cursor Paging: In GraphQL we have cursor-based pagination. The cursors are opaque, either offset or ID-based pagination can be implemented. In the cursor-base pagination based on request Graphql query response return 'Edges', 'PageInfo'(object). Edges consist of an array of objects with properties like 'node', 'cursor'. So 'node' property holds single record data into it. 'cursor' is a base64 string that can be either made by the row number or record primary key id value. So each 'edge' contains 'node'(contains single record data) along with 'cursor', so using the 'cursor' value we can query our server to return the records either before or after the 'cursor' value. PageInfo consist of  'hasNextPage', 'hasPreviousPage', 'startCursor', 'endCursor'. Sample Query

.Net5 Blazor WebAssembly Application Invoke GraphQL Endpoints Using Strawberry Shake Library

In this article, we will implement a Blazor WebAssembly Application that consumes GraphQL endpoints using the Strawberry Shake library. Strawberry Shake: Strawberry Shake is a GraphQL client library that can be used by the .Net Standard Library. So all .NetCore applications (from .Net5) like APIs, MVC, Blazor Server, Blazor WebAssembly, etc. Strawberry Shake will generate all boilerplate code for the GraphQL Server schema, which lays an easy path for consuming the data from our Blazor WebAssembly application. Strawberry Shake CLI Tool Configuration: Strawberry Shake CLI needs to be configured because CLI will help us to generate the GraphQL client. Create a dotnet tool-manifest dotnet new tool-manifest Now install the Strawberry Shake CLI Tool dotnet tool install StrawberryShake.Tools --local Create A .Net5 Blazor WebAssembly Project: Let's start our journey by creating a .Net5 Blazor WebAssembly application sample project. Visual Studio users can easily create .Net5 Bl

Hot Chocolate GraphQL Custom Authentication Series Using Pure Code First Technique - Part4 - Refresh Token

Part3  we had enabled the JWT token validation and discussed different authorization techniques as well. In this article, we will understand Refresh Token creation and its usage. When To Use Refresh Token: A refresh token is a unique random encrypted string. On the expiration of the JWT auth access token, instead of showing a login page to the user, we can make the user authenticated immediately using the refresh token. By using refresh token we can fetch new user access tokens from the server without any user credentials. Generate Refresh Token: In 'AuthLogic.cs' file add a new private method like 'GenerateRefreshToken()'. Logics/AuthLogics.cs: private string GenerateRefreshToken() { var randomNumber = new byte[32]; using (var generator = RandomNumberGenerator.Create()) { generator.GetBytes(randomNumber); return Convert.ToBase64String(randomNumber); } } Here using 'System.Security.Cryptography.RandomNumberGenerator' generated refresh token of leng

Hot Chocolate GraphQL Custom Authentication Series Using Pure Code First Technique - Part3 -Validating JWT Token And Different Authorization Techniques

Part2  we had generated a JWT access token for the user authentication. In this article, we are going to validate the JWT access token and also understand different techniques of Authorization. Install JwtBearer NuGet: To enable jwt token validation service we have to install JwtBearer NuGet. Package Manager Command: Install-Package Microsoft.AspNetCore.Authentication.JwtBearer -Version 5.0.4 .Net CLI Command: dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer --version 5.0.4 Register JwtBearer Service: In the 'Startup.cs' file, we should register our JwtBearer validation service. Startup.cs: services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { var tokenSettings = Configuration .GetSection("TokenSettings").Get<TokenSettings>(); options.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = tokenSettings.Issuer, ValidateIssuer = true, ValidAudience = tokenSettings.Audienc