Getting Started with Hot Chocolate .NET GraphQL Server

Step 1: Create a New .NET Core Web API Project

dotnet new webapi --no-https

Step 2: Install Hot Chocolate GraphQL in the .NET Core Project

dotnet add package HotChocolate.AspNetCore dotnet add package HotChocolate.Data

Step 3: Create Your GraphQL Schema

//Actor.cs namespace Models { public class Actor { public string FirstName { get; set; } public string LastName { get; set; } } }//Movie.cs namespace Models { public class Movie { public int Id { get; set; } public string Title { get; set; } public List<Actor> Actors { get; set; } } }

Step 4: Create Your Dataset

//SeedData.cs using Models; namespace Data { public class Seed { public static List<Movie> SeedData() { var actors = new List<Actor> { new Actor { FirstName = "Bob", LastName = "Kante" }, new Actor { FirstName = "Mary", LastName = "Poppins" } }; var movies = new List<Movie> { new Movie { Id = 1, Title = "The Rise of the GraphQL Warrior", Actors = actors }, new Movie { Id = 2, Title = "The Rise of the GraphQL Warrior Part 2", Actors = actors } }; return movies; } } }

Step 5: Define Your Data Resolvers

  • At the root of your project, create a folder called Resolvers
  • Inside the Resolvers folder, create a C# file called Query.cs andpaste the following code snippet
using Data; using Models; namespace Resolvers { public class Query { public List<Movie> GetMovies() => Seed.SeedData(); public Movie GetMovieById(int id) => Seed.SeedData().FirstOrDefault(x => x.Id == id); } }

Step 6: Configure Your GraphQL Server

//Startup.cs public void ConfigureServices(IServiceCollection services) { services .AddGraphQLServer() .AddQueryType<Query>(); }
//Startup.cs public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app .UseRouting() .UseEndpoints(endpoints => { endpoints.MapGraphQL(); }); }

Step 7: Execute Your GraphQL Query

dotnet run
query { movies { title actors { firstName } } }

Step 8: Create A Public Endpoint For Your GraphQL API

  • Download the current version of ngrok
  • Register and get a token
  • Open your terminal and set the ngrok token with this command: ngrok authtoken YOUR_AUTHTOKEN
  • Create a tunnel with this command: ngrok http -host-header=localhost http://localhost:5000
  • The output of the last command will list a public forwarding URL, which will point to your local server

Step 9: Create Your Database on Hasura Cloud

  • Sign in to Hasura Cloud
  • Create a project and click on the Launch Console button
  • Navigate to the Data tab and create a new Heroku Postgres database
  • After the database has been created, click on the Create Table button in the Data tab and create the table Movies with the following columns as shown below

Step 10: Attach Your Hot Chocolate GraphQL Server With Hasura’s Remote Schema

Step 11: Merge data with Hasura Remote Join

Step 12: Test the GraphQL API

  • Navigate to the API tab and examine the query explorer. We can see it contains both the schema in the Hot-Chocolate GraphQL server and the automatically generated schema from the Postgres Hasura database instance
  • Paste the following query in the query box. (You can also explore the complete GraphQL schema in the sidebar and toggle based on the fields you want)
query MyQuery { Movies { Id MovieUrl ReleaseDate movie_metadata { title actors { firstName } } } }

Learn More About Hot Chocolate GraphQL and Hasura




⚡️ Instant realtime GraphQL APIs! Connect Hasura to your database & data sources (GraphQL, REST & 3rd party API) and get a unified data access layer instantly.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Save Historical Data in the Power BI Service with XMLA Endpoint (Incremental Refresh on ANY Source!)

How to Create a Simple WordPress Plugin

Building Simple Software

Recruitment Stories — Tahmid’s Experience at NewsCred

Day 2

Opulous Development Update: March 2022

Effective Java! Use Varargs Judiously

Breadth First Search

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


⚡️ Instant realtime GraphQL APIs! Connect Hasura to your database & data sources (GraphQL, REST & 3rd party API) and get a unified data access layer instantly.

More from Medium

Securing Tokens (JWTs, etc.) in Browsers (trying to be novel)

Managing and deploying NodeJS monorepo project

Merging Multiple TypeDefs — Organizing with Apollo Federation

Create a multi-tenancy application in Nest.js Part 1 (microservices setup)