drminnaar / graphql-dotnet-series

A collection of examples demonstrating how to build a GraphQL API using .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

graphql-series-dotnet-header

 

Graphql For .NET

 

A collection of examples demonstrating how to build a GraphQL API using .NET

 


 

Key Takeaways

 

  • 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

 


 

Project Overview

 

 

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

 


 

Required Setup and Tools

 

It is recommended that the following tools and frameworks are installed before trying to run the examples:

 

 


Additional Learning Resources

 

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:

 

 

HotChocolate Templates

 

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
    

 


 

Versioning

 

I use SemVer for versioning. For the versions available, see the tags on this repository.

 


 

Authors

 

 


About

A collection of examples demonstrating how to build a GraphQL API using .NET


Languages

Language:C# 77.3%Language:Smalltalk 19.5%Language:Bicep 3.2%