Join Glenn Block for an in-depth discussion in this video Create the OrdersSubscription, part of API Development in .NET with GraphQL.
- [Instructor] Let's go create our orders subscription. So we're going to right click on schema. We're going to say add. We're going to add a new class, and we're going to call that orders subscription. This is going to be a lot of code and the lines are kind of long so I'm just going to auto hide the solution explorer. So we've got a couple of using statements we're going to need to add. So we're going to add graphQL dot types, graphQL dot subscription, and graphQL dot resolvers.
We're also going to need to access our services and our models. So we'll include those as well. Okay, so let's get started. We're going to make this class public, and we're going to make it inherit from object graph type of object. Now, the first thing is, we're going to need access to the collection of events. We're going to create a private events variable that holds our order events service.
Now we're going to go create the constructor and we're going to inject that order event service. And we will set the private variable equal to the injected instance. Let me scroll down here a little bit. Okay. Now we'll go and set the name. And we're going to need to create our field. So, I mentioned before how previously we were using these field accessers, this time we're going to use the add field method because subscriptions are not supported in the field helpers that we've been using before.
So this is going to be of event stream field type. We'll give it a name. And we'll give it arguments. Now in this case, the arguments that are going to be supplied by the clients will be a list of one or more statuses that they're interested in. They're also may be an empty collection. So we'll create our query arguments.
And because it's going to be a collection we'll use list graph type of order statuses enum. We'll give that a name and then we're also going to set the type which in this case will be type of order event type. Now we're going to have to supply a resolver and subscriber. So for the resolver, and I mentioned this in the overview where I talked about some of the new classes that we're going to use, we're going to use this func field resolver, which we don't have to deal with directly when we use the field builders.
So we're going to have a func field resolver of order event and we're going to pass it a function called resolve event, which we've not authored yet, but we're going to go author. We're also going to set the subscriber. So for that, we're going to use the event steam resolver type and we'll give a function called subscribe. Neither of which we've authored yet. Close this off. Now let's go write those two functions.
So the first function resolve event is going to return and order event and it's going to accept a resolve field context. That's that type that we haven't really had to deal with directly either, before. So it is going to grab the order event from the context and return it. And we can see that the squiggly line went away so that's working fine. Now let's go and create our subscribe event.
Now because the subscribe is a stream this is where we're using some RX. So we're going to give it an Iobservable of order event as the return value. And the subscribe function is now going to accept that the resolve event steam context, which is the context that is past for an event stream field. This code's going to get a little hairy, but I'm going to walk you through it. So, first we want to grab the status list that has been provided by the client in the request.
So we're going to use Get Argument, and the type that we're going to use is I List of order statuses. Now we can also set a default value, which we're going to do now, so we always get at least an empty collection. I was just going to simplify our code. Looks like I've got a typo here. Let's see why. Ah, I'm missing some parenthesis here.
So let me just put those in. There we go. Okay, so now we've got our status list. Let's check it, and see if the count is greater than zero, and if it is, we're going to go and first, set a temporary variable. This temporary variable statuses, this is going to hold the final list of statuses that the client has requested. Remember this is an enum flag, so we can set it to multiple values.
Now we're going to loop through the status and we're going to or against whatever status was provided. Lastly, we're going to return our event stream and we're going to apply on top of it a filter, I think we're missing one using statement there, so we will supply a filter where the status ends with the status that has been supplied.
This is basically what's going to allow us to filter out the values to get the appropriate filter. If no statuses were provided, we're just going to return everything. Oh, got a small typo there, I'll just fix that. Okay, so quite a bit of code that we wrote. Let's just build it and see if it works.
That's very promising. Now we can continue to the final step before we can test this out.
- 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