Integrating GraphQL with ASP Dot Net Core
Now, the question may arise why do we really need GraphQL and Why not use REST ? Well, one of the important advantage of using GraphQL is that it has only one endpoint. For example, if we are constructing an API using REST, what we used to do is that we will have separate url for insert(say: /api/user/insert), separate url for update (say: /api/user/update), separate url to get list of users (say: /api/users), similarly we will have separate url for delete and getById. So, by general speaking we will have 5 different endpoints for single object. what if we have 100's of Object? Of course our API will have maybe 500 endpoints. which ultimately will increase maintanance time which is not good. And with GraphQL we will have only one endpoint (say: /api/graphql) and all the request and response are sent and received through this endpoint only. Another advantage of using GraphQL is that we can get exact amount of data from the response. i.e say we only need name, and address from the response , if so we can get only those response by specifying it in the request. However, with REST this may not be the case. i.e sometimes we receive more data than we need and sometimes we get less. Now, before any further delay let us dive into the process of Integrating and using GraphQL with ASP.NET Core.
Step 1: First of all create a new ASP.NET Core Web Project in Visual Studio and then Click Ok by Providing Project Name.
Step 2: Next, Select WebApplication(Model, View, Controller) and then Click Ok.
Step 3: Now, In this step Let us create a context so that we can make a connection with the database. So, Make a new Folder named Data and then Create a class Named ApplicationDbContext (or anything you like). Next, let us Create a User Model with Entities(Id, Name and Address) inside Models Folder.
Now, Inside our ApplicationDbContext class just Created, Create a DBSet of User, and then add connection string on OnConfiguring Method as Shown below.
Now, next thing we need to do is Open Startup.cs file and then inside ConfigureServices Method add the following line:
After this open Project Manger Console and then run the following command to perform migration.
After sucessfull Database Update New Database will be Created Named GraphQLDemo with User Table in it as shown below.
Step 4: Next, let us create a Repository to perform operations related to User. So, Create a new folder Named Repository and then create a class Named UserRepository inside it. And in UserRepository class we will perform basic CRUD operations. Similarly, for our convenience let us create an interface named IUserRepositroy in the same file as shown below.
Now, we also need to inject the dependency for UserRepository. For this open Startup.cs file and then add following line in it.
After changes on Startup file ConfigureService Method in Startup.cs file will look as below:
Similarly, after all these operations, our solution explorer might look like this. The Highlighted items are the classes that we added. Whereas, the Classes Inside the Migrations folder were added while running the migrations above.
Step 5: Till now we haven't started with GraphQL yet. However, we just created a basic requirement before proceding to GraphQL. For Using GraphQL we will need 3 dependencies to be installed. i.e. GraphQL, GraphQL.Server.Transports.AspNetCore, and GraphQL.Server.Ui.Playground. Let, us install them one by one. for that RightClick on your Project and then click Manage Nuget Package . Now, under Browse tab type GraphQL and then install the package.
Next, Search for GraphQL.Server.Transports.AspNetCore and then install it.
Next, install GraphQL.Server.Ui.Playground.
These are all the dependencies that we need, and we installed them correctly. Since, this part is getting abit longer and we have lot to do with GraphQL, So let us break this tutorial into 2 parts. In the meantime if you encountered any problem in the process above, Please feel free to comment in the comment section below. Meanwhile, the Link of Part 2 is: https://www.everestparked.com/post/integrating-graphql-with-asp-dot-net-core-part-2 . Click Below for Part 2.