Over a million developers have joined DZone.

GraphQL End-Point Middleware for ASP.NET Core

DZone's Guide to

GraphQL End-Point Middleware for ASP.NET Core

We take a look at a new piece of middleware for ASP.NET Core that lets us create a GraphQL endpoint quickly and easily! Find out how you can make use of it.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

The feedback about my last blog post about the GraphQL end-point in ASP.NET Core was amazing. That post was mentioned on Reddit, many times shared on Twitter, linked on asp.net and - I'm pretty glad about that - it was mentioned in the ASP.NET Community Standup.

Because of that and because GraphQL is really awesome, I decided to make the GraphQL MiddleWare available as a NuGet package. I did some small improvements to make this MiddleWare more configurable and more easy to use in the Startup.cs


Currently, the package is a prerelease version. That means you need to activate to load preview versions of NuGet packages:

Install via Package Manager Console:

PM> Install-Package GraphQl.AspNetCore -Pre

Install via dotnet CLI:

dotnet add package GraphQl.AspNetCore --version 1.0.0-preview1

Using the Library

You still need to configure your GraphQL schema using the GraphQL-dotnet library, as described in my last post. If this is done, open your Startup.cs and add a using to the GraphQl.AspNetCore library:

using GraphQl.AspNetCore;

You can use two different ways to register the GraphQL Middleware:

app.UseGraphQl(new GraphQlMiddlewareOptions
  GraphApiUrl = "/graph", 
  RootGraphType = new BooksQuery(bookRepository),
  FormatOutput = true 
app.UseGraphQl(options =>
  options.GraphApiUrl = "/graph-api";
  options.RootGraphType = new BooksQuery(bookRepository);
  options.FormatOutput = false; 

Personally, I prefer the second way, which is more readable, in my opinion.

The root graph type needs to be passed to the GraphQlMiddlewareOptions object, depending on the implementation of your root graph type, you may need to inject the data repository or a EntityFramework DbContext, or whatever you want to use to access your data. In this case, I reuse the IBookRepository of the last post and pass it to the BooksQuery , which is my root graph type.

I registered the repository like this:

services.AddSingleton<IBookRepository, BookRepository>();

and needed to inject it to the Configure method:

public void Configure(
  IApplicationBuilder app,
  IHostingEnvironment env,
  ILoggerFactory loggerFactory,
  IBookRepository bookRepository)


Another valid option is to also add the BooksQuery to the dependency injection container and inject it to the Configure method.


The GraphQlMiddlewareOptions are pretty simple. Currently, there are only three properties to configure:

  • RootGraphType: This configures your GraphQL query schema and needs to be set. If this property is unset an ArgumentNullException will be thrown.
  • GraphApiUrl: This property defines your GraphQL endpoint path. The default is set to /graph which means your endpoint is available under //yourdomain.tld/graph
  • FormatOutput: This property defines whether the output is prettified and indented for debugging purposes. The default is set to false.

This should be enough for the first time. If needed it is possible to expose the Newtonsoft.JSON settings, which are used in GraphQL library later on.

One More Thing

I would be happy if you try this library and get me some feedback about it. A demo application to quickly start playing around with it is available on GitHub. Feel free to raise some issues and to create some PRs on GitHub to improve this MiddleWare.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

graphql ,.net ,asp.net core ,nuget ,integration

Published at DZone with permission of Juergen Gutsch, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}