Code samples used in this blog series have been updated to latest version of .NET Core (5.0.4) and GraphQL-Dotnet (4.2.0). Follow this link to get the updated samples.
We will start by creating a blank ASP.Net Core App. Gradually we will create a middleware that can handle GraphQL requests.
dotnet new web
If you are familiar with ASP.NET Core middleware, you may have noticed in
Startup.cs, that it responses back "Hello World!" on application start-up. Here,
UseEndpoints is a middleware that executes an endpoint associated with the current request.
Practically a middleware is a delegate or more precisely; a
request delegate. As the name suggests, it handles an incoming request and decides whether or not to delegate it to the next middleware in the pipeline. In our case, we configured a request delegate using the
Run() (extension) method of
IApplicationBuilder. Between three extension methods (
Run() terminates the request for further modifications in the request pipeline.
The code in our console application was very simple and can only respond to a result of a hardcoded static query. However, in a real-world scenario, the query should be dynamic hence we must read it from the incoming request.
It's a rule of thumb that all GraphQL requests (either query/mutation/subscription) should be posted over HTTP
POST method. So, we change the
MapGet method to
MapPost. We also update the endpoint to a more specific one rather than the default root path (
A request body can contain a whole lot of fields, but let's say the passed in query comes within a field named
query. So we can parse out the JSON string content of the
body into a complex type that contains a
Every request delegate accepts a
HttpContext. If the query is posted over an HTTP request, you can easily read the request body as JSON using the following code and parse it to
var request = await JsonSerializer
PropertyNameCaseInsensitive = true
We are following the code-first approach for creating a schema. So, we have a Query class as follows,
Finally, we use
DocumentExecuter to execute the query. It returns a
ExecutionResult. We use the
DocumentWriter to write the
ExecutionResult to the context response body.
DocumentExecuteris available in
DocumentWriteris available in
So, after all the modifications, code in the
UseEndpoints method of
Startup.cs looks as follows,
Now you make a
POST request containing the query field using any rest client (Postman/Insomnia),
We are pretty much done with this post. But you can see that we have a lot of
newing ups of objects like the
new DocumentWriter(), etc. In the next post, we will see how we can use the built-in dependency system of
ASP.NET Core and make them injectable.