Join Glenn Block for an in-depth discussion in this video Start updating models and services, part of API Development in .NET with GraphQL.
- [Instructor] The first thing we're going to need to do is to create a new order event model which will represent each event that occurs in the system. So to do this I'm going to right-click on models, I'm going to add a new class, and I'm going to call that Order Event, we'll make that public. And then similar to the other models we've created, we'll create a constructor and we'll start adding some parameters to it.
We're going to have an order ID because each event is going to correspond to a specific order. We'll have a name, we're going to have an order statuses collection, so if you remember when we created the order status we allowed it to use flags, we're going to go take advantage of that so that clients could actually subscribe to multiple statuses if they're interested, and then we're going to give it a timestamp.
So we'll go and generate properties for all of these, and we'll just go and modify some of the acessors on the properties. So we'll make that setable, we'll make this have a private setter so you can't change the timestamp once it's been created, and then we're going to add one more property which is going to be the unique identifier for this event, and we will initialize that to a Guid.
Great. So now we've got our event but what we need to do similar to the way we've done our other models is we're going to need to create a service for persisting and retrieving these events. So let's go over and we will create that service, we'll call it Order Event Service, so we're going to right-click on services, we're going to add a new class, and we'll call it Order Event Service, we'll make that public. Now, let's just talk a little bit about this before we go forward.
So, we are going to create a service, but unlike the services that we've done before, this time we're going to take advantage of the reactive framework in order to expose an event stream because we're going to have one or more clients that are subscribing to get notifications, and what this will allow by having an event stream on this event service, it will actually allow the server to subscribe to a stream for each client that is listening so that they only see the events that they're interested in.
So let's get started in doing that. So first we'll start off like we've been with creating our interface so, we'll create an interface called I Order Event Service, and we're going to be using some different collections too that are pretty powerful, so one, because of the fact that we're dealing with a stream, we're going to use a collection called Concurrent Stack. Now, neither of these usings have been included, so I'm going to include them. I'm just using Visual Studio to add those using statements in which you would see that popped at the top, system collections, dot concurrent namespace, which was added on line three, and also my order models.
So I'm going to create a property which is read-only called All Events. I'm going to make an add error method and add events method, that's how I'll add events to the stream, and now here's where I'm going to pull in some reactive magic, and I'm going to have an I observable of order event, and we will add an event stream which the server will subscribe to for each client so that they can see the events that they're interested in.
- 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