A collection of examples demonstrating how to build a GraphQL API using .NET
- Project Overview
- Learn how to build a .NET 6 GraphQL API from scratch
- Learn how to define GraphQL queries, and mutations
- Learn how to provide validation and error handling
- Add EntityFramework support both in-memory and SQLite database
- Add support for query projection, filtering, sorting, and paging
- Provision Azure App Service using Azure CLI, Azure, Powershell, and Azure Bicep
- This project explores how to build and deploy a GraphQL API in a number of easy to follow examples.
- Each example introduces a new concept that is clear, small, and easy to understand
- All examples are built using .NET 6 (LTS) and ChilliCream Hot Chocolate (Open-Source GraphQL Server for Microsoft .NET platform)
- The GraphQL API is deployed to Azure App Service
- The project demonstrates how to provision an Azure App Service using Azure CLI, Azure Powershell, and Azure Bicep
All examples are based on a fictitious company called MyView, that provides a service to view games and game ratings. It also allows reviewers to post and delete game reviews. Therefore, in the examples that follow, I demonstrate how to build a GraphQL API that provides the following capabilities:
- List games
- List games with associated reviews
- Find a game by id
- Post a game review
- Delete a game review
The details of the provided examples are as follows:
- π Example 1
- Create empty API project
- Add GraphQL Server support through the use of the ChilliCream Hot Chocolate nuget package
- Create a games query
- Create a games mutation
- Add metadata to support schema documentation
- π Example 2
- Add global error handling
- Add input validation
- π Example 3
- Add EntityFramework support with an in-memory database
- Add support for query filtering, sorting, and paging
- Add projection support
- Add query type extensions to allow splitting queries into multiple classes
- π Example 4
- Change project to use SQlite instead of an in-memory database
Lastly, I provide the instructions on how to provision an Azure App Service and deploy the GraphQL API to it. The details are as follows:
- Provision Azure App Service using Azure CLI
- Provision Azure App Service using Azure Powershell
- Provision Azure App Service using Azure Bicep
- Deploy GraphQL API
π Find all the infrastructure as code here
It is recommended that the following tools and frameworks are installed before trying to run the examples:
-
All code in this example is developed with C# 10 using the latest cross-platform .NET 6 framework.
See the .NET 6 SDK official website for details on how to download and setup for your development environment.
-
Find more information on Visual Studio Code with relevant C# and .NET extensions.
-
Everything you need to install and configure your windows, linux, and macos environment
-
Official Bicep Visual Studio Extension
Provides Bicep language support
-
jq is a lightweight and flexible command-line JSON processor
-
A package of all the Visual Studio Code extensions that you will need to work with Azure
-
Tools for developing and running commands of the Azure CLI
I have provided a number of examples that show how to build a GraphQL Server using ChilliCream Hot Chocolate GraphQL Server. If you would like to learn more, please view the following learning resources:
- Official Getting Started Documentation
- Official ChilliCream Hot Chocolate Examples
- On.NET Show - Getting Started with HotChocolate
- Modern data APIs with EF Core and GraphQL
- Entity Framework Community Standup - Hot Chocolate 12 and GraphQL 2021
- GraphQL API with .NET 5 and Hot Chocolate (Still applicable to .NET 6)
- GraphQL in .NET with HotChocolate (Playlist)
- Azure Tips & Tricks - How to use GraphQL on Azure
- Hot Chocolate: GraphQL Schema Stitching with ASP.Net Core - Michael Staib - NDC London 2021
There are also a number of Hot Chocolate templates that can be installed using the dotnet CLI
tool.
- Install HotChocolate Templates:
# install Hot Chocolate GraphQL server templates (includes Azure function template) dotnet new -i HotChocolate.Templates # install template that allows you to create a GraphQL Star Wars Demo dotnet new -i HotChocolate.Templates.StarWars
-
List HotChocolate Templates
# list HotChocolate templates dotnet new --list HotChocolate
Template Name Short Name Language Tags ----------------------------------- ----------- -------- ------------------------------ HotChocolate GraphQL Function graphql-azf [C#] Web/GraphQL/Azure HotChocolate GraphQL Server graphql [C#] Web/GraphQL HotChocolate GraphQL Star Wars Demo starwars [C#] ChilliCream/HotChocolate/Demos
- Create HotChocolate project using templates
# create ASP.NET GraphQL Server dotnet new graphql --name MyGraphQLDemo # create graphql server using Azure Function dotnet new graphql-azf --name MyGraphQLAzfDemo # create starwars GraphQL demo mkdir StarWars cd StarWars dotnet new starwars
I use SemVer for versioning. For the versions available, see the tags on this repository.
- Douglas Minnaar - Initial work - drminnaar