Home

Awesome

GraphQl.AspNetCore

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 http://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

Branches & contributing & testing

The master branch is the stable branch and I don't axcept PRs to that branch. To contribute, please create PRs based on the develop branch. To play around with the latest changes, please also use the develop branch.

Changes on the develop branch ("next version" branch) will be pushed as preview releases to MyGet. To see whether this branch is stable, follow the builds on AppVeyor: Build status

Changes on the master branch ("current version" branch) will be pushed as releases to NuGet. To see whether this branch is stable, follow the builds on AppVeyor: Build status

Usage and short documentation

NuGet

Install that package 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.

First configure your schema(s) in the ConfigureServices method in Startup.cs. Make sure all referenced graph types are registered as well so they can be resolved from the container.

// Configure the default schema
services.AddGraphQl(schema =>
{
    schema.SetQueryType<BooksQuery>();
    schema.SetMutationType<BooksMutation>();
});

// Also register all graph types
services.AddSingleton<BooksQuery>();
services.AddSingleton<BooksMutation>();
services.AddSingleton<BookType>();
services.AddSingleton<AuthorType>();
services.AddSingleton<PublisherType>();

In the Configure method, you add the GraphQL middleware like this:

You can use two different ways to register the GraphQlMiddleware:

app.UseGraphQl("/graphql", new GraphQlMiddlewareOptions
{
    FormatOutput = true, // default
    ComplexityConfiguration = new ComplexityConfiguration()); //default
});

// or like this:

app.UseGraphQl("/graph-api", options =>
{
    options.SchemaName = "OtherSchema"; // only if additional schemas were registered in ConfigureServices
    //options.AuthorizationPolicy = "Authenticated"; // optional
});

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

Options

The GraphQlMiddlewareOptions are pretty simple.

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.

GraphQL.AspNetCore.Graphiql

This library provides a middleware to add a GraphiQL UI to your GraphQL endpoint. To learn more about it and the way I created it, read the blog post about it: GraphiQL for ASP.NET Core

NuGet

There's no NuGet Package available yet. I will provide one as soon as possible.

Using the library

Open your Startup.cs and configure the middleware in the Configure method.

You can use two different ways to register the GraphiqlMiddleware:

app.UseGraphiql("/graphiql", new GraphQlMiddlewareOptions
{
    GraphQlEndpoint = "/graphql"
});


app.UseGraphiql("/graphiql", options =>
{
    options.GraphQlEndpoint = "/graphql";
});

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

The GraphQlEndpoint needs to match the paht a GraphQL endpoint.

Options

Currently the options just have two properties:

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 here on GitHub. Feel free to raise some issues and to create some PRs to improve this MiddleWare.