hotchoc - graphql - getting started
Create a dotnet core api project using cli. I am using dotnet 8.
dotnet new webapi -n app
Then add the hotchoc packages
dotnet add package HotChocolate.AspNetCore
Then you can add the necessary code to Program.cs
using HotChocolate;
using HotChocolate.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
builder.AddGraphQLServer()
.AddQueryType<Query>();
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
AddQueryType<Query> - this query is a class that contain simple code here.
namespace app.model
{
public class Query
{
public Book GetBook() =>
new Book
{
Title = "C# in depth.",
Author = new Author
{
Name = "Jon Skeet"
}
};
}
public class Book
{
public string Title { get; set; }
public Author Author { get; set; }
}
public class Author
{
public string Name { get; set; }
}
}
Next you need to add
That's it. Run your project. In your browser, go to /graphql endpoint.
Simple sample code can be found here.
https://github.com/mitzenjeremywoo/hotchoc-tut
Comments