GraphQL using .NET Boxed: Subscriptions

This post is going to continue my exploration of GraphQL using the .NET Boxed template as a jumping off point. The code I am starting with can be found here. Check out GraphQL using .NET Boxed: Mutations from last week for an exploration of mutations.

Subscriptions are GraphQL’s way of allowing a client to request notification of changes to the data.

Starting Point

As discovered a few weeks ago,  MainSchema is the central point to finding how GraphQL is set up in this template. For reference here is the full class.

Today we are interested in the  Subscription property which is being assigned a SubscriptionObject.

Subscription Object

The following is the full  SubscriptionObject for reference. I will point out a few details after the code.

A lot of this is going to look very similar to the other types we have reviewed for queries and mutations. For example, the  Type is  HumanCreatedEvent which derives from  HumanObject which is  ObjectGraphType around the  Human class.

One of the hardest things exploring GraphQL is getting a good handle on the object graph. I highly recommend you spend some time in these classes getting the connection solid in your mind.

As another example that should look pretty similar to things we coved in the other post is the  Resolver which is dealing with the base  Human type.

This next bit is new and deals with the actual notification of GraphQL when the  HumanRepository creates a new human. The following code has had the home planet related stuff removed for clarity.

What is  WhenHumanCreated? Looks like it is an observable provided by the HumanRepository.

Looking at the  AddHuman function you will see that this observable is provided a new value everytime a human is created which in turn provides notification to our GraphQL setup to notify any clients that are subscribed that a new human was added.

Wrapping Up

That covers my exploration of subscriptions. For me, this was the coolest part of the things I have seen in GraphQL.

I know this was a bit of a strange series as we just looked at the code generated by a template. I hope you found it useful. I know it helped me get a better grip on the idea behind GraphQL and how it can be handled in .NET Core.

The associated sample code can be found here.

Also published on Medium.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.