Join Glenn Block for an in-depth discussion in this video Create a subscription and fire a notification, part of API Development in .NET with GraphQL.
- [Narrator] Now let's go test out our subscriptions. You can see I've started the server in visual studio. Now to test subscriptions we need multiple graphical instances. You can see that I've opened two additional browser tabs. The tab on the left is where I'm going to perform mutations which will modify the state. The two tabs on the right are where we will create subscriptions and we should receive notifications when those mutations happen. So first we'll go and do the subscriptions.
The first subscription is going to be get get notified whenever the order is created. So I'm going to come over here and I'm going to say subscription. Order event. Now when I create a subscription, because of the fact that we have a parameter for the subscription which is the status. You'll see that shows up now. So I can do statuses, colon, and this is an array because I can give it multiple statuses. And I'm going to give it a status of created.
Now it's going to ask me for which fields I want to bring back. So we'll bring back the order ID, the name, and the status. Great. Now we can pull this over. let's go run our subscription. So you can see on the right it says your subscription data will appear. So what's going to happen is, what should happen as changes happen on this server and the orders get created, we should see them show up on the right.
let's do the second subscription. Now remember that subscription, we're just going to copy paste this. That is going to be to get notified whenever the order is started. So I'm going to copy this, come over here, paste. And we're just going to make this slight change here from created to processing. And now I'm going to run that.
Okay so now we have our two subscriptions running. And I just want to show you one thing 'cause I've mentioned how subscriptions use web sockets. So you can see here when I go to the console, you can see these web sockets that are running right now. It's telling me that I've got these active web socket connections. These are the real time streams that I showed in the diagram at the beginning of the chapter. Okay, now let's go and actually create a mutation. So, we'll create a mutation and we'll call it create order.
We've done this before. We're going to create the order. Now that is going to accept a parameter. So actually I have to put that parameter in. And that is going to be dollar sign, order, order input. And then for create order we will pass in the order. And we're going to get back the ID, the name. That's probably enough for us.
Okay, now what we need to do is modify our query variables because we now have an order variable that we need to define which is our order. So I want to go here to find a new order. Cintex completion is just really really nice here. So let's say test subscription create. And description, a test order.
Customer ID, we'll give it one. And then for created we'll say one, one, 2018. Okay, let's try it out here. So if this works correctly when I create this order I would expect that it would only show up on the top right window. So I'm going to click here. Boom. We can see right there on the right that our order was created and we got notified as we expected.
So now what we're going to do is take that same order and let's move it to a state of processing. We're going to copy that guid which is the order ID. And we're going to create another mutation. And that mutation will be start order. Same thing. In this case start order actually just takes an order as a parameter. So what we'll do is call start order and we'll pass in the order ID, putting the order ID that we just specified.
And then we just need to put our fields in for what we'll get when the order is started. So we'll get the ID and let's say the status. What am I missing here? Ah, this parentheses is not necessary. GraphQL will let you know when you do the wrong thing. Okay, so now what we're going to do is start that order. And what we would expect is that the top subscription will not show anything.
The bottom subscription should show us that that order is now being processed. And it does. So you've now seen here one of the really powerful aspects of GraphQL. I think subscriptions are really really exciting feature. Because imagine you have tons of clients that are, some are in the browsers, some are mobile apps. And in this day and age we really want to build these rich applications that are highly reactive and responding to the world around them.
And subscriptions provides a great way to do that. Now in this case you can see that I'm only subscribing to created and processing. Because when we implemented in the code these are the only events that we've supported. But from what you've learned in this chapter, I would challenge you to now go and implement support for the rest of the events. And this should be very easy to do just by following the patterns that we've already followed. Or maybe add some other kinds of events entirely.
Like when the customer gets created. You can do a lot with the fundamentals that you've learned in this chapter and in this course.
- 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