Join Glenn Block for an in-depth discussion in this video Update models and services, part of API Development in .NET with GraphQL.
- [Instructor] Now we're going to get started updating our models in order to support mutations. We're going to support two mutations, one for creating new orders, and a second for starting an order. Now, to support creation we need to provide what is known in GraphicQL as an input type, which is what the client will use to provide the values for the created object. What we're going to do is we're going to right-click on models. We're going to add a new class, and we're going to call that OrderCreateInput.
We'll make that class public. We're going to go add a few properties. So we will add a name of type string. We'll add a description, a customer ID, and we'll allow specifying the created date.
Next, we want to move on to supporting our second mutation, which is allowing starting the order. So if I come over to the order, currently you can see that status is a read only property. Now, we need that to be changed, but we don't want that changed simply by modifying the status from the outside. We want to expose an action that will allow starting that order. So what we're going to do is we're going to create a private setter here. Then I'm going to create a new method called Start.
And that method is just going to update the status to set it to be processing. The next thing we need to do is expose these on our service because remember that with everything we've done with GraphicQL using graphicgl.net, today basically we use our services as the way to get to the models to retrieve them. We're also going to use them similarly to do updates. So what we're going to do is go over to our OrderService, and we're going to modify the interface, and add two new methods.
One is actually going to be used for creation, and that is going to be called CreateAsync, and it is going to accept an order. We're going to have another method, which is called StartAsync, and that is going to accept an orderId. Now I'm going to have to go implement that on my interface, so I can come up here, and I can have Visual Studio generate the subs for me.
And let's go implement those methods, so we'll start with CreateAsync. We're first going to create a helper function called GetById that we'll need later. Now, we already have a function that allows retrieving an ID asynchronously. What this one is going to do is it's actually going to have some error handling, and we're going to see how error handling works. So I'm going to retrieve my order, and I'm going to use SingleOrDefault, which is a link function.
This way if it doesn't return any results, I will be able to handle that. Now what I'm going to do is check to see if that order is null. And if that order is null, I'm just going to throw a new arbitrary ArgumentException, and I'm going to pass it some information about the error. So I'm going to say that using string.Format. I'm going to say that order ID, and we'll use a placeholder is invalid.
And I'm going to pass in the ID. If everything is fine I'm just going to return that order. Now we'll go and implement our two methods. First, we'll actually do CreateAsync, that's going to be very simple. I'm just going to add my order, and then I'm going to return the order.
Next, we'll go do StartAsync. I'm going to use that new function that I wrote, GetById, and I'm going to pass it in, the orderId. Then I'm going to just start that order, and I'm going to return it. Okay, that's everything we need to do to enable our models and services.
- 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