Creating a GraphQL Backend with .NET 6

Vinod Pal
3 min readJun 25, 2023

--

Photo by Markus Winkler on Unsplash

In this article, I will walk you through the process of creating a GraphQL backend using .NET Core 6 and the Hot Chocolate library. GraphQL is a powerful query language for APIs that allows clients to request specific data in a flexible and efficient manner. With the latest enhancements in .NET Core 6, you can take advantage of new features and improvements to build a robust GraphQL API. By following along with this tutorial, you will learn how to set up the project, define the schema, implement resolvers, configure dependency injection, and test your GraphQL API.

Prerequisites:

To complete this tutorial, you should have a basic understanding of .NET Core and C#. Ensure that you have .NET Core 6 SDK installed on your machine.

Setting Up the Project

Create a new .NET Core project:

  • Open a command prompt or terminal.
  • Navigate to the directory where you want to create your project.
  • Run the following command to create a new .NET Core project:
dotnet new web -n myGraphQLApi

Install Hot Chocolate package

cd myGraphQLApi
dotnet add package HotChocolate.AspNetCore

Let’s begin the process of creating a basic GraphQL backend.

To create our query node, we’ll start by defining a public Query class. We’ll register our first operation, which is a method called Hello, within this class. Next, we need to add the necessary GraphQL-related services to the dependency injection container. We achieve this by invoking the AddGraphQLServer method on the ServiceCollection and registering our newly created QueryType.

var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddGraphQLServer()
.AddQueryType<Query>();
var app = builder.Build();

app.MapGraphQL();

app.Run();

To complete the setup, we just need to call app.MapGraphQL. This step will ensure that our GraphQL server is up and running, ready to handle incoming requests.

By following these steps, we will have a fully functional GraphQL server in place.

Now create a class name Query.cs in the same folder

public class Query {
public string Hello() => "World";
}

Now run the project by command line as below or use Visual Studio

dotnet run

Once the project is running open the url in the browser and at the end append /graphql.

So in my case the below was my url

https://localhost:7044/

Now after appending /graphql the url becomes

https://localhost:7044/graphql/

This will open up Banana cake pop tool, here you can play with your newly created graphql backend.

Congratulations! You have successfully created a GraphQL backend with .NET Core 6 and Hot Chocolate. You learned how to set up the project, define the schema, implement resolvers, configure dependency injection, and test your GraphQL API. This tutorial provides a solid foundation for building more complex GraphQL APIs using .NET Core 6. Experiment with additional types, mutations, and resolvers to tailor the API to your specific application needs.

Make sure to explore the Hot Chocolate documentation for more advanced features, such as data loaders, subscriptions, and schema stitching, to further enhance your GraphQL backend. Keep learning and leveraging the power of GraphQL and .NET Core to build scalable and efficient APIs. Happy coding!

--

--

Vinod Pal

Just your friendly neighborhood full-stack developer. Building awesome applications, one line of code at a time.