Join Glenn Block for an in-depth discussion in this video Creating the GraphQL API solution, part of API Development in .NET with GraphQL.
- [Instructor] To get started I've opened up Visual Studio and opened our Orders GraphQL sample solution. Let's take a look at what is here. So first I have an ASP.NET core web app which I just created the boiler plate application and then I made some slight changes to it. So I've gone into the www root folder and I've placed our static index.html and graphical files. This is what's going to allow us to use the graphical editor and this will be the place where we will host our GraphQL endpoint.
The other thing that I've done is I have created an orders class library. Now that library that's going to be where I define my models, my schema, and my services which my GraphQL endpoint will use. This is a good practice to keep these two separated rather than having one big monolithic application. It's going to make it easier to maintain and to test as we go forward. The one other thing I wanted to call out is I have a nuget.config here and the reason for that is the version of GraphQL.net that we are going to use is currently not available yet in NuGet but it is available in the GraphQL MyGet repository which is what we're going to use.
One other thing I wanted to call out is that if you'd like to create these projects from scratch yourselves I've added the nuget.config file as well as the files that are in the www root folder and I've put them in the starter files folder in with your exercise files. Now that you have an understanding of what is in the solution let's go and add some NuGet packages. So first we'll go to the orders project and I'm going to right-click on dependencies to get up the manage NuGet packages screen.
You'll notice that you can see the GraphQL-dotnet repo is showing up there. The MyGet repo where we're going to install those packages from and the first package that we're going to install is the GraphQL package. Now this package contains a bunch of helper libraries and classes that are really going to be key for us in building out our GraphQL endpoint and it contains hosting infrastructure. I'm going to accept the license for all the packages.
The next package that I'm going to install is the system.reactive package which we're also going to need later as we build out the solution. Now I'm going to install the stable release. Okay so that package has now been installed. Let's jump over to the server. So what I'm going to do is I'm going to right click on dependencies and do manage NuGet packages again.
This time I'm going to install the Microsoft.ASP.NETCore.StaticFiles package and the reason we need this is because we're going to be serving up that graphical interface. So we need to have a static file server as part of our endpoint. So I'm also going to accept the license and then the last packages I'm going to install are a set of transport packages. Now the reason I need these is because I am using GraphQL subscriptions which relies on web sockets and this will pull in the necessary packages to support that websockets transport.
So I'm going to install the websockets package and I'm going to accept the license and you can see that it's pulling in Microsoft's websockets package which it depends on. So now that I've got all the packages configured the last thing that I need to do is make some slight configuration changes to my server project.
So the first thing I'm going to do is remove this boilerplate code. This gets generated whenever you create the default ASP.NET core application. Now what I'm going to do is enable default files. This is so that whenever our endpoint is hit in the browser it will automatically surface the index.html and then I'm also going to enable the static file server.
The last thing I'll do is build my solution and I can see that it built fine. Now one other thing I want to point out is you'll notice I've created these subfolders for models, schema, and services. This is just an organizational mechanism and we'll be adding things into these folders as we go forward but you can have any folder structure that you like. Okay let's 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