Join Glenn Block for an in-depth discussion in this video How to enable subscriptions, part of API Development in .NET with GraphQL.
- In this chapter, we're going to focus on subscriptions. Subscriptions are a very new and powerful feature of GraphQL. Basically, they provide a way to notify GraphQL clients about changes that are occurring within the system real-time through a push model. That's opposed to having those clients having to continually poll. Now the way that this is handled is conventionally through a web-socket-based communication. So what will happen is when a client is subscribing, it will keep a web socket open to the server.
So here's a diagram that illustrates how this works. So first we see we have our clients, and they're going to subscribe to notifications that are exposed on the GraphQL Endpoint. And basically the Endpoint can expose one or more notifications. So once the clients are subscribed, at some point a change is going to occur in the system. And when that happens, that's going to get published back to the GraphQL server. Then those clients who subscribed to that event are going to be notified.
And if you look in the diagram, you notice in this case that I haven't notified all of my clients because each of those clients subscribed to different events that they were interested in, and only the ones that had expressed interest in the particular event that is occurring right now got that notification. So how do we enable subscriptions? We're going to update our models and services. So we're actually going to have some additional models and an additional service, but we're also going to be updating some of our existing services as well.
We're going to have a new event GraphQL type, which is going to be used for the subscription side of things. We're also going to have a subscription, just like we've created a query and a mutation. We'll also have a high-level subscription. We're going to have to update the schema, and then we're going to do our configuration of the GraphQL server. Now if you remember when we first started the beginning of the course, I said that we were wiring up some web socket things which we'll use later.
This is that later. So we're not going to have to do that. But there is going to be some configuration around the additional services and types that we create. Once that's all done, we're going to be able to enable the server, and we'll be able to test subscriptions. And in this case we're actually going to use three GraphQL instances, 'cause we're basically going to show how I can have three instances running. And one of those will be performing an action to update using a mutation, and the other two will be subscribing and will get notifications as soon as that update happens.
Okay let's get started.
- Creating a new GraphQL project
- Exposing a new GraphQL API
- Enabling and creating mutations
- Updating and configuring the schema
- Enabling subscriptions
- New GraphQL.NET schema classes
- Creating the OrderEventType