Integrating GraphQL with ASP Dot Net Core Part 2
This tutorial is the continuity of the previous part of this tutorial. Before having look at this tutorial please have a look at previous part of this Tutorial https://www.everestparked.com/post/integrating-graphql-with-asp-dot-net-core. In the previous part what we have done is we installed three dependencies for GraphQL which are: GraphQL, GraphQL.Server.Transports.AspNetCore, and GraphQL.Server.Ui.Playground. Which can be confirmed from viewing the installed nuget package as shown below:
Now moving on create a new Folder Named GraphQL. and Under GraphQL let us create a new folder named Types and under Types Create a Class Named UserType which inherits from ObjectGraphType<User>. as shown below:
Next, create a New Folder under GraphQL named Queries(or any thing you like). And inside Queries Create a new class named AppQuery that Inherits from ObjectGraphType. as shown below:
Next, Create a new Folder named AppSchema(or anyother) and Create a new Class Named AppSchema as shown below:
So, Finally our folder structure will look as shown below:
Now, Finally after doing all this, In Startup.cs File inside ConfigureServices Method add the following lines:
Similarly, Inside Configure Method Add the following line:
So, that's all we need to do in our coding part. Next, Build and Run the project and then navigate to /ui/playground. Here you can see the screen as shown below:
The above screen (i.e. Playground) is a place where we can test our query. the left part is the query for the request and the right part is the response that we get from the server. Let us now try to get the list of users from the server. For that first i will populate the users table with some data manually and then execute the following query.
After runnning the query we get the list of users which is shown in the screenshot below:
Here, we are getting the list of users with entities id, name and address. But, what if we need only id, and name Entities. For that we need to specify it in the request query itself which is shown below:
and then we get the response as shown below:
Here we only got id, and name from the list of users. Now, you may be wondering well, we sended the data using /ui/playground right? What if we want to use postman instead? do we really need this /ui/playground ? The answer is no we don't. Playground is just for easy testing purpose. If we had to use postman, the same step can be carried out by writing it under raw tab which is shown below:
As, the popularity of GraphQL is increasing, Postman has included the Separate tab for GraphQL with which we can easily send and receive our response. which is shown below:
In this way we successfully integrated GraphQL with ASP.NET Core. Thankyou for reading this article. If you encountered with any type of problem with this process. Please feel free to comment in the comment section of this article.