GraphQL .NET Example
GraphQL example implementation in .NET 8.
Notice
Part of this blog post
LICENSE
MIT
Hello, everyone! This is my first time to write series. I will write series about GraphQL example project in .NET 6. Please make sure you are already downloaded some tools. Here the lists,
In part 1, I will not use databases. I will use in-memory lists. If you have many questions about GraphQL, I suggest you to learn more about GraphQL here.
dotnet new webapi -o GraphQLNetExample
dotnet new sln
dotnet sln add GraphQLNetExample
.csproj
file. This is my .csproj
definition.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GraphQL" Version="4.6.1" />
<PackageReference Include="GraphQL.MicrosoftDI" Version="4.6.1" />
<PackageReference Include="GraphQL.Server.Authorization.AspNetCore" Version="5.0.2" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore.SystemTextJson" Version="5.0.2" />
<PackageReference Include="GraphQL.Server.Ui.Altair" Version="5.0.2" />
<PackageReference Include="GraphQL.SystemTextJson" Version="4.6.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.5" />
</ItemGroup>
</Project>
Note: If you copy-paste the definition, don't forget to download the dependencies, you can use dotnet restore GraphQLNetExample
or dotnet restore
.
Create folder/directory under GraphQLNetExample
with name Notes
Create Note.cs
in GraphQLNetExample/Notes
. This class is the model definition.
namespace GraphQLNetExample.Notes;
public class Note
{
public Guid Id { get; set; }
public string Message { get; set; }
}
NoteType.cs
in GraphQLNetExample/Notes
. This class is the type definition for GraphQL.
using GraphQL.Types;
namespace GraphQLNetExample.Notes;
public class NoteType : ObjectGraphType<Note>
{
public NoteType()
{
Name = "Note";
Description = "Note Type";
Field(d => d.Id, nullable: false).Description("Note Id");
Field(d => d.Message, nullable: true).Description("Note Message");
}
}
GraphQLNetExample/Notes/NotesQuery.cs
. This class is for you define your query definition. You need to define the resolver, in this case we just use in-memory list.
using GraphQL.Types;
namespace GraphQLNetExample.Notes;
public class NotesQuery : ObjectGraphType
{
public NotesQuery()
{
Field<ListGraphType<NoteType>>("notes", resolve: context => new List<Note> {
new Note { Id = Guid.NewGuid(), Message = "Hello World!" },
new Note { Id = Guid.NewGuid(), Message = "Hello World! How are you?" }
});
}
}
GraphQLNetExample/Notes/NotesSchema.cs
. I use this class to register my query and define my schema.
using GraphQL.Types;
namespace GraphQLNetExample.Notes;
public class NotesSchema : Schema
{
public NotesSchema(IServiceProvider serviceProvider) : base(serviceProvider)
{
Query = serviceProvider.GetRequiredService<NotesQuery>();
}
}
Program.cs
. This is my Program.cs
.
using GraphQL;
using GraphQL.MicrosoftDI;
using GraphQL.Server;
using GraphQL.SystemTextJson;
using GraphQL.Types;
using GraphQLNetExample.Notes;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// add notes schema
builder.Services.AddSingleton<ISchema, NotesSchema>(services => new NotesSchema(new SelfActivatingServiceProvider(services)));
// register graphQL
builder.Services.AddGraphQL(options =>
{
options.EnableMetrics = true;
})
.AddSystemTextJson();
// default setup
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "GraphQLNetExample", Version = "v1" });
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "GraphQLNetExample v1"));
// add altair UI to development only
app.UseGraphQLAltair();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
// make sure all our schemas registered to route
app.UseGraphQL<ISchema>();
app.Run();
dotnet run --project GraphQLNetExample
https://localhost:7298/ui/altair
. You can open the URL at browser and see the UI like this.
{
notes {
id,
message
}
}
The result will be like this, please expect Id
will be different since we always generate that.
How about if you want message only? Yeah, that is handled by GraphQL. Change the query like this,
{
notes {
message
}
}
GraphQL example implementation in .NET 8.
Part of this blog post
MIT
Easy, right? In this part, you are already learned about GraphQL and how to write GraphQL query in .NET 6. If you have some questions, please comment here. I really appreciate with all your suggestions and opinions.
Thank you for following this article until the end of this part. I will continue the next part maybe next week or next month. I really appreciate if you have other queries about my article. The next part is continuing this article and we will learn more about mutation
and connect the query
& mutation
to database. Stay tune.