GraphQL with .Net Core Mvc

Gergő Nagygyörgy
4 min readJan 25, 2020

This article is about using GraphQL in a .Net core web server, by providing examples, and template projects.

Since I felt that GraphQL started gained traction, I decided to check whether it is available for .Net, since I felt like it is regularly mention in the Javascript scene. It is of course implemented and its been out in the wild for a while now.

And its worth noting to check these articles too, since they cover most of what you are going to need to know digging deeper into GraphQL(pagination and data loader for example).

I wrote this article assuming the reader is familiar with the GraphQL basics.

GraphQL

Building the back end to be REST API compatible was(and still is in some cases) the default architecture I considered it to be, and which everyone agreed upon.

New technologies rapidly pops up, solving shortcomings here and there(also introducing new ones), which can sometimes light up arguments about how it should be done considering X or Y design.

I consider GraphQL to be the next step forward in API design. Some of the benefits, and advantages to REST which are usually mentioned, can be found with an easy Google search.

+1

After having my first real experience with this stack, I realized that you can skip writing View Models, and look at GraphType definition class as a View Model in itself. You can add your own property, with any kind of graph type, and have it mapped with a custom resolver.

Query example from GraphQL dotnet documentation

+2

I was also looking to achieve inheritance on the controller action level, but it was impossible without writing a custom model binder (because .Net core does not support it out of the box), or adding some discriminator to the View Model and than using it in a condition which then used to resolve to the correct type.

GraphQL has a support for inheritance.(yes, it has a “secret” property called __typename).

The project has some more functionality which are not related to GraphQL, but worth talking(or writing?) about. In the next sections, I am going to present them.

Security management

There are two different types, one is Identity and the other is Group. Both of these are derived from SecurityObject base class, which only has an Id property declaration, which in turn will be used many times to fulfill permission handling at different stages of the application logic (like in achieving row level security).

The security id could point to a group or a single user instance, which gives a big flexibility to the application when it comes to managing permissions, ownership etc. on different kind of resources.

Stepper

There is another feature added in the project template which I call stepper. Its basically a workflow engine for custom development. I had some great time writing this with generic methods and messing around with type parameters and interfaces.

FlowOneController is a prepared example to demonstrate the usage of this functionality. You basically define states and state transitions on a diagram like you would in a workflow engine, which are in turn, in our case, gets translated into classes, in this case manually by hand(the goal was here to enjoy the good old classic OOP actually). These classes are than can be set up in many different ways. State transitions could produce tasks so before the next transition could be fulfilled, somebody needs to approve it(by implementing the IProducesTask interface). A transition can have a requirement(state change object base class has to be of type StateChangeHasTackPrerequityState which is an overloaded version of StateChange) of having to finish all the previous tasks, or just the one that has been created. Restrictions on what the starting state, and the ending state can be configured as well. Or which user or group of users has access to execute the desired state change with HasPermissionToFullfillChange configuration. These options can be set up through returning an instance of StateChangeConfiguration in the state change class. A View Model also can be included as a type param to StateChange base type which will be supplied to methods IsValid and StateAction of the state change object.

Tasks creation can be configured to assign the ability of finishing one task to many users, or assigning each users a different copy of the same task. Its up to the state change configuration.

This hierarchy supposed to show the possibilities can be achieved with type parameters in C#. Since I`ve done the very first version of this in about an hour or so, you could just go ahead and give it a try yourself and than see how it goes.

GraphQL compatible HTTP client

I’ve been using Postman since it got popular and been happily using it ever since. It has GraphQL support, but for me, its just not going to cut it. The usefull part of using a GraphQL compatible http client is having intellisense support, where Postman implementation of it fell short at the time of writing. Insomnia is another http client with more emphasis on GraphQL support. Where as postman does have an intellisense too but its not that capable of than Insomnia’s version.

An example project is available at this github repository. Templates for the database and web projects are available here, and here.

--

--