Join Glenn Block for an in-depth discussion in this video Complete updating models and services, part of API Development in .NET with GraphQL.
- Okay. So we've now got our interface. Now let's go and implement it. So we'll come up here and we'll make the class implement that interface and then we'll have Visual Studio generate the stubs for us. And now we can fill those out. (typing sounds) So first thing that I'm going to do is I'm going to create a read-only event stream variable (typing sounds) and we'll need some using statements here, cause we're using reactive.
(typing sounds) This is also using reactive ReplaySubject. (typing sounds) Then I'm going to add a constructor, I'm going to initialize AllEvents, (typing sounds) I got a typo there. Okay, that's good.
And we will change this. (typing sounds) Okay. So now we'll implement our AddError, which is just going to call on the eventStream.OnError. This is also RX. This is how you basically pass on errors that are occurring on a stream. (typing sounds) And we will implement our AddEvent.
So what we're going to do is we're going to push that event onto the stream (typing sounds) we're going to call OnNext. (typing sounds) That will basically notify the clients. And then we're also going to return that orderEvent. (typing sounds) Lastly, we just need to implement our EventStream property, and what's that going to do is just return the stream AsObservable.
And we'll have a using statement that we'll need to include there as well. Okay. So now that we've created this service, the next thing we need to do is to get those notifications to actually happen. So to do this we're going to modify our order service, so that every time an order is created, or when it is started, we want to also generate an event. So here's how we're going to do this. Right here, we're just going to instantiate a new orderEvent, (typing sounds) and we're going to pass in the order.Id, the order.Name, we're going to give it a status of created, and we'll add a timestamp.
Okay, so the next thing we want to do is to add this event to our event service, but we haven't brought that in yet. So let's go do that now. So we're just going to go to the constructor here, and we're going to bring in our IOrderEventService, and then we'll have it create a field called events, and we'll just rename that (typing sounds) Okay. So now that I have that event, I can go here and I can add that event.
So I'm going to do events.AddEvent and I'm going to add the orderEvent. So now I've got CreateAsync working, let's do the same thing for when we do start. And I'm just going to copy the code here, cause it's basically going to be the identical code, just to save time. So I'm going to copy this event here, and I'm going to go down to our StartAsync, and I'm going to create a new event, and the only thing I'm going to change that's different is I'm going to change the status here to be processing.
Okay. We're almost done. The only other thing we have to do is register our service, so we're going to come back to our startup code like we've done before, and we're just going to add another registration. We'll do services.AddSingleton and we're going to register our IOrderEventService, OrderEventService. Okay, we're done. Now we can move on to our schema.
- 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