Join Glenn Block for an in-depth discussion in this video Enable server and issue a query, part of API Development in .NET with GraphQL.
- [Instructor] Now we're ready to enable our GraphQL server so I'm just going to go into the Startup class. And there's a couple of name spaces I need to pull in. So I'm going to pull in these two transports. And next I'll have some services that I need to add. You've been working with ASP.NET Core so you know how the model works.
You have to register the services and then you configure them. So we're going to add a GraphQLHttp service and we're also going to add a GraphQLWebSocket service specifying our schema. And then in our configuration we have to use these. So we'll go and first turn on WebSockets at the ASP.NET Core level. And then we will enable our GraphQLWebSocket and our GraphQLHttp.
Now both of these are just configuring the transports that we used above. Okay, let's save it. Rebuild. And now what we're going to do is run our server and try out GraphiQL, which we previously enabled. So here we can see the GraphiQL interface.
Now, if you've never worked with GraphQL before, this is a very powerful tool that allows us to actually query against our endpoint. Most likely when you develop your own application, you will use GraphiQL as a way to just test things out. Very often companies that are exposing GraphQL endpoints will actually expose GraphiQL as well. So, for example, if you use GitHub, GitHub has a GraphQL endpoint and they expose the GraphiQL editor. It's a really, really convenient and nice aspect of GraphQL that you can make this available to your users or use it for your own development.
So to test it out, I'm going to create a query. And one of the things you can see right away is that I'm getting syntax completion. So I can pull my list of orders and you can see that I can nicely pick the things that I want. So I can pick the id, the name, the description. Let's run that. So we can see that our endpoint actually returned us the list of orders, so it's working. And we can see all the fields that we specified: id, name, description.
If I drop out one of those fields, like I say oh I really don't want the description, I can just run it again and execute that query again and you can see that I got back exactly what I wanted. This is really the power when you get back to why you would even want to use GraphQL, you're seeing it right here. It puts so much power into the client to get exactly the things that they want to get. Now to really see this benefit here, let's take a look at that customer object. Because remember, the customer object is a related type, essentially, and I want to pull fields off the customer as well.
So I can specify customer and I can say, hey give me the customer id and give me the name. And now I can run that query. So now you see that not only did I get the orders back, but I also have the related customer information. And GraphQL lets us go as deep as we want to into that graph, pulling back exactly the things that we want to see. But there's another benefit. So if you look over on the right there, you can see docs. When I use GraphiQL I can actually browse the schema. So you can see that I can click on my query that I defined.
And now I see I have the orders type and I can see all the properties of the orders type. So, we're just getting started, but this should give you a flavor of what's so awesome about GraphQL and how you can utilize your ASP.NET skills to create a GraphQL endpoint using the GraphQL.net library.
- 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