In this article, we are going to understand the GraphQL pagination using the 'UseOffsetPaging' middleware.
Add the database connection string to the 'appsettings.Development.json'.
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
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 install the entity framework core NuGet Package.
.NET CLI Command:
dotnet add package Microsoft.EntityFrameworkCore --version 6.0.3
dotnet add package Microsoft.EntityFrameworkCore --version 6.0.3
Package Manager:
dotnet add package Microsoft.EntityFrameworkCore --version 6.0.3
dotnet add package Microsoft.EntityFrameworkCore --version 6.0.3
Let's install the entity framework core SQL NuGet Package.
.NET CLI Command:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 6.0.1
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 6.0.1
Package Manager:
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.1
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.1
Let's create an entity that represents our 'Todo' table like 'Todo.cs'
Data/Entities/Todo.cs:
namespace Dot6.HotChoco.Graphql.Paging.Demo.Data.Entities; public class Todo { public int Id { get; set; } public string ItemName { get; set; } public bool IsCompleted { get; set; } }Let's implement the Database context by creating an entity like 'MyWorldDbContext.cs'.
Data/MyWorldDbContext.cs:
using Dot6.HotChoco.Graphql.Paging.Demo.Data.Entities; using Microsoft.EntityFrameworkCore; namespace Dot6.HotChoco.Graphql.Paging.Demo.Data; public class MyWorldDbContext : DbContext { public MyWorldDbContext(DbContextOptions<MyWorldDbContext> context) : base(context) { } public DbSet<Todo> Todo{get;set;} }
- (Line: 6) To make a plain class as database context, we have to inherit the 'Microsoft.EntityFrameworkCore.DbContext'.
- (Line: 12) Entities that represent our 'Todo' table will register inside of the database context.
appsettings.Development.json:
"ConnectionStrings": { "MyWorldDbConectiton":"Your_ConnectionString_Value" }Register the database context into the 'Program.cs'.
Program.cs:
builder.Services.AddDbContext<MyWorldDbContext>(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("MyWorldDbConectiton")); });
Install Hot Chocolate NuGet Package:
Let's install the Hot Chocolate NuGet Package.
.NET CLI Command:
dotnet add package HotChocolate.AspNetCore --version 12.7.0
dotnet add package HotChocolate.AspNetCore --version 12.7.0
Package Manager:
Install-Package HotChocolate.AspNetCore -Version 12.7.0
Install-Package HotChocolate.AspNetCore -Version 12.7.0
Create GraphQL Query Entity:
So let's create GraphQL Query Entity in which we are going to implement our pagination resolver method. So let's create a query entity like 'TodoQuery.cs'.
using Dot6.HotChoco.Graphql.Paging.Demo.Data; using Dot6.HotChoco.Graphql.Paging.Demo.Data.Entities; namespace Dot6.HotChoco.Graphql.Paging.Demo.Queries; public class TodoQuery { [UseOffsetPaging] public IQueryable<Todo> GetTodo([Service] MyWorldDbContext myWorldDbContext) { return myWorldDbContext.Todo.OrderBy(_ => _.Id).AsQueryable(); } }
- (Line: 9) Decorated our resolver method with the 'UseOffsetPagging' attribute which enables the pagination.
- (Line: 10-13) Paging can be applied for return types like 'IQueryable', 'IEnumerable'. For good performance use the 'IQueryable' return type because pagination filters will be directly applied to the raw SQL query.
Register GraphQL Service And Endpoint:
Let's register the GraphQL service and endpoint middleware into the 'Program.cs'.
Program.cs:
using Dot6.HotChoco.Graphql.Paging.Demo.Data; using Dot6.HotChoco.Graphql.Paging.Demo.Queries; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); builder.Services .AddGraphQLServer() .AddQueryType<TodoQuery>(); var app = builder.Build(); // code hidden for display purpose app.MapGraphQL(); app.Run();
- (Line: 7-9) Register our GraphQL service as well as our Query entity.
- (Line: 13) Configured GraphQL endpoint.
Test Pagintaion Endpoint:
Sample Query:
query{ todo(skip: 0, take: 5) { items{ id itemName } pageInfo { hasNextPage hasPreviousPage } } }
- Here 'skip' and 'take' parameters are automatically read from the request by the 'UseOffsetPagging'.
- (Line: 3-6) Specify the required properties that need to return by the server.
- (Line: 7-10) Requesting addition pagination info like 'hasNextPage', 'hasPreviousPage' both are boolean types their value will depend on the records inside of the database.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful information on Hot Chocolate GraphQL Pagination Using UseOffsetPaging Middleware. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Comments
Post a Comment