Introduction
This article will discuss GraphQL basics and one example using .NET Core Web API.
Agenda
- Introduction
- Example
- Features
- Basic Concepts
- Implementation using .NET Core 7 API
Prerequisites
- Visual Studio 2022
- .NET Core 7 SDK
What is GraphQL?
- GraphQL is an open-source server-side technology developed by Facebook to optimize RESTful API calls.
- GraphQL is a query language for API and a server-side runtime for executing queries and collecting data from different sources.
- Using GraphQL, we can fetch data from that we want nothing more than that using a single endpoint.

- GraphQL is strongly typed, so we can validate our query within GraphQL before execution, which helps us build stronger APIs.
- GraphQL is fast and stable and always gives us predictable results.
- If you want to learn more about GraphQL, then check the official website of GraphQL.
Example
- Suppose we have one product application service that will return product details like Id, Product Name, Product Description, Product Price, and Product Stock.
- But, one of the users wants to get the product name and stock details, which has an API endpoint like https://localhost:5000/api/v1/getproductdetails; it will return all other fields with over-fetching. So, this problem we can solve this by using GraphQL, as shown below.
query{
productList {
id,
productName,
productStock
}
}
{
"data" : {
"productList":[
{
"id": "26c48bfe-66a1-442d-8795-46c701e8ed02",
"productName": "IPhone",
"productStock": 100
},
{
"id": "bd957f94-dc15-46aa-bbeb-5d59e78e88b8",
"productName" : "Samsung TV",
"productStock": 120
},
{
"id" : "1a560b5e-ccb5-4a94-8bc3-15b287bcbd43",
"productName" : "Mobile",
"productStock" : 120
}
What are the Features of GraphQL?
- GraphQL deals with a different database.
- Human readable syntax.
- Using GraphQL doesn’t get over fetching problems.
- Fetch data using a single API call.
What are the components of GraphQL?
GraphQL has the following components, and each one has its purpose.
- Query – The query is the API request sent by the client that we used to fetch the values, and it has fields and arguments using which we can fetch data as per requirement.
- Schema – The schema is used to shape our available data, and GraphQL has Schema Definition Language (SDL) for that to write schema.
- Types – The types are the most important feature of GraphQL that helps us to create a custom object and use it to fetch data.
- Mutation- The mutation is used for operation sent to the server to create, update and delete the data.
To learn more about types, queries, and schemas, visit the official GraphQL website.
How to implement GraphQL in ASP.Net Core 7 Web API?
We use Hot Chocolate for GraphQL in this article.
What is Hot Chocolate GraphQL?
- Hot Chocolate is the open-source GraphQL server for the Microsoft .NET Platform.
- It reduces complexity and focuses on delivering things fast.
- Hot chocolate is compatible with all clients, like Apollo Client, Relay, and many more.
Step 1
Open the visual studio and create the new .NET Core Web API.
Step 2
Install the following NuGet Packages.

Step 3
Create Product Details class.
namespace graphql_demo.Entities
{
public class ProductDetails
{
public Guid Id { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public int ProductPrice { get; set; }
public int ProductStock { get; set; }
}
}
Step 4
Next, add DbContextClass inside the data folder.
using graphql_demo.Entities;
using Microsoft.EntityFrameworkCore;
namespace graphql_demo.Data
{
public class DbContextClass : DbContext
{
public DbContextClass(DbContextOptions<DbContextClass>
options) : base(options)
{
}
public DbSet<ProductDetails> Products { get; set; }
}
}
Step 5
Add seed data class inside data, which initially helps add some records when we run the application.
using graphql_demo.Entities;
using Microsoft.EntityFrameworkCore;
namespace graphql_demo.Data
{
public class SeedData
{
public static void Initialize(IServiceProvider serviceProvider)
{
using (var context = new DbContextClass(
serviceProvider.GetRequiredService<DbContextOptions<DbContextClass>>()))
{
if (context.Products.Any())
{
return;
}
context.Products.AddRange(
new ProductDetails
{
Id = Guid.NewGuid(),
ProductName = "IPhone",
ProductDescription = "IPhone 14",
ProductPrice = 120000,
ProductStock = 100
},
new ProductDetails
{
Id = Guid.NewGuid(),
ProductName = "Samsung TV",
ProductDescription = "Smart TV",
ProductPrice = 400000,
ProductStock = 120
});
context.SaveChanges();
}
}
}
}






Shahrzad OssatiPosted Oct 11, 2024, 10:53 AM
Thank you so much for this course. I have learned a lot :)