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.
Join the DZone community and get the full member experience.
Join For Freethe 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
nuget
currently, the package is a prerelease version. that means you need to activate to load preview versions of nuget packages:
- package name: graphql.aspnetcore
- version: 1.0.0-preview1
- https://www.nuget.org/packages/graphql.aspnetcore/
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.
options
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.
Published at DZone with permission of Juergen Gutsch, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments