Join Glenn Block for an in-depth discussion in this video Create customer and order GraphQL types, part of API Development in .NET with GraphQL.
- [Instructor] Let's get started by creating our graphQL type. So we're going to put these all into the schema folder. So I'm going to right click on the schema folder and I'm going to add a new class and the first type that we want to define is our customer type. So I'm going to create a class called customer type. Now, we're going to start to use a name space, you'll see me put this in on line one that we haven't used up until now. And this is called graphQL.types. And this is where the common classes that we're going to use to define our schema, live.
So I'm going to make this class public, and what I'm going to do is to tell graphQL.net that this actually is a graphQL type. I'm going to define from that class that we mentioned earlier, ObjectGraphType of T. Now the T here is actually our model, I just added a using statement, by the way, for orders.models which you saw got added on line two because we're going to be using our models here. So what object graph type allows me to do is the T that i can pass in is my model, my customer model that I created previously and then it's going to give me a fluent API that I can use to do the mapping and to expose the fields.
So what I'm going to do now is I'm going to create a constructor and I'm going to use this nice handy method called Field. And Field is going to accept a lambda, and in that lambda, i can tell it, hey, i want to expose my Field ID. Now, there's other things I can do after this like set a description, I'm not going to do that for now. But if I did, those descriptions will actually populate back to the client when they use graphical and they view the schema. So I've got my ID, I'm going to also now define the name.
That's it, that's how easy it is to define my graphQL types. So let's go on to the next type. The next type's going to be a little bit more sophisticated. This is our order type. So I'm going to right click, I'm going to say add new, I'm going to add a class, and I'm going to call it, order type. Now you notice also from a naming perspective, that I'm putting type as the suffix. You can call it whatever you want to, that's just the convention to make it clear to me that this is not my domain model.
This is actually a graphQL type. So once again we're going to add our graphQL.types using statement on line one and then because we know we're going to use it, let's just go ahead and add our models in there. So we've got our order type, we're similarly going to derive from object graph type of T. And in this case, we're using order. We have a customer ID on an underlying order that we created before.
But what we actually want to do is expose the customer itself. This is the beauty of graphQL that it allows the client to get the entire graph, so the way that we're going to do that is you remember we created our customer service, the nice thing about graphQL.net is when these types get constructed, I can utilize an IFC container to inject services that that type might need, and you're going to see this in a minute. So what I'm going to do is I'm actually going to inject our customer service that we created before.
Now, I'll just go and have Visual Studio include the orders.services namespaces which you can see got added on line three. And now I'm going to start to define my fields again. So I'm going to map my ID. I'm going to map the name, description, so now what we want to do is a little bit more complicated.
So our order object exposes a customer ID but graphQL is all about allowing me to expose a graph of object so what I really want to do, is I want the graphQL order type to also expose the customer type so we can get access to it. And the way that we do this, is there's actually a field of T method, so instead of just using Field, we're going to use field of T and we're going to specify that we want to return a customer type.
So now what I'm going to do, is I'm going to specify the name of it. This is basically creating a new property that will show up on that order type that gets returned. And when I do this, and I use this Field overload, I need to provide it a resolve function and that resolve function is basically going to get invoked in order to retrieve the value. So in this case, what we want to do is use the customer id from the order to get back the actual customer. So what I'm going to do here is say customers.GetCustomerByIDAsync and you can see I'm using here that customers service that was injected.
The context object then exposes a source property and off of that source property, I can then get access to the customer ID which is what I need to have this resolve work. Finally what I'm going to do is expose the created field. So this was a little bit more of a sophisticated example but you can see the power here and how graphQL is playing in, how I'm able to now, take my graph of objects easily expose them and the other thing that you're seeing here with that customer example, is that I can dynamically create new properties that will show up in my graphQL schema that don't actually exist within my models.
- 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