• This repository has been archived on 28/Apr/2023
  • Stars
    star
    183
  • Rank 210,154 (Top 5 %)
  • Language
    C#
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ASP.NET Core MiddleWare to create a GraphQL end-point

GraphQl.AspNetCore

This repo is outdated. See https://github.com/graphql-dotnet to use a really good GraphQL library for .NET

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

Preview builds on MyGet and release builds on NuGet.

Install that package via Package Manager Console:

PM> Install-Package GraphQl.AspNetCore

Install via dotnet CLI:

dotnet add package GraphQl.AspNetCore

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>();
// ... more types if needed

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

You can use different ways to register the GraphQlMiddleware:

// the simplest form to use GraphQl. defaults to '/graphql' with default options
app.UseGraphQl();

// or specify options only (default path)
app.UseGraphQl(new GraphQlMiddlewareOptions
{
    FormatOutput = true, // default
    ComplexityConfiguration = new ComplexityConfiguration()); //default
});

app.UseGraphQl(options =>
{
    options.EnableMetrics = true;
});

// or specify path and options

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.

  • SchemaName: This specifies the registered schema name to use. Leave null for the default schema.
  • AuthorizationPolicy: This configures the authorization policy name to apply to the GraphQL endpoint.
  • FormatOutput: This property defines whether the output is prettified and indented for debugging purposes. The default is set to true.
  • ComplexityConfiguration: This property is used to customize the complexity configuration.
  • ExposeExceptions: This property controls whether exception details such as stack traces should be returned to clients. This defaults to false and should only be set to true in the Development environment.
  • EnableMetrics: Enable metrics defaults to false. See GraphQL .net client documentation how to create a stats report.

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

Preview builds on MyGet and release builds on NuGet.

Install that package via Package Manager Console:

PM> Install-Package GraphQl.AspNetCore.Graphiql

Install via dotnet CLI:

dotnet add package GraphQl.AspNetCore.Graphiql

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 path a GraphQL endpoint.

Options

Currently the options just have two properties:

  • GraphQlEndpoint: This is the path of your GraphQL end-point, configured with the GraphQlMiddleware. In theory it could be any possible path or URL that provides an GraphQL endpoint. Until now, I just tested it with the GraphQlMiddleware.

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.

More Repositories

1

react-chat-demo

A demo application to play around with React, SignalR and ASP.NET Core
C#
60
star
2

GraphQlDemo

C#
34
star
3

blog

Personal blog about web development based on .NET and .NET Core
HTML
31
star
4

sharply

Blockly C# Editor
JavaScript
26
star
5

angular2-aspnetcore-vs

HTML
22
star
6

di-core-multi-demo

C#
18
star
7

unittesting-aspnetcore

C#
15
star
8

angular2-aspnetcore

JavaScript
14
star
9

InetaDatabase

A small project to learn Angular2 and Typescript. Using ASP.NET Core MVC in the backend.
C#
13
star
10

LightCore

LightCore is a lightweight dependency injection container which can be used as a service locator, too. It has originally been written by Peter Bucher, and is temporarily maintained by me.
C#
13
star
11

Gos.Tools

Small helper libraries I use to simplify my life as a developer ;)
C#
12
star
12

disqus-to-github-issues

C#
11
star
13

unit-testing-aspnetcore3

C#
10
star
14

dotnetconf2019

Demo Code for the ASP.NET Core Health Checks talk at the .NET Conf 2019
C#
7
star
15

aspnetcore3-demos

demos for the talk "What is new in ASP.NET Core 3.0"
C#
5
star
16

healthchecks-demo

C#
4
star
17

weatherstats-demo

Demo application about gRPC, Blazor, Worker Service and many more
C#
4
star
18

dotnetpro-ecollector

C#
2
star
19

razor-pages-demo

C#
2
star
20

async-proxy

C#
2
star
21

aspnetcore-and-react

Building a reliable web application using ASP.NET Core and React
1
star
22

aspnetcore-workshop

C#
1
star
23

dnc-iot-rpi-demos

JavaScript
1
star
24

NetCommunityEvents

.NET Community Event Kalender
JavaScript
1
star
25

RaspPiOwinWebHost

C#
1
star
26

feedkitchen

A feedburner alternative based on C# and Azure
C#
1
star
27

lightweight-devops

Book about lightweight DevOps using various open source tools
1
star
28

cd-using-cake

Demo project for the talk
PowerShell
1
star