Join Glenn Block for an in-depth discussion in this video Create order service, part of API Development in .NET with GraphQL.
- [Instructor] Now that we've created our CustomerService, let's go create the OrderService. So I'm going to right-click on Services. I'm going to add a new class. And I'm going to call it OrderService. Once again, because we're going to be using our models, on line 4 I'm going to include my models. And I'm also going to include Linq since we'll be working with that as well.
So I'm going to make that OrderService public on line 9. And now let's go create our interface. Now, the OrderService is just going to have two methods. One which returns an order, which we will call GetOrderByIdAsync. And it will accept an id.
Just get that using statement in. And then we will have another method that returns a collection of orders. And that will be called GetOrdersAsync. Now we'll go and implement our interface.
And similar to the CustomerService, we're also going to have a private collection of orders which we'll call orders. And we'll create a constructor. We'll initialize that orders collection, as I'm doing on line 16, to a new collection. And we'll paste in some sample data so you don't have to watch me type that. And now let's go implement our methods.
So our GetOrderByIdAsync method is going to return a task. And we're just going to look in the collection and match on the id. And then GetOrdersAsync is just going to give us the collection as an enumerable.
Okay, so we've completed the OrderService. The next thing that we're going to need to do now that we've created these services, we need to register them. So in our Startup method, you can see by line 18 we have this method called ConfigureServices. So now what we're going to do is register our services in our ConfigureServices method. And we're going to use the AddSingleton function. And I'm going to register my OrderService and my CustomerService.
Let's save it and build. Builds successfully. And now we can continue.
- 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