Exploring GraphQL with .NET and Hot Chocolate: A Simplified Approach
Written on
Chapter 1: Introduction to GraphQL and .NET
In the realm of web development, I've encountered various implementations of GraphQL across languages like Node.js, Java, and Python. While the complexity varies—some frameworks being more user-friendly than others—they fundamentally share the same principles. This raises the question: how intricate is the setup in .NET? Let’s dive into the code.
Creating a New Project
To begin, you'll want to set up a new project. Execute the following command:
dotnet new web -o graphql-medium -f net8.0
Next, you need to add the Hot Chocolate package:
dotnet add package HotChocolate.AspNetCore
Why Hot Chocolate?
While GraphQL.NET is a widely used framework for implementing GraphQL, it requires significant configuration, even for basic setups. Although powerful, I prefer a more straightforward approach. I concentrate on immediate needs rather than future scalability.
Hot Chocolate, developed by ChilliCream, provides an entire ecosystem for GraphQL in .NET, which includes:
- Hot Chocolate: For building GraphQL services.
- Strawberry Shake: For creating GraphQL clients.
- Banana Cake Pop: A Monaco-based GraphQL IDE for running queries and exploring endpoints.
- Green Donut: Enhances data loading performance.
Adjusting launchSettings.json
GraphQL operates on a single endpoint URL, so we need to modify the launch URL to graphql. I’ve also changed the application port for consistency during development; feel free to choose a setup that suits you.
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "graphql",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"}
}
Setting Up the Service
In GraphQL, queries are essential for data retrieval. Create a new folder named Service and add a Query.cs file within it:
namespace graphql_medium.Service;
public class Query
{
public string GetGreeting() => "Hello World!";
}
Integrating GraphQL into Our Project
Enable GraphQL server-side functionality by adding a statement to register the query type, which we’ll utilize shortly:
builder.Services
.AddGraphQLServer()
.AddQueryType();
Next, modify the GET request mapping for the base URL to return a more descriptive message, indicating that the API is operational:
To set up the HTTP pipeline, ensure you map GraphQL as an endpoint before the Run call:
app.MapGraphQL();
Running the Project
With everything configured, it's time to launch the app:
dotnet run --launch-profile "https"
Executing a Query
Click the Create Document button, type in greeting (the name of our query), and hit the Run button.
If you’re keen on diving deeper into GraphQL and Hot Chocolate, check out my article on integrating Entity Framework into this application.
Conclusion
We’ve successfully executed our first GraphQL query! Setting up Hot Chocolate is impressively straightforward. While this is just the beginning, it offers a solid foundation for further exploration.
If you found this information helpful and want to join our expanding community, please hit the follow button. Your thoughts and feedback are always appreciated, so feel free to share!
Thank You for Reading!
Before you leave, consider giving a clap and following the writer! 👏 Connect with us on X, LinkedIn, YouTube, and Discord. Explore our other platforms: In Plain English, CoFeed, and Venture.
Chapter 2: Video Resources
In this chapter, we will explore some helpful videos that can enhance your understanding of Hot Chocolate and GraphQL.
First Look at Hot Chocolate Fusion with Aspire - This video offers a comprehensive introduction to Hot Chocolate Fusion, showcasing its features and capabilities.
Port to .NET 6 and Summary - GRAPHQL API IN .NET w/ HOT CHOCOLATE #14 - This video discusses transitioning to .NET 6, providing a summary of the GraphQL API in .NET with Hot Chocolate.