Join Glenn Block for an in-depth discussion in this video How to configure the schema, part of API Development in .NET with GraphQL.
- [Instructor] Now that we've created our models and services it's time to start using GraphQL.net so we can get our GraphQL server running. First we're going to configure the schema. Now here are the steps. We're going to create GraphQL types. GraphQL.net gives us a bunch of classes that we can use to define the GraphQL types that we're going to admit through our endpoint to our GraphQL clients. We're going to create a GraphQL query object. This is going to allow us to define the queries that will then surface to the clients which they can then issue against our endpoint.
We're going to create a schema object. We need to do this because the schema is what allows tools like graphical to inspect and see what types are available. It's also necessary to enable GraphQL clients. Then we're going to register all of those types with our asp.net core IOC container so that they will be available to GraphQL.net. Now GraphQL.net gives us, as I mentioned, a bunch of common classes that we can work with. And let's take a look at the ones that we're going to be using.
The schema object, that's self explanatory. That's where we're going to define our query and in the future our mutations and subscriptions. ObjectGraph<T> is what we're actually going to use to define GraphQL types. Now up until now you've seen us define our models. Those models are very close to our application. Now what we need to do is to define what is going to get surfaced to the GraphQL clients that are querying our endpoint and we're going to use ObjectGraph<T> to do this.
And the nice thing that GraphQL.net gives us is the ability to map between our GraphQL types and our native classes that we're using within our application. ListGraph<T> is just a convenience type that we'll use in order to expose a collection. CompletGraphType<T> is as we define our GraphQL types we're going to be defining fields on those types and there's a nice fluent API field function that we're going to use to do that.
And I'll just mention at this point that one of the common patterns you'll see when you use GraphQL.net is that it embraces this idea of using fluent APIs to give a declarative syntax. We're also going to use the EnumerationGraphType. GraphQL has enums and we need a way to be able to define those enums and that's what we use the EnumerationGraphType for.
- 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