
I am a developer from Toronto, Canada and I enjoy writing about JavaScript, Angular and the .NET framework.
Search for a command to run...

I am a developer from Toronto, Canada and I enjoy writing about JavaScript, Angular and the .NET framework.
Nice article!
In this series, I will post articles going over the C# language and other .NET related topics
Using Projections - getting related data
What does 99.99% mean?

Using Projections - getting related data

Reactive programming is somewhat of a shift in thinking on how you get values from a source of data. Instead of you asking the source every few seconds for an update, the source sends you data you are interested in over time. In other words, we are t...

Learn to create your own tags like control

This article assumes that you know the basics of working with Entity Framework and that you have a sample application that has an entity that you want to expose through GraphQL.
Let's assume that you have the following model that you use to create an entity in your database:
using System.ComponentModel.DataAnnotations;
namespace Example.Models;
public class Country
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
How can we go about creating a GraphQL endpoint that exposes this model?
The answer to this is quite simple and in this article, I want to show you how to do it using the HotChocolate framework.
Here are the steps:
Install the following NuGet packages (we are assuming you already have the required nuget packages for Entity framework installed)
dotnet add package HotChocolate.AspNetCore
dotnet add package HotChocolate.Data.Entityframework
Create a Query.cs file in your project (it can be within a folder if you want) and add the following code to it:
// DbContext is in Data - referencing it here
using Example.Data;
using Example.Models;
namespace Example.GraphQL;
public class Query
{
// Service - this is a special attribute that allows us to inject a service into our resolver method
public IQueryable<Platform> GetCountries([Service] AppDbContext context) => context.Country;
}
Update the Program.cs file to add the GraphQL middleware and endpoint:
using Example.Data;
using Example.GraphQL;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add our database reference to handler
builder.Services.AddDbContext<AppDbContext>(opt => opt.UseSqlServer("name=ConStr"));
// GraphQL related config - note that we pass our Query class type
builder.Services.AddGraphQLServer()
.AddQueryType<Query>();
var app = builder.Build();
app.UseRouting();
// This will allow us to reach our graphQL endpoing
app.UseEndpoints(endpoints => { endpoints.MapGraphQL(); });
app.Run();
We are going to assume that you have a connection string to your database in the appsettings.Development.json file already and you were able to update your database and can see the tables on the database server already.
That's all! - run the application and point to the /graphql path using your favorite API testing tool. I am using postman for this example:

A few things to note:
All calls to GraphQL endpoints are done using the POST verb.
We haven't taken advantage of relationships
Concurrency has not been addressed
In the next post, we add cities into some of these countries and see how we can make GraphQL return us countries with cities inside them.