Overview
We've got a better understanding of how GraphQL works, it's time to implement it!
In this lesson, we will:
- Explore the Hot Chocolate GraphQL framework
- Install and set up a Hot Chocolate server
Hot Chocolate, a .NET GraphQL framework
GraphQL by itself is a specification, a query language for APIs. To implement GraphQL in a server, we typically use a GraphQL framework.
For example, if you're a JavaScript/TypeScript developer, you might use Apollo Server.
In the .NET ecosystem, we can use Hot Chocolate, a GraphQL framework developed and maintained by ChilliCream. Here's how they introduce themselves in their docs:
Hot Chocolate is an open-source GraphQL server for the Microsoft .NET platform that is compliant with the newest GraphQL October 2021 spec + Drafts, which makes Hot Chocolate compatible to all GraphQL compliant clients like Strawberry Shake, Relay, Apollo Client, and various other GraphQL clients and tools.
Setting up Hot Chocolate
From your project directory, open up a new terminal. Let's install the Hot Chocolate package.
dotnet add package HotChocolate.AspNetCore --version 13.8.1
Note: We've chosen to include the specific version number here (which was the latest version at the time of course publication) to guarantee that the project works with the course instructions.
Next, open up the Program.cs
file in the root of the repo. Currently, it's set up to return a simple "Hello World!" message from the root of where the server is running (which by default is http://localhost:5059).
var builder = WebApplication.CreateBuilder(args);var app = builder.Build();app.MapGet("/", () => "Hello World!");app.Run();
In between the line instantiating the builder
(line 1), and the line instantiating the app
(line 2), let's add the Hot Chocolate GraphQL server to our web app's services.
builder.Services.AddGraphQLServer();
Then we'll replace the MapGet
function with MapGraphQL()
.
app.MapGraphQL();
This function adds a GraphQL endpoint to the endpoint configurations, which means our GraphQL server will be available at /graphql
.
Building with Hot Chocolate
Hot Chocolate offers three different approaches to implementing a GraphQL server: annotation-based, code-first, and schema-first.
In the schema-first approach, we write the GraphQL schema in SDL (schema definition language). Then, we need to write resolver functions to populate each field in the schema.
In both annotation-based and code-first approaches, we only write resolvers. The schema is generated automatically behind the scenes. The type systems of C# and GraphQL make this possible: C# types are mapped to GraphQL schema types, so we don't need to learn or remember the specifics of SDL syntax.
The code-first approach requires writing explicit code to define and wire up everything needed for a GraphQL schema, like the field name, field type, arguments, descriptions, and more.
The annotation-based approach uses C# attributes, which are like tags for your code. Attributes are enclosed within square brackets and can take arguments. Here's an example using the built-in ObsoleteAttribute
:
[Obsolete("ThisClass is obsolete. Use ThisClass2 instead.")]public class ThisClass {}
Hot Chocolate comes with its own attributes we can use for GraphQL development. These attributes are like a shortcut to the code that we would need to write if using the code-first approach.
You can mix and match approaches in the same project. Under the hood, both schema-first and annotation-based approaches will be translated into code-first by Hot Chocolate.
In this course, we'll be using the annotation-based approach. If you're just getting started with GraphQL, it's helpful not to have to worry about learning SDL syntax; we'll stick with familiar C# types and let Hot Chocolate do the work to generate the schema for us. Annotations also mean less boilerplate code for us!
Want to use a different approach? The Hot Chocolate documentation provides code snippets for each approach. In the next lesson, we'll provide an example of what the Hello World example might look like with schema-first and code-first, but we'll stick with annotation-based for the rest of the course.
Practice
Key takeaways
- Hot Chocolate is an open-source GraphQL server for the Microsoft .NET platform.
- Hot Chocolate offers three development approaches: schema-first, code-first, and annotation-based.
- In the code-first and annotation-based approaches, Hot Chocolate maps C# types to GraphQL schema types, reducing the need to learn the specifics of the GraphQL Schema Definition Language (SDL) syntax.
Up next
Our GraphQL server is on standby, awaiting a schema. We'll give it what it wants in the next lesson!
Share your questions and comments about this lesson
This course is currently in
You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.