

How to Add GraphQL Wrapper Over REST API
source link: https://hackernoon.com/how-to-add-graphql-wrapper-over-rest-api
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Learning Objectives
- How to add GraphQL wrapper
- How to use the NSwag tool to generate code
Prerequisites
- Require “HotChocolate.AspNetCore” from the NuGet package.
- Require .Net 5 Framework.
- Visual Studio IDE.
- Understanding of .Net Web API project.
- 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 the 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 a swagger JSON file in the project directory using the below command
curl <SwaggerJsonFilePath> > swagger.json
Click as highlighted below know the swagger JSON file path
Example
curl https://localhost:44323/swagger/v1/swagger.json > swagger.json
Step 5: Generate service files
Generate REST service file using the below command with respective namespace and class name.
dotnet nswag swagger2csclient
/input:swagger.json
/classname:<Clasname>
/namespace:<Namespace>
/output:<OutputFile>
Example
dotnet nswag swagger2csclient
/input:swagger.json
/classname:RestClass
/namespace:RestNamespace
/output:ToDoService.cs
Step 6: Update the 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){
services.AddHttpClient<RestClass>();
services
.AddRouting()
.AddGraphQLServer()
.AddQueryType<Query>();
}
Create GraphQL Query class as follows
public class Query
{
public async Task<ICollection<TodoReader.WeatherForecast>> GetTodosAsync(
[Service] TodoService service,
CancellationToken cancellationToken)
{
return await service.WeatherForecastAsync(cancellationToken);
}
}
Map GraphQL in Configure method
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapGraphQL();
});
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
ssukhpinder/DotNetCoreRestApi *Add graphql and swagger to REST API. Contribute to ssukhpinder/DotNetCoreRestApi development by creating an account on…*github.com
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, Dev.to, Pinterest, Substack, Wix
Also published here.
Recommend
-
16
Mikä on GraphQL ja miten se eroaa REST-rajapinnoista (API)? Teknologiamaailmassa tunnetusti riittää jargonia. Toinen toistaan parempi teknologia tuntuu seuraavan toisiaan. Viime vuos...
-
5
REST API to OpenApi to GraphQLThe journey from undocumented untyped API to typed GraphQL using GraphQL-MeshI love GraphQL. I really believe it is the next generation of API implementation. It i...
-
10
-
14
我们知道,两个单独的应用程序需要中介程序才能相互通信。因此,开发人员通常会搭建桥梁(应用程序编程接口),以允许一个系统访问另一个系统的信息或功能。为了快速、大规模地集成应用程序,API是使用协议或规范实现的,这些协议或规范定义了通...
-
9
C# Wrapper for Power BI REST API – Version 2 Some time ago, when Microsoft released the first version of Power BI Rest API I already wrote a wrap...
-
7
Samson Omojola Follow I'm an experienced software engineer. I love creating applications with responsive, beautiful, intuitive, state-of-the-art designs. I'm sk...
-
6
Spring REST GET API vs Spring GraphQL Query APIIn this tutorial, I would like to show you how to create GraphQL Query API using Spring Boot. Most software developers use REST API, so for a better understanding, I wil...
-
13
Introduction This article introduces a model-driven REST or GraphQL API for CRUD (Create, Read, Update, Delete). With it, you can write simple models (specifying a database table and the set of columns to be exposed) and the REST endpoin...
-
6
REST vs GraphQL vs gRPC三者API技术比较 3种最流行的A...
-
6
GraphQL + REST APILet’s understand how we can combine the best of Graph QL with REST API to achieve the maximum results.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK