visit
GraphQL allows you to ask for what you want in a single query, save bandwidth, and reduce requests.
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
Install the “HotChocolate.AspNetCore” package from the NuGet gallery into the newly created solution in Visual Studio.
dotnet new tool-manifest
dotnet tool install NSwag.ConsoleCore
curl <SwaggerJsonFilePath> > swagger.json
Example
curl //localhost:44323/swagger/v1/swagger.json > swagger.json
dotnet nswag swagger2csclient
/input:swagger.json
/classname:<Clasname>
/namespace:<Namespace>
/output:<OutputFile>
dotnet nswag swagger2csclient
/input:swagger.json
/classname:RestClass
/namespace:RestNamespace
/output:ToDoService.cs
public void ConfigureServices(IServiceCollection services){
services.AddHttpClient<RestClass>();
services
.AddRouting()
.AddGraphQLServer()
.AddQueryType<Query>();
}
public class Query
{
public async Task<ICollection<TodoReader.WeatherForecast>> GetTodosAsync(
[Service] TodoService service,
CancellationToken cancellationToken)
{
return await service.WeatherForecastAsync(cancellationToken);
}
}
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapGraphQL();
});
That’s it, successfully integrated GraphQL playground to the .Net REST API along with swagger.