Adding GraphQL wrapper over REST API


How to add GraphQL wrapper over existing REST API along with Swagger.

Learning Objectives

  1. How to add GraphQL wrapper

  2. How to use the NSwag tool to generate code


  1. Require “HotChocolate.AspNetCore” from NuGet package.

  2. Require .Net 5 Framework.

  3. Visual Studio IDE.

  4. Understanding of .Net Web API project.

  5. Basic knowledge of Swagger UI.

Getting Started

GraphQL allows you to ask for what you want in a single query, save bandwidth, and reduce requests.

Step 1: Create a new project

Create a new project in ASP.Net Core Web API template for this article demonstration, as shown below.

The problem of generating helpful documentation and help pages for Web APIs. Now .Net project is by default enabled with swagger, as shown on the localhost URL

Step 2: Add GraphQL packages

Install the “HotChocolate.AspNetCore” package from NuGet gallery into the newly created solution in Visual Studio.

Step 3: Generate Manifest file

Go to file explorer > project folder and run the following command using the Dotnet Global tool.

dotnet new tool-manifest

and Install the NSwag tool

dotnet tool install NSwag.ConsoleCore

Step 4: Create swagger.json

Create swagger JSON file in project directory using below command

curl <SwaggerJsonFilePath> > swagger.json

Click as highlighted below know the swagger JSON file path


curl https://localhost:44323/swagger/v1/swagger.json > swagger.json

Step 5: Generate service files

Generate REST service file using below command with respective namespace and class name.

dotnet nswag swagger2csclient 


dotnet nswag swagger2csclient 

Step 6: Update base URL

Update the base URL in generated “ToDoService” file.

Step 7: Configure GraphQL Playground

Attach playground inside the ConfigureServices method in the “Startup.cs” file

public void ConfigureServices(IServiceCollection services){

Create GraphQL Query class as follows

Map GraphQL in Configure method

app.UseEndpoints(endpoints =>

Step 8: Run the playground

Browse the localhost URL at route “/graphql,” and the following GraphQL playground will be displayed.

That’s it, successfully integrated GraphQL playground to the .Net REST API along with swagger.

Github Sample

Live Demo

Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section.

Follow me on C# Publication, LinkedIn, Instagram, Twitter,, Pinterest, Substack, Wix

#Aspnetcore #GraphQL #Csharp #GraphqlVsRest #Dotnet

Recent Posts

See All

Design Pattern – Adapter

#Aspnetcore #AdapterDesignPattern #Csharp #DesignPatterns #Dotnet According to Gang of Four, the Adapter Pattern converts the interfaces of a class into interfaces that the client requires. In other w