From Query to Mutation with GraphQL.Conventions and ASP.NET Core

Image for post
Image for post

Introduction

What is GraphQL.Conventions?

Prerequisites

Creating a new project

dotnet new webapi --name GraphApp
cd GraphApp
dotnet run

Setting up GraphQL.Conventions

Adding the NuGet package

dotnet add package GraphQL.Conventions
PM> Install-Package GraphQL.Conventions

Preparing the query, mutation and user context object

Creating an implementation for IDependencyInjector

Injector.cs

Configuring Startup.cs

Startup.cs

Creating the API controller

GraphController.cs

Testing the GraphQL endpoint

dotnet run

Writing our first mutation

In-memory repository

TodoRepository.cs
services.AddScoped<ITodoRepository, TodoRepository>();

Add a GraphQL todo type

Todo.cs

Adapt the mutation class

Mutation.cs

Testing the AddTodo mutation

mutation AddTodo {
addTodo(description: "Do the dishes") {
id
description
}
}
{
"data": {
"addTodo": {
"id": "78081912-ec8e-4486-8326-3c23bb3c8fad",
"description": "Do the dishes"
}
}
}

Writing our first query

Adapt the query class

Query.cs

Testing the queries

query All {
todos {
id
description
}
}
query One {
todo(id: "abdbb6f9-ff50-4ea0-8c1b-24a4066b40ae") {
id
description
}
}
{
"data": {
"todos": []
}
}

Wrapping up

Software Engineer and Architect focusing on .NET and Microsoft technologies. Microsoft MVP. Practitioner of clean code. #solid #tdd #ddd #cqrs #es #graphql

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store