GraphQL In .NET Core Web API With Entity Framework Core - Part Two

In this article, we will see how we can pass an argument with GraphQL query and expose the same using Web API Core.

In the previous article, we have seen how we can make a GraphQL query on Employees and select the columns in which we would like to have a response. Today, we will see how we can declare a parameter, and with the same parameter value, how we can query and get the result accordingly.

We will use the same application which we have created in our previous article. So, add one more method in the repository called GetEmployeeById which expects employeeId as long.

IEmployeeRepository

  1. using System.Collections.Generic;  
  2. using System.Threading.Tasks;  
  3.   
  4. namespace GraphQLInWebApiCore  
  5. {  
  6.     public interface IEmployeeRepository  
  7.     {  
  8.     …  
  9.         Task<Employee> GetEmployeeById(long id);  
  10.     }  
  11. }  

EmployeeRepository

  1. using Microsoft.EntityFrameworkCore;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5.   
  6. namespace GraphQLInWebApiCore  
  7. {  
  8.     public class EmployeeRepository : IEmployeeRepository  
  9.     {  
  10.         …  
  11.         public Task<Employee> GetEmployeeById(long id)  
  12.         {  
  13.             return _context.Employee.SingleAsync(a => a.Id == id);  
  14.         }  
  15.     }  
  16. }  

In GraphQL implementation, we need to modify the query part, only as we haven’t changed any field in Employee, the schema will be the same to resolve the EmployeeQuery.

GraphQL In .NET Core Web API With Entity Framework Core 

In the query, we need to add one more field of type EmployeeType. We need to add the argument value. For that, we need to initialize QueryArguments which actually expects QueryArgument of type NonNullGraphType of IdGraphType. Then, we provide the argument name as id. Now at the time of resolving the context, pass the same id to the repository method which actually returns the employee details having that id.

EmployeeQuery

  1. using GraphQL.Types;  
  2.   
  3. namespace GraphQLInWebApiCore  
  4. {  
  5.     public class EmployeeQuery : ObjectGraphType  
  6.     {  
  7.         public EmployeeQuery(IEmployeeRepository employeeRepository)  
  8.         {  
  9.         …  
  10.             Field<EmployeeType>(  
  11.                 "employee",  
  12.                 arguments: new QueryArguments(new QueryArgument<NonNullGraphType<IdGraphType>>  
  13.                 { Name = "id" }),  
  14.                 resolve: context =>  
  15.                 {  
  16.                     var id = context.GetArgument<int>("id");  
  17.                     return employeeRepository.GetEmployeeById(id);  
  18.                 }  
  19.             );  
  20.         }  
  21.     }  
  22. }  

Run the application and in the query, we need to pass the argument; i.e., id. Here, for example, we have passed id:1. And at the right side, we can see that we get only one employee having id as 1.

GraphQL In .NET Core Web API With Entity Framework Core 

Now, in order to use GraphQL in API, add GraphQL.Client NuGet package.

Create one more "Get" method in EmployeeController with parameter id.

Initialize GraphQLClient with GraphQL URL

Create an instance of GraphQLRequest and write the same query which we tried in playground UI inside that. Along with the query, declare employeeId variable and set it with the value passed in the API.

Call PostAsync method of GraphQLClient and you will get the response.

Convert that response to Employee type and return it.

  1. using GraphQL.Client;  
  2. using GraphQL.Common.Request;  
  3. using Microsoft.AspNetCore.Mvc;  
  4. using System.Threading.Tasks;  
  5.   
  6. namespace GraphQLGraphTypeFirstSingleTableArgument.Controllers  
  7. {  
  8.     [Route("Employee")]  
  9.     public class EmployeeController : Controller  
  10.     {  
  11.         [HttpGet("{id}")]  
  12.         public async Task<Employee> Get(int id)  
  13.         {  
  14.             using (GraphQLClient graphQLClient = new GraphQLClient("http://localhost:64925/graphql"))  
  15.             {  
  16.                 var query = new GraphQLRequest  
  17.                 {  
  18.                     Query = @"   
  19.                         query employeeQuery($employeeId: ID!)  
  20.                         { employee(id: $employeeId)   
  21.                             { id name email   
  22.                             }  
  23.                         }",  
  24.                     Variables = new { employeeId = id }  
  25.                 };  
  26.                 var response = await graphQLClient.PostAsync(query);  
  27.                 return response.GetDataFieldAs<Employee>("employee");  
  28.             }  
  29.         }  
  30.     }  
  31. }  

Run the application and copy the URL from the browser. Open the Postman application, paste the copied URL, append with Employee/1, and click on the "Send" button.

You can see the employee details having id=1.

GraphQL In .NET Core Web API With Entity Framework Core 

So far, we have seen queries with one table only, in the next article we will see how we can configure GraphQL for multiple tables, like parent-child relationships.

You can download the sample from here.