Join Glenn Block for an in-depth discussion in this video Create models, part of API Development in .NET with GraphQL.
- [Instructor] Now that we've created our starter solution, let's get started implementing our endpoint. The nice thing about using GraphQL.NET is we can create our own models and map them into the GraphQL type system. First, we'll go and implement our customer model. So, I'm going to right click on the model's folder and I'm going to add a new class. Which I will call customer. I'm going to make that class public.
And then I'm going to create a constructor and I'm going to parametrize that constructor with a set of a few parameters. So, I'm going to give it an ID and I'm going to give it a name. Then I can rely on the magic of visual studio to go and create properties for me, for those parameters. And then one thing I'm going to do is to make the ID of a private setter. I'm also going to allow the name to have a public getter and setter.
Okay, so that completes our customer. Now let's create our order model. So, we'll right click on models and we'll do add class, order. We'll make that class public. And we'll give it a constructor with a bunch of parameters, we'll give it a name, a description, created date, customer ID, and a unique identifier.
And we'll go and turn those into properties. Save that first. Okay, so, get name, description, created, customer ID, and ID. Now, we'll set some of the accessor's and visibility. So, name will make have a setter. Description will have a setter. Created, we'll give it a private setter.
Customer ID, we'll give it a setter. And then with the ID we'll give that a private setter as well. The next thing we're going to need to do is add a status to our order because our order can have various statuses throughout its lifecycle. So to do that, we're going to create an enum called order statuses and we'll give it a bunch of status values. So, we'll say created equal two, processing equal four, completed equal eight, canceled equal sixteen, and closed equals 32.
And we're going to mark this with a flags attribute. This is going to allow this order statuses to have multiple values which will be useful later on when we create our subscriptions. And the last thing we're going to do is then add a status property to our order class. And we'll initialize it whenever the order gets created to a status of orderstatuses.created.
This completes creation of our customer and order model.
- 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