Join Glenn Block for an in-depth discussion in this video Configure the GraphQL query and schema, part of API Development in .NET with GraphQL.
- [Instructor] Now we're going to go create our query. So let's go create a new class, and we'll call that OrdersQuery. Now we're going to include some namespaces, as you can see I'm doing on line one. So, we'll make our class public and, similar to our other types, we're going to derive from ObjectGraphType.
But, this time, for T we're just going to put object and that's because we don't have an existing class that we're mapping to. We're just creating a GraphQL type on the fly. So create our constructor and what we're going to do with our query is we're going to expose a query that will return a list of orders. So I'm going to inject our IOrderService. We'll just add that using statement in. Thank you Visual Studio.
So now what I want to do is I'm going to give it a name and then I'm going to define a field for getting back that list of orders. So the type that I'm going to use here is ListGraphType Order Type because of the fact that I'm returning a collection. You remember earlier we talked about how we're going to use this ListGraphType. So I'm going to give it a name from my field which will be orders and then I've got to give it that similar resolve function.
In this, I'm just going to call GetOrdersAsync because I want to return the list of orders. And finally I'm going to close that off. Great! So we've now finished creating our query. Next we'll go and define the schema. So I'm going to add another class and this one I'm going to call OrdersSchema. So we will also use GraphQL and we'll bring in GraphQL.Types.
I'm going to derive from a new type here that we haven't yet called schema. Now because my namespace is Schema, that's going to cause a namespace conflict. So, I'm just going to fully qualify it. So, we'll call GraphQL.Types.Schema. I'm going to create my constructor. And in my constructor, I'm going to pull in my OrdersQuery that I just created. And I'm also going to pull in the dependency resolver.
This is going to be necessary in order for dependency injection to work And you're going to see where we get to register this next. Now, I'm going to just map those parameters that were injected to the properties on the OrdersSchema object. So, it also has a dependency resolver. I'm going to set the resolver. Great, so I've now created my schema. I've created my query. The last thing I need to do is just register everything that we just created so that GraphQL.NET will be able to use it. So what I'm going to do is jump over to my startup class where I've already got some other registrations.
And let's start registering the types that we've been creating. Okay, so I'm going to register my OrderType. And I've got a few more registrations, so I'm just going to copy these. And I will paste another registration and that's going to be my CustomerType. And we're going to do another one which is going to be our OrderStatusesEnum.
And now we also need to register our query and our schema. So we'll do that, OrdersQuery. OrdersSchema. Finally, we need to register our dependency resolver. So for that we're going to use a slightly different method. And why we need to do the dependency resolver that's so that all these types that we've registered, will be available to GraphQL.NET. So to do that, we're basically going to use the AddSingleton of T, and we're going to use a different overload.
Now, I'm going to pass in IDependencyResolver. And I'm going to make sure that I bring in that GraphQL namespace so I can access that. And GraphQL.NET provides an out-of-the-box, very simple dependency resolver that allows you to pass in a function. So, I'm just going to use that. And I've called GetRequiredService of type.
So what this is going to actually do is whenever any object is constructed by GraphQL.NET, that needs to access a dependency, like a service, this is the code that's going to run. It's going to use this dependency resolver to do that. And that's why we have to do that configuration code. If we wouldn't have done that configuration code, we wouldn't have set the dependency resolver and you would have gotten errors at runtime. Let's see if it builds.
It does. So 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