- [Instructor] We've created our models but we need a way to persist and retrieve them. To do that we're going to create a set of services which we can then register so that we'll be able to inject them into our GraphQL.net classes. Now, for the sake of this example, these services are just going to be in memory storage, but in the real world you'll probably call out to some type of external data store, or another API in order to retrieve and update your models.
So the first thing that we'll do is we will right click on our services, and we're going to create a new class, which we will call CustomerService. Now, before we do anything we're going to be using our models, so we're just going to pull in our Orders.Models namespace on line four and we'll make our CustomerService public. (instructor typing) And first we'll create an interface called ICustomerService.
This is so that when we register our CustomerService we register with the interface, which allows us to more easily test our application and maintain it. And on this interface we're going to create a method called GetCustomerById, which returns a customer. (instructor typing) And we'll create another version of that which is async (instructor typing) that is called GetCustomerByIdAsync.
Now, the reason we're creating an async method is because GraphQL.net will allow us to return tasks and this is really the right way to do things in order to build a scalable, responsive API. Lastly, we'll create a method that returns a collection of customers. (instructor typing) And we'll call that GetCustomersAsync. Now that we've created our interface we can go an implement it.
So we'll basically make our CustomerService, implement that interface, and we'll allow visual studio to generate for us our method steps. Now as we mentioned this CustomerService is just going to be storing an in-memory collection so lets go initialize a private variable to store the collection of customers. (instructor typing) Now we'll create a constructor and the first thing we'll do is we will initialize our customers collection.
(instructor typing) And finally we'll go an add some seed data. For sake of time, I'm just going to paste some sample customers, but you can find this in the Sample Customers text file in your Exercise folder. Let's go now and implement our methods. So the first thing we're going to do is implement the GetCustomerById method and what we'll do is we're just going to call against our other method.
That will implement our GetCustomerByIdAsync and return the result of the task. That's fine for this example. Next, we'll implement our GetCustomerByIdAsync, so that has to return a task, so we'll use Task.FromResult, and we will have it do a query. (instructor typing) So we'll just use link here, so I'm just enabling the link reference and we will have it return (instructor typing) whichever order matches on id.
(instructor typing) And then the last thing we'll do is return our full collection. (instructor typing) Okay, so that completes the CustomerService.
- 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