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

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

Step 9: Create Your Database on Hasura Cloud

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

query MyQuery { Movies { Id MovieUrl ReleaseDate movie_metadata { title actors { firstName } } } }

Learn More About Hot Chocolate GraphQL and Hasura

--

--

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